Search Unity

Handy generic method to load any type of Addressable

Discussion in 'Addressables' started by GB_HB, Aug 21, 2019.

  1. GB_HB

    GB_HB

    Joined:
    Jul 14, 2019
    Posts:
    20
    Hopefully, this helps some people that may be having a confusing or hard time. When I started using addressables I found the docs weren't exactly newbie friendly. Anyways, I hope this quick tutorial and code example can help anyone that may be scratching their heads.

    Code

    Tutorial-
     
  2. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    973
    Code (CSharp):
    1. if (_assets == null)
    2.     yield return null;
    3. if(_assets.Count <= 0)
    4.     yield return null;
    5.  
    Shouldn’t that be yield break? Unless you are certain that _assets is not going to be null a frame later.

    Also, I am a bit confused about what this does:

    Code (CSharp):
    1.  
    2. var asset = GenericType<T>.Value;
    3. asset = op;
    4.  
    Why not just var asset = op or T asset = op?
     
    Last edited: Aug 22, 2019
    GB_HB likes this.
  3. GB_HB

    GB_HB

    Joined:
    Jul 14, 2019
    Posts:
    20
    I thought this as well, but when I use yield break I get an unresolved jump warning.

    I derpped, cannot tell you why I didn't do this. Must've been a leftover fragment from a previous version I really appreciate the fresh set of eyes, thank you for editing this. I've updated the code and will update the video asap
     
  4. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    973
    I guess the question is, what is the intention?
    yield return null;
    just yields for a frame,
    yield break;
    terminates the coroutine, which sounds like the correct thing to do if anything is null. Of course, in the particular example there, _assets is never going to be null as it is initialised at construction time of the component.

    Sorry if I'm sounding too critical, it's not my intention, it's just that considering this is meant to be something beginners can follow, it would be good to strip it back to the bare essentials so that they won't get confused by unnecessary stuff. :)
     
    GB_HB likes this.
  5. GB_HB

    GB_HB

    Joined:
    Jul 14, 2019
    Posts:
    20
    I don't think you sound critical at all, I really value your feedback.

    You are right that I shouldn't be doing a null check, I shouldn't be checking either here. As I already do the checks when loading the asset. Derpy on my part.


    Code (CSharp):
    1. private IEnumerator NullCheckThenUse()
    2.     {  
    3.         yield return new WaitUntil(() => AddressableLoader.IsLoaded);
    4.  
    5.         UseAssets();
    6.     }
     
    Last edited: Aug 26, 2019