- Home /
Webpage freeze with WaitForCompletion()
Hi, When using WaitForCompletion() and build Webgl, webpage freeze with no error showing. Works fine on Editor. Tried 2019 to 2020 with addressable 1.17.17 to 1.18.4 allways freezing just when I add WaitForCompletion() on my code. Here the code. Any help??
public static Material CreateDoorMat(string cs)
{
string[] Code=cs.Split("&"[0]);
string color=Code[1];
GameObject.Find("HIDER").transform.Find("LoadingCircle").gameObject.SetActive(true);
var handle = Addressables.LoadAssetAsync<Texture2D>(Code[1]);
Texture2D T=handle.WaitForCompletion();
Material mata=new Material(Shader.Find("Standard"));
mata.SetTexture("_MainTex", T);
GameObject.Find("HIDER").transform.Find("LoadingCircle").gameObject.SetActive(false);
Addressables.Release(handle);
return mata;
}
Thanks,
Answer by Bunny83 · Jun 07, 2021 at 09:12 PM
Yes, of course it does because websites are single threaded. There is no way to wait for an asynchronous operation synchronously. Because you are freezing the main thread of your website, the operation can never finish. You can only use coroutines in such cases and work with callbacks. Without a coroutine activating your "loading circle" also has no effect since, again, you're freezing the execution inside this method. So there is no way to draw the next frame. Apart from that your use of GameObject.Find should be optimised and is also lacking error checking. You do 3 blind reference accesses in one line and each one could fail.
So you should use a coroutine just like in the example in the docs. It's not clear how or where you use this method, so it's impossible to show you how it should be changed.
Answer by Hec63 · Jun 08, 2021 at 03:06 PM
Hi, Thanks for the answer. As in unity document. The AsyncOperationHandle.Task property is not available on WebGL as multi-threaded operations are not supported on that platform.
I will do it with coroutine with callbacks,
Thanks,
using await handle; do the trick for WEBGL see this: https://forum.unity.com/threads/await-without-task.951329/
async void LoadAndInstantiate(string address)
{
var handle = Addressables.LoadAssetAsync<GameObject>(address);
var go = await handle;
Instantiate(go);
}
Hi, Hec: I tried the async extension approach you mentioned:
async void LoadAndInstantiate(string address) {
var handle = Addressables.LoadAssetAsync(address);
var go = await handle; Instantiate(go);
}
and it turned out to work at the same performance as the standard Coroutine approach:
IEnumerator LoadAndInstantiate(string address) {
var handle = Addressables.LoadAssetAsync(address);
yield return handle;
var go = handle.Result; Instantiate(go);
}
which, does NOT solve the problem: The browser is freezing during the process of asset downloading. This can be a fatal issue in WebGL projects that need to download dozens of images and 3D models after entering a scene. If it had solved your issue then did I miss something? Did you manage to solve it somehow?