I’d much prefer to not go through my entire project to replace all of my async/awaits with coroutines… Is there something I can replace await with which works in webgl?
Why do you think that async/await doesn’t work on WebGL? I’ve used async/await on WebGL just fine.
How exactly?
A simple await Task.Delay() causes a threading error.
For example, a project with just this one script will fail:
public class AwaitTest : MonoBehaviour
{
public GameObject Ball;
void Start() {
Drop();
}
private async void Drop() {
while (true)
{
Instantiate(Ball, Vector3.zero, Quaternion.identity);
await Task.Delay(500);
}
}
}
That is not (just) async. That is System.Threading. You cannot use System.Threading on WebGL (yet).
You’re right. So only alternative are coroutines?
Have you got solution , I am also facing this problem
UniTask is a good plugin.
Hi I am currently using async await for API calls along with Unity Web Request should I avoid it if I am planning for deploying on WebGL?
Yes You should avoid it. Because at the end it will not work in WebGl . I have faced similar issue. Use UniTask Instead.
Hi Katori, how did you go about doing that because it looks like you can’t?
Aync/Await absolutely works with WebGL, it’s System.Threading that doesn’t. Also, the new 2023.1 “Awaitables” (so await Awaitable rather than Task) absolutely work with WebGL, and will let you.
await Awaitable.WaitForSecondsAsync() works in WebGL, and will let you do the same as a task.delay.
Bonus points for the fact that using Awaitable means that async/await actually works sanely in the editor.
Don’t await BackgroundThreadAsync() though, that just drops off a cliff, since Awaitable doesn’t change the fact that threads don’t exist in WebGL.
I can also confirm async/await works in my projects’ WebGL builds without issue (first started using in 2022 LTS and am still using in 2023.3 alphas).