Search Unity

Loading Addressable directory

Discussion in 'Addressables' started by avigo, Apr 6, 2019.

  1. avigo

    avigo

    Joined:
    Jul 3, 2017
    Posts:
    4
    Hi,
    I have a directory with 100 sprites in it, I want to load all of those sprites and show them to the user.
    I tried marking the directory itself as the addressable, but then, how do I load it. I mean what is the type (***)
    var operation = Addressables.LoadAsset<***>(direcotryKey);

    I can mark all the sprites in the directory as addressables and load them 1 by 1 with:
    var operation = Addressables.LoadAsset<Sprite>(spriteKey);
    or even use LoadAssets functions.
    but then I'll have to load them by their names (and there are 100 of them) so I'll need to save their names somewhere, which is not good for me.

    Is there a proper way to do what I want to accomplish?

    Thanks!
     
  2. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    either by label, or with a ScriptableObject holding an array of your sprites (or AssetReference)
     
    Denis-535 and avigo like this.
  3. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    You need `LoadAssets` (note the 's'). and the type is Sprite. As to the key, you cannot send in the directoryKey. That key is just used to properly name the things inside. If however, you label your directory as @M_R suggested, all the contents will have that label as well. So you want `Addressables.LoadAssets<Sprite>("directoryLabel")`
     
    minhdangle2512 and Smireles like this.
  4. kreager

    kreager

    Joined:
    Mar 22, 2020
    Posts:
    2
    I'm running Unity 2019.3.7f1 and I need a script to swap Texture2D(type Default) from my Default addressable group using System.Collections.Generic.List to loop through and assign a single texture to my MeshRenderer Shader _BaseMap property. I've had success with SpriteRenderer, swapping out Sprites from a SpriteAtlas, but can not use the Texture2D(type Sprite 2D and UI) with MeshRenderer.

    @unity_bill Using your earlier example I've place all my Texture2D (type Default) assets into a project folder called "FolderOfFronts", I then highlighted them all and clicked on the Blue Label icon near the bottom right of the Inspector window, and I created a new Label called "CardFronts". I verified that all the intended Texture2D(type Default) assets were Labeled. I dragged over the "FolderOfFronts" into the Default addressable group (check the Addressable box). I changed the Addressable name of the folder, removing the directory path, to just "FolderOfFronts". As you suggested, I'm attempting to KEY off of this Label "CardFronts"

    @unity_bill Note(Apr 16, 2019): Addressables.LoadAssets, I could not find any documentation on this, and it seems that LoadAsset is now Obsolete, so I have to load using either Addressables.LoadAssetAsync<Texture2D>("MyAddressableLabel") or Sub-Assets using Addressables.LoadAsset<IList<Texture2D>>("MyAddressableLabel")

    My failed attempt getting a list of Texture2D assets using the label I created.
    UnityEngine.ResourceManagement.ResourceManager+CompletedOperation`1[System.Collections.Generic.IList`1[UnityEngine.Texture2D]], result='', status='Failed': Exception of type 'UnityEngine.AddressableAssets.InvalidKeyException' was thrown., Key=CardFronts, Type=UnityEngine.Texture2D

    Code (CSharp):
    1.  
    2. public static List<String> texture2DFrontNames;[/I]
    3.  
    4. void Start()
    5.     {
    6.      AddressablesTexture("CardFronts");
    7.     }
    8.  
    9.     public void AddressablesTexture(String labelName)
    10.     {
    11.         Addressables.LoadAssetAsync<IList<Texture2D>>(labelName).Completed += onTexture2DLabelLoaded;
    12.     }
    13.  
    14.     private void onTexture2DLabelLoaded(AsyncOperationHandle<IList<Texture2D>> handle)
    15.     {
    16.         if (handle.Status == AsyncOperationStatus.Succeeded)
    17.         {
    18.             if (handle.Result != null || handle.Result.Count > 0)
    19.             {
    20.                 Debug.Log("Result[1] was of type: " + handle.Result[1].GetType());
    21.                 foreach (Texture2D texture in handle.Result)
    22.                 {
    23.                     texture2DFrontNames.Add(texture.name);
    24.                     Debug.Log("Texture2D[" + texture.name + "] added");
    25.                 }
    26.             }
    27.             Addressables.Release<IList<Texture2D>>(handle);
    28.         }
    29.         else if (handle.Status == AsyncOperationStatus.Failed)
    30.         {
    31.             Debug.Log("List of Texture2D failed to load.");
    32.         }
    33.     }
    34.  
    My Successful attempt getting a list of Sprites assets using SpriteAtlas.

    Code (CSharp):
    1.  
    2.     private AsyncOperationHandle<SpriteAtlas> spriteAtlasHandle;
    3.     public static List<String> spriteAtlasFrontNames;
    4.  
    5.     void Start()
    6.     {
    7.         AddressablesSpriteAtlas("Faces");
    8.     }
    9.  
    10.     public void AddressablesSpriteAtlas(String spriteAtlasName)
    11.     {
    12.         Addressables.LoadAssetAsync<SpriteAtlas>(spriteAtlasName).Completed += OnSpriteAtlasLoaded;
    13.     }
    14.  
    15. private void OnSpriteAtlasLoaded(AsyncOperationHandle<SpriteAtlas> handle)
    16.     {
    17.         if (handle.Result == null)
    18.         {
    19.             Debug.LogError("no atlases");
    20.             return;
    21.         }
    22.         Debug.Log(handle.Result.name + " SpriteAtlas name");
    23.        
    24.         // Release previous stored addressable
    25.         if (spriteAtlasHandle.IsValid())
    26.         {
    27.             Debug.Log(spriteAtlasHandle.Result.name + " SpriteAtlas Release.");
    28.             Addressables.Release<SpriteAtlas>(spriteAtlasHandle);
    29.         }
    30.        
    31.         // hold AsyncOperationHandle for future release.
    32.         spriteAtlasHandle = handle;
    33.        
    34.         // get temp list of Sprits from our result
    35.         Sprite[] allSprites = new Sprite[handle.Result.spriteCount];
    36.         handle.Result.GetSprites(allSprites);
    37.        
    38.         // Create a list of sprite names for future KEY addressable lookups, of the same size
    39.         List<String> spriteNames = new List<string>(handle.Result.spriteCount);
    40.  
    41.         // Add each sprite name to our new list
    42.         String spriteName = "";
    43.         foreach (Sprite sp in allSprites)
    44.         {
    45.             // Strip "(Clone)" before adding to list. Will not need for future KEY lookups
    46.             spriteName = sp.name.Replace("(Clone)", "");
    47.             Debug.Log("SpriteAtlas[ " + spriteName + " ] added");
    48.             spriteNames.Add(spriteName);
    49.         }
    50.         spriteFrontNames = new List<string>(spriteNames);
    51.         Addressables.Release<SpriteAtlas>(spriteAtlasHandle);
    52.     }
    53.  
     
  5. Denis-535

    Denis-535

    Joined:
    Jun 26, 2022
    Posts:
    34
    Why don't you support "directoryKey"?
     
    wethings likes this.
  6. andymilsom

    andymilsom

    Unity Technologies

    Joined:
    Mar 2, 2016
    Posts:
    294
    There is no directory after the build, only Assets.
    When building a directory. It is essentially a container to say "All assets under this folder should be addressable". Each Asset when building for the lookup catalog. each have the key <directoryKey>/<relative asset path to directory>, and any labels that are set on the directory.
    In effect setting a label on the directory entry, is the same thing as a directory key would be.
     
    aydin_khp likes this.
  7. aydin_khp

    aydin_khp

    Joined:
    May 15, 2020
    Posts:
    29
    I have three questions regarding the thread and your answer:
    1. Since you said building an addressable folder means "All assets under this folder should be addressable", does it mean I could load each individual asset from under than folder directly with its key (i.e. "<directoryKey>/<relative asset path to directory>")?
    2. If the answer to 1 is positive, does it mean loading an individual asset from that folder won't cause the whole folder to load into memory? (I could not find the reference but for some reason I am under the impression that when accessing one asset in an addressable folder will cause the whole folder to load into memory)
    3. It seems that when making assets addressable using their containing folder, it is not possible to change the name part of the asset; For example my ScriptableObjects always have a ".asset" at their end which is not very user-friendly. Is there something can be done about this?
    Look forward to hearing from you.