我正在尝试从我的服务器获取一些信息,现在我想在条形图上显示进度(如下载栏),但下载进度一直在继续。
这是我恢复帖子的功能
public IEnumerator RecoveryPosts(string username, string first_last_post, int id_post, Action<List<RecoveryPostsStruct>> Oncomplete)
{
string postsJson = "";
WWWForm form = new WWWForm();
form.AddField("RecoveryPosts",
"{\"username\": \"" + username + "\"," +
"\"first_last_post\": \"" + first_last_post + "\"," +
"\"id_post\": \"" + id_post + "\"}");
UnityWebRequest www = UnityWebRequest.Post(URI_USER, form);
Debug.Log("Execute".Tint(Color.yellow).Bold());
FindObjectOfType<PostController>().StartCoroutine(ShowDownloadProgress(www));
yield return www.Send();
if (!www.isNetworkError)
{
Debug.Log("Success!\n<color=#00e640><b>Recupera Posts</b></color>\n");
Debug.Log("Response:" + www.downloadHandler.text + "\n");
postsJson = www.downloadHandler.text;
if (postsJson != Util.ROUNDSTARS_ERROR)
{
List<RecoveryPostsStruct> listPostsStruct = new List<RecoveryPostsStruct>();
List<string> list = new List<string>();
RecoveryPostsJson[] posts = JsonHelper.FromJson<RecoveryPostsJson>(postsJson);
foreach (RecoveryPostsJson item in posts)
{
listPostsStruct.Add(new RecoveryPostsStruct(item.id_post, item.profilePicture, item.username, item.post, item.info_likes, item.qtd_comments, item.postPicture, item.date_post));
}
Oncomplete(listPostsStruct);
}
}
,这是为了显示进度
public IEnumerator ShowDownloadProgress(UnityWebRequest www) {
while (!www.isDone) {
if(LoadingScreen.instance != null)
LoadingScreen.Fill(www.downloadProgress);
Debug.Log(string.Format("Downloaded {0:P1}", www.downloadProgress));
yield return new WaitForSeconds(.1f);
}
Debug.Log("Done");
}
修改
这是新的ShowDownloadProgress:
public IEnumerator ShowDownloadProgress(UnityWebRequest www) {
while (!www.isDone) {
if(LoadingScreen.instance != null)
LoadingScreen.Fill(www.downloadProgress);
Debug.Log(string.Format("Downloaded {0:P1}", www.downloadProgress));
yield return null;
}
Debug.Log("Done");
}
这就是新的RecoveryPosts:
public IEnumerator RecoveryPosts(string username, string first_last_post, int id_post, Action<List<RecoveryPostsStruct>> Oncomplete)
{
string postsJson = "";
WWWForm form = new WWWForm();
form.AddField("RecoveryPosts",
"{\"username\": \"" + username + "\"," +
"\"first_last_post\": \"" + first_last_post + "\"," +
"\"id_post\": \"" + id_post + "\"}");
UnityWebRequest www = UnityWebRequest.Post(URI_USER, form);
Debug.Log("Execute".Tint(Color.yellow).Bold());
//Make request. Don't yield
www.SendWebRequest();
//Yield/wait in the ShowDownloadProgress until ShowDownloadProgress returns
yield return FindObjectOfType<PostController>().StartCoroutine(ShowDownloadProgress(www));
if (!www.isNetworkError)
{
Debug.Log("Success!\n<color=#00e640><b>Recupera Posts</b></color>\n");
Debug.Log("Response:" + www.downloadHandler.text + "\n");
postsJson = www.downloadHandler.text;
if (postsJson != Util.ROUNDSTARS_ERROR)
{
List<RecoveryPostsStruct> listPostsStruct = new List<RecoveryPostsStruct>();
List<string> list = new List<string>();
RecoveryPostsJson[] posts = JsonHelper.FromJson<RecoveryPostsJson>(postsJson);
foreach (RecoveryPostsJson item in posts)
{
listPostsStruct.Add(new RecoveryPostsStruct(item.id_post, item.profilePicture, item.username, item.post, item.info_likes, item.qtd_comments, item.postPicture, item.date_post));
}
Oncomplete(listPostsStruct);
}
}
else
{
Debug.Log("Error: " + www.error);
}
}
答案 0 :(得分:0)
您应该在调用www.isDone
之后才使用www.Send()
。这可能是为什么它从100变为0.此外,当您使用www.Send()
时,不要产生www.isDone
。如果您不关心或需要该请求的进度,则仅收取www.Send()
。最后,等待或屈服ShowDownloadProgress
函数,直到它返回,然后才能访问下载的数据或执行if (!www.isNetworkError)
。无需等待ShowDownloadProgress
功能,您将尝试在下载尚未完成时访问下载的数据。
在RecoveryPosts
功能中,更改
UnityWebRequest www = UnityWebRequest.Post(URI_USER, form);
Debug.Log("Execute".Tint(Color.yellow).Bold());
FindObjectOfType<PostController>().StartCoroutine(ShowDownloadProgress(www));
yield return www.Send();
到
UnityWebRequest www = UnityWebRequest.Post(URI_USER, form);
Debug.Log("Execute".Tint(Color.yellow).Bold());
//Make request. Don't yield
www.Send();
//Yield/wait in the ShowDownloadProgress until ShowDownloadProgress returns
yield return FindObjectOfType<PostController>().StartCoroutine(ShowDownloadProgress(www));
在ShowDownloadProgress
功能中,将yield return new WaitForSeconds(.1f);
更改为yield return null;
,以便每帧都取得进度,而不是每0.1
秒。
答案 1 :(得分:0)
我认为是服务器问题。首先我使用谷歌驱动器直接下载,它运行得很糟糕,和你一样的问题。然后,我尝试使用 github raw gists,它运行正常,再也没有回来。