You are really using WWW
and isDone
wrong. If you must use isDone
, you must put it in while loop. You must also yield in the while loop with yield return null;
otherwise the game will freeze until the download is done. This whole thing requires coroutine so the function must be made a coroutine function.
You really don't need isDone
. That's only used when you want to know the progress of the download.
Here is a proper way to use isDone
:
private static IEnumerator WaitUntilResolved(WWW request)
{
while (!request.isDone)
{
Debug.Log("Download Stat: " + request.progress);
//Wait each frame in each loop OR Unity would freeze
yield return null;
}
if (string.IsNullOrEmpty(request.error))
{
//Success
}
}
If you don't need to know the progress of the download then below is something you should use:
private static IEnumerator WaitUntilResolved(WWW request)
{
yield return request;
if (string.IsNullOrEmpty(request.error))
{
//Success
}
}
Finally, you cannot call a coroutine function directly. You have to use StartCoroutine
function to do that:
WWW www = new WWW("www.yahoo.com");
StartCoroutine(WaitUntilResolved(www));
EDIT:
How do I set a timeout for the coroutine?
Most WebRequest
tutorials uses timer. You don't need to do that in Unity with the WWW
API. It is not necessary here because this is a non blocking operation. If you must then see the code below.
private static IEnumerator WaitUntilResolved(WWW request)
{
float timeout = 5000, timer = 0;
while (!request.isDone)
{
Debug.Log("Download Stat: " + request.progress);
timer += Time.deltaTime;
if (timer >= timeout)
{
Debug.Log("Timeout happened");
//Break out of the loop
yield break;
}
//Wait each frame in each loop OR Unity would freeze
yield return null;
}
if (string.IsNullOrEmpty(request.error))
{
//Success
}
}
If you need to return the status of the download, see this post that explains how to do that.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…