Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Await Addressable Instantion

Discussion in 'Addressables' started by MylesLambert, Sep 1, 2018.

  1. MylesLambert

    MylesLambert

    Joined:
    Dec 31, 2012
    Posts:
    61
    Hello!
    I'm wondering what the correct way to wait for an addressable to have loaded is?
    Using OnComplete callback is fine if you don't need to return anything but when I want to wait for the addressable to have finished what is the correct/ best way?
    To use coroutines or create a task and wait?

    Cheers,
    Myles
     
    KwahuNashoba likes this.
  2. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    Aside from using OnComplete, coroutines are the primary method we've been using. We really haven't explored using tasks to wait much, but if a user comes up with a good model around that, I don't see a reason to avoid it either.

    So the short answer is "we use coroutines a lot, but you should use what works for you"

    -Bill
     
    WaqasGameDev and MNNoxMortem like this.
  3. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    load operations are async stuff with a result, so await is actually best here.

    i.e. instead of doing this:
    Code (CSharp):
    1. var op = Addressables.LoadAsset<GameObject>(...);//assetRef.Load();
    2. yield return op;
    3. var go = op.Result;
    you can do:
    Code (CSharp):
    1. var go = await Addressables.LoadAsset<GameObject>(..); // go is GameObject
    also, you can better chain async methods than coroutines, with proper exception handling :)

    you don't need a Task for await. you can await anything tht has a
    GetAwaiter()
    methods that returns an object that:
    1) implements
    System.Runtime.CompilerServices.INotifyCompletion

    2) has a
    GetResult()
    method
    more info: https://blogs.msdn.microsoft.com/pfxteam/2011/01/13/await-anything/
     
    Sdk_fn and Thaina like this.
  4. Kolyasisan

    Kolyasisan

    Joined:
    Feb 2, 2015
    Posts:
    397
    To be honest, it is also a bit clunky for me. I just can't figure out how to use a coroutine to have everything suspended until Instantiate is complete, you know, like a vanilla Instantiate function. I want my game to migrate and use the addressables, have Addressables.Instantiate behave just like the usual Instantiate, but it seems that is not really possible when you need to halt literally everything else until the operation is complete.
     
  5. jeremies_unity

    jeremies_unity

    Joined:
    Oct 16, 2018
    Posts:
    26
    In production, coroutines are a real pain to use. Hard to debug, needs Monobehaviour, can't return objects, etc. Right now we must do some pretty hacky stuff to make addressables work with async/await. It would be MUCH simpler if IAsyncOperation implemented GetAwaiter().
     
  6. jeremies_unity

    jeremies_unity

    Joined:
    Oct 16, 2018
    Posts:
    26
    I'll post my GetAwaiter() implementation here in a bit for reference.
     
  7. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
  8. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,042
    Any news?
     
  9. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    We've got await coming in either the next or the next+1 release of Addressables (it'll be 1.0 or 1.1).

    Hope that'll help.
     
  10. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,042
    Nice!

    I hope to see await in more Unity APIs from now on.

    Do you know if it has been discussed internally?
     
    MNNoxMortem likes this.
  11. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    @N3uRo Can't remember where but I have seen someone from Unity post that await is on the radar.
     
  12. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,157
    Really, unity codebase should just use GetAwaiter everywhere since unity 2018. We should stop using Coroutine already
     
    MNNoxMortem likes this.
  13. madebynoxc

    madebynoxc

    Joined:
    Jan 14, 2014
    Posts:
    10
    I am using 1.1.10 and it doesn't seem to support await still. The code compiles and runs if I do
    Code (CSharp):
    1. var obj = await Addressables.InstantiateAsync(...).Task;
    but it doesn't seem to wait for anything, just launches and forgets

    Am I doing something wrong?
     
  14. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,091
    I think you're confusing with how it works. Your method will wait with executing further until the addressable you're requesting is instantiated.

    i.e. if you're running this (see code below) it will first log 'Waiting' and 'Waiting complete' and only then 'Initialized'
    So it depends on how you launch. If you use async void it will be acting like a fire and forget. Basically not waiting at all.
    if you await it as a Task it will wait until that Task is complete.

    Code (CSharp):
    1. private async void Start()
    2. {
    3.        await Init();
    4.        Debug.Log("Initialized!");
    5. }
    6.  
    7. public async Task Init()
    8. {
    9.     Debug.Log("Waiting");
    10.     var obj = await Addressables.InstantiateAsync(...).Task;
    11.     Debug.Log("Waiting complete");
    12. }
     
    yyylny and marie-hmm like this.
  15. madebynoxc

    madebynoxc

    Joined:
    Jan 14, 2014
    Posts:
    10
    Thank you for your reply.

    Apparently I did everything right and my logic was correct. But because I was stupid enough to make my method return UniTaskVoid instead of UniTask it was acting like 'fire and forget'

    Now the code looking much cleaner.
     
  16. tencnivel

    tencnivel

    Joined:
    Sep 26, 2017
    Posts:
    39
    I am attaching the following wrapper that allows to load an addressable with the async/await mechanism and also works in edit mode (assuming the key of the addressable is the default asset path).
    It works fine on windows and Android but not on iOS devices where it returns null.

    I made sure there is no problem with the asset bundles by running the game in the unity editor with "Play Mode Script" set to "Use Existing Build"

    @unity_bill any idea on how to make this works on iOS?

    I use Unity 2020.1.2f1 and Addressables 1.8
     

    Attached Files:

    Last edited: Aug 13, 2020
  17. tencnivel

    tencnivel

    Joined:
    Sep 26, 2017
    Posts:
    39
    It seems that there's nothing wrong with the code.
    After upgrading to the latest version of the Addressables package and deleting the addressables folder in StreamingAssets (it contained a link.xml, I am not 100% sure that this was the problem but it is now working).
    It is now working on an iPad Pro from 2015 with iOS13.
    It is still not working on the an iPhone 6 with iOS12. In the log I can see that the first file get downloaded but not the following ones.

    I am attaching the error log.
     

    Attached Files:

    Last edited: Aug 17, 2020
  18. madebynoxc

    madebynoxc

    Joined:
    Jan 14, 2014
    Posts:
    10
    For anyone interested I cannot recommend enough UniTask v2 asset that adds seamless allocation free async/await integration for Unity and solves problems with awaiting Addressables too
    https://github.com/Cysharp/UniTask
     
    gr3chu, glenneroo, EmilCoehl and 5 others like this.
  19. joanpescador

    joanpescador

    Joined:
    Dec 21, 2016
    Posts:
    122
    It's looks amaizing. Did you saw anywhere any tutorial for noobs, not in japanese?
     
    Wilhelm_LAS and gr3chu like this.
  20. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    The readme contains almost everything you need to know.