'What is the difference between using AsyncOperation and not using AsyncOperation to show progress in UnityWebRequest?
I wrote a script that simply downloads a text file using UnityWebRequest. (I set the text size to 50 MB to see meaningful progress.)
private IEnumerator GetRequest(Uri uri)
{
using var unityWebRequest = UnityWebRequest.Get(uri);
var operation = unityWebRequest.SendWebRequest();
while (!operation.isDone)
{
progressText.text = $"Progress : {unityWebRequest.downloadProgress * 100:0.0}%";
yield return null;
}
}
The above code shows the desired result.
Then I got curious of one question. So I modified the code to not use the UnityWebRequestAsyncOperation variable.
private IEnumerator GetRequest(Uri uri)
{
using var unityWebRequest = UnityWebRequest.Get(uri);
unityWebRequest.SendWebRequest(); // Edit here
while (!unityWebRequest.isDone) // Edit here
{
progressText.text = $"Progress : {unityWebRequest.downloadProgress * 100:0.0}%";
yield return null;
}
}
But oddly enough, it works the same.
To solve this, I looked up the difference between UnityWebRequest.isDone and UnityWebRequestAsyncOperation.isDone.
https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest-isDone.html https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequestAsyncOperation.html
Even after reading the above two posts, I don't quite understand. Is there any difference between these two scenarios?
Solution 1:[1]
Async operation run on a separate thread and thus, do not hinder the game's normal flow. Let's say you are downloading a video from the internet in your game, which is 100 MB big. If you don't use async operations, the game will actually wait until this download is complete (and you might think it froze). When downloading small things like a text file or accessing a website, it can be ok to not use async methods, but I would recommend using async methods to not block the main thread, in case something goes wrong (like if the online connection takes longer for any reason).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | KBaker |
