Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Loading multiple assets of a type using tags

Discussion in 'Addressables' started by t1000, Apr 2, 2023.

  1. t1000

    t1000

    Joined:
    Jan 30, 2016
    Posts:
    16
    Issue

    How on Earth addressable work? I have tried to load a single asset which worked then obviously that is not convenient for my use case so I tried to load various at the same time and get an array with them, but it does not find them.
    1. I had created a new group in Addressables (also tried with default)
    2. I had tagged the two only assets that I have there currently as "weapons")
    3. I have used Addressables.MergeMode.None as well as Addressables.MergeMode.Union.
    4. I am able to use LoadAssetAsync passing the asset name, but no way to load multiple assets using labels because LoadAssetsAsync says the key is not found no matter what I put.
    Current code which should work

    Code (CSharp):
    1.  
    2.     private async void LoadAssets()
    3.     {
    4.         AsyncOperationHandle<IList<WeaponScriptableObject>> handle = Addressables.LoadAssetsAsync<WeaponScriptableObject>("weapons", null, Addressables.MergeMode.None);
    5.         await handle.Task;
    6.  
    7.         if (handle.Status == AsyncOperationStatus.Succeeded)
    8.         {
    9.             WeaponScriptableObject[] weapons = handle.Result.ToArray();
    10.             foreach (WeaponScriptableObject weapon in weapons)
    11.             {
    12.                 Debug.Log($"(Weapn has been loaded via dataloader {weapon.weaponName}");
    13.             }
    14.         }
    15.  
    16.     }
    The Unity docs on the matter are rather impossible to follow for new comers. The examples are terrible and overcomplicated so ended up lost. If someone can help on this matter or throw some light on what am I doing wrong that would be appreciated.

    My goal

    Have a list of ScriptableObjects "indexed" as Addressable Objects tagged with whatever label, and then I want to loop through that list of assets in the Addressable Objects Group and save all the returned ScriptableObjects as JSON into disk at an arbitrary location.

    Thanks in advance
     
    Last edited: Apr 2, 2023
  2. t1000

    t1000

    Joined:
    Jan 30, 2016
    Posts:
    16
    ANSWER:

    You cannot pass a string as argument for Addressables.LoadAssetsAsync, it needs to be a list of strings with the keys, apparently. That way my code above worked.

    However the documentation examples clearly use "objectKey" as reference like here
    https://docs.unity3d.com/Packages/c...ableAssets.html#addressablesloadassetsasync-1

    Unity gives people this example
    Code (CSharp):
    1. IEnumerator LoadAllAssetsByKey()
    2. {
    3.     //Will load all objects that match the given key.
    4.     //If this key is an Addressable label, it will load all assets marked with that label
    5.     AsyncOperationHandle<IList<GameObject>> loadWithSingleKeyHandle = Addressables.LoadAssetsAsync<GameObject>("objectKey", obj =>
    Which DOES NOT work. So the methods arguments provided are wrong, wrong signatures.

    How annoying.