Search Unity

[Request] AsyncOperationHandle AssetReference.LoadAssets (IEnumerable<AssetReference> References)

Discussion in 'Addressables' started by TextusGames, Aug 12, 2019.

  1. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    64
    This function should act like AssetReference.LoadAssetSync(). It should assign Asset variable of AssetReference.

    Preferable as an extension method with the ability to write something like this
    Code (CSharp):
    1. public List <AssetReference>  References;
    2. private AsyncOperationHandle operation;
    3.  
    4. void  Start()
    5. {
    6.      operation = References.LoadAssets();
    7.      operation.Comleted += DoSomethingOnComplete;
    8. }
    9.  
    10. void Update()
    11. {
    12.     if (operation.IsDone)
    13.         return;
    14.  
    15.     Debug.Log( "Loaded" + operation.CompletionPercent)
    16.  
    17. }
    You have provided a basic sample project called "ListOfReferences" there you just iterate for each asset reference and load it and on completion decrement some index in order to know then all assets are loaded.
    This approach assigns Asset field of each AssetReference.
    But this approach does not provide AsyncOperationHandle benefits.

    The second approach is to call operation = Addressables.LoadAssets( References.Select(r => r.RuntimeKey), SomeAction, MergeMode.Union); . Here you get the
    AsyncOperationHandle with it benefits.
    but this does not assigns Asset field of each AssetReference becouse it has not called AssetReference.LoasdResourceAsync().
    And there is no way to know which asset reference corresponds to which loaded object( because Addressables.LoadAssetsAsync() returns only unique assets and if multiple asset references have the same runtime keys the size of initial AssetReferences and the size of the returned list of objects will not be equal).

    So it would be convenient to have some built-in method to load a collection of AssetRferences with both benefits.