Using AddressableAssets.LoadAssetsAsync

Viewed 8023

The documentation is quite thin on how to use Addressables.LoadAssetsAsync ?

When using a single string as first parameter, it work the same as using Addressables.LoadAssetAsync.

So this is working:

    Addressables.LoadAssetsAsync<TextAsset>
          ("questions",OnDownloadQuestionsCategoryComplete)
          .Completed += OnDownloadQuestionsComplete;

But this is generating an exception:

    Addressables.LoadAssetsAsync<TextAsset>(
          new string[] {"questions"},
          OnDownloadQuestionsCategoryComplete)
          .Completed += OnDownloadQuestionsComplete;

The exception generated is:

ChainOperation of Type: System.Collections.Generic.IList`1[UnityEngine.TextAsset] 
failed because dependent operation failed
Exception of type 'UnityEngine.AddressableAssets.InvalidKeyException' was thrown., Key=System.String[]
1 Answers

When using a list of keys on LoadAssetsAsync, you need to add the 3rd parameter for MergeMode. Otherwise, your list is treated as a list of IResourceLocation.

private void DownloadQuestions()
{
    string[] arrQuestions = new string[]
    {
        "categories",
        "questions_1",
        "questions_2",
        "questions_3",
        "questions_4"
    };

    Addressables.LoadAssetsAsync<TextAsset>(
        arrQuestions,
        OnDownloadQuestionsCategoryComplete,
        Addressables.MergeMode.Union).Completed += OnDownloadQuestionsComplete;
}

private void OnDownloadQuestionsCategoryComplete(
    TextAsset obj) {}

private void OnDownloadQuestionsComplete(
    AsyncOperationHandle<IList<TextAsset>> listAssets) {}
Related