Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Async Await on Android not working

Discussion in 'Scripting' started by LiableDuke, Apr 1, 2021.

  1. LiableDuke

    LiableDuke

    Joined:
    Feb 8, 2018
    Posts:
    32
    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

    Code (CSharp):
    1.  
    2. private async Task LoadModelAsync(string bundleName)
    3. {
    4.     int versionToCheck = (versions.ContainsKey(bundleName)) ? versions[bundleName] : 0;
    5.     string path = "";
    6.  
    7. #if UNITY_IOS
    8.     path = "https://" + SERVER_URL + "/iOS/" + bundleName;
    9. #elif UNITY_ANDROID
    10.     path = "https://" + SERVER_URL + "/Android/" + bundleName;
    11. #endif
    12.  
    13.     Debug.Log("Path: " + path);
    14.  
    15.     UnityWebRequest www = new UnityWebRequest(path);
    16.     www.downloadHandler = new DownloadHandlerBuffer();
    17.     await www.SendWebRequest();
    18.  
    19.     Debug.Log(www.isNetworkError);
    20.     Debug.Log(www.isHttpError);
    21.     Debug.Log(www.isDone);
    22.  
    23.     Debug.Log(www.downloadHandler);
    24.     Debug.Log(www.downloadHandler.data);
    25.  
    26.     AssetBundle assetBundle =
    27. AssetBundle.LoadFromMemoryAsync(www.downloadHandler.data).assetBundle;
    28.     loadedBundles.Add(assetBundle);
    29.  
    30.     // return bundle
    31.     if (OnBundleLoaded != null) OnBundleLoaded(assetBundle);
    32.     Debug.Log("Download complete and added to list");
    33. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    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. :)

    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.
     
    Brathnann likes this.
  3. Simon-Larsen

    Simon-Larsen

    Joined:
    Feb 26, 2013
    Posts:
    11
    Hard to say without the full code, but it looks like you got a "LoadFromMemoryAsync" async method which is never awaited
     
    VolodymyrBS likes this.
  4. tomkail_betterup

    tomkail_betterup

    Joined:
    Nov 17, 2021
    Posts:
    100
    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.
     
  5. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    `AssetBundle.LoadFromMemoryAsync` returns a other async op, you will need to await it as well
     
    Lorrak likes this.