Async Await on Android not working

I am trying to use async await from c# .net. It works fine in the editor but when I build it no longer work. It doesn’t break but the code doesn’t runs async

private async Task LoadModelAsync(string bundleName)
{
    int versionToCheck = (versions.ContainsKey(bundleName)) ? versions[bundleName] : 0;
    string path = "";

#if UNITY_IOS
    path = "https://" + SERVER_URL + "/iOS/" + bundleName;
#elif UNITY_ANDROID
    path = "https://" + SERVER_URL + "/Android/" + bundleName;
#endif

    Debug.Log("Path: " + path);

    UnityWebRequest www = new UnityWebRequest(path);
    www.downloadHandler = new DownloadHandlerBuffer();
    await www.SendWebRequest();

    Debug.Log(www.isNetworkError);
    Debug.Log(www.isHttpError);
    Debug.Log(www.isDone);

    Debug.Log(www.downloadHandler);
    Debug.Log(www.downloadHandler.data);

    AssetBundle assetBundle =
AssetBundle.LoadFromMemoryAsync(www.downloadHandler.data).assetBundle;
    loadedBundles.Add(assetBundle);

    // return bundle
    if (OnBundleLoaded != null) OnBundleLoaded(assetBundle);
    Debug.Log("Download complete and added to list");
}

Any reason you’re not just using coroutines? They work all day long for UnityWebRequest processing, whereas this newfangled async crud seems to show up in a lot of “this doesn’t work” questions here on the forum.

You know, the basic “when in Rome” type approach. :slight_smile:

Also, remember to respect the IDisposable interface of the UnityWebRequest objects and properly .Dispose() your stuff when done, otherwise you could leak resources on certain targets.

1 Like

Hard to say without the full code, but it looks like you got a “LoadFromMemoryAsync” async method which is never awaited

1 Like

Unless we’ve messed something up, the same appears to be true of iOS.
The C# async are a bit weird, but they’re also incredibly useful and used by a lot of 3rd party APIs. They’re well worth understanding.

AssetBundle.LoadFromMemoryAsync returns a other async op, you will need to await it as well

1 Like