Search Unity

I can't figure out how to use LoadAssets()

Discussion in 'Addressables' started by PhilSA, Mar 26, 2019.

  1. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    I have a list of asset references, and I want to load them all and do something once they are all loaded.

    Code (CSharp):
    1.  
    2. public List<AssetReferenceGameObject> SpawnableAssets = new List<AssetReferenceGameObject>();
    3.  
    4. public void PreloadAssets()
    5. {
    6.     Addressables.LoadAssets<AssetReferenceGameObject>(SpawnableAssets, opCallback);
    7. }
    8.  
    9. private void OnPreLoadComplete(IAsyncOperation<AssetReferenceGameObject> op)
    10. {
    11.     Debug.Log("Pre-load complete");
    12. }
    13.  

    The purpose here is that once my "SpawnableAssets" list will be loaded, I will be able to instantiate them instantly via

    Code (CSharp):
    1.  
    2. // Note: I need to instantiate like this because I cannot instantiate them in async in this project. My assets need to be ready for instant instantiation
    3. GameObject.Instantiate(SpawnableAssets[i].Asset);
    4.  

    However, get the following errors and my "Pre-load complete" never shows:


    Does anyone have ideas? I haven't been able to find proper examples of LoadAssets(). I also do not really understand the purpose of the "callback" passed as argument to "LoadAssets", when the method returns the IAsyncOperation anyway
     
    Last edited: Mar 26, 2019
  2. taptpci

    taptpci

    Joined:
    Jan 31, 2018
    Posts:
    11
    Code (CSharp):
    1. public List<AssetReferenceGameObject> ...
    Instead of AssetReferenceGameObject, try a list of string or AssetReference, where the string is the address or the AssetReference is simply set via the addressable asset dropdown.

    Edit : also, you probably should be doing a yield return

    Code (CSharp):
    1. yield return Addressables.LoadAssets<AssetReferenceGameObject>(listOfAddresses, opCallback);
    Which means you'd want PreloadAssets() to return IEnumerator.