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!
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")`
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): public static List<String> texture2DFrontNames;[/I] void Start() { AddressablesTexture("CardFronts"); } public void AddressablesTexture(String labelName) { Addressables.LoadAssetAsync<IList<Texture2D>>(labelName).Completed += onTexture2DLabelLoaded; } private void onTexture2DLabelLoaded(AsyncOperationHandle<IList<Texture2D>> handle) { if (handle.Status == AsyncOperationStatus.Succeeded) { if (handle.Result != null || handle.Result.Count > 0) { Debug.Log("Result[1] was of type: " + handle.Result[1].GetType()); foreach (Texture2D texture in handle.Result) { texture2DFrontNames.Add(texture.name); Debug.Log("Texture2D[" + texture.name + "] added"); } } Addressables.Release<IList<Texture2D>>(handle); } else if (handle.Status == AsyncOperationStatus.Failed) { Debug.Log("List of Texture2D failed to load."); } } My Successful attempt getting a list of Sprites assets using SpriteAtlas. Code (CSharp): private AsyncOperationHandle<SpriteAtlas> spriteAtlasHandle; public static List<String> spriteAtlasFrontNames; void Start() { AddressablesSpriteAtlas("Faces"); } public void AddressablesSpriteAtlas(String spriteAtlasName) { Addressables.LoadAssetAsync<SpriteAtlas>(spriteAtlasName).Completed += OnSpriteAtlasLoaded; } private void OnSpriteAtlasLoaded(AsyncOperationHandle<SpriteAtlas> handle) { if (handle.Result == null) { Debug.LogError("no atlases"); return; } Debug.Log(handle.Result.name + " SpriteAtlas name"); // Release previous stored addressable if (spriteAtlasHandle.IsValid()) { Debug.Log(spriteAtlasHandle.Result.name + " SpriteAtlas Release."); Addressables.Release<SpriteAtlas>(spriteAtlasHandle); } // hold AsyncOperationHandle for future release. spriteAtlasHandle = handle; // get temp list of Sprits from our result Sprite[] allSprites = new Sprite[handle.Result.spriteCount]; handle.Result.GetSprites(allSprites); // Create a list of sprite names for future KEY addressable lookups, of the same size List<String> spriteNames = new List<string>(handle.Result.spriteCount); // Add each sprite name to our new list String spriteName = ""; foreach (Sprite sp in allSprites) { // Strip "(Clone)" before adding to list. Will not need for future KEY lookups spriteName = sp.name.Replace("(Clone)", ""); Debug.Log("SpriteAtlas[ " + spriteName + " ] added"); spriteNames.Add(spriteName); } spriteFrontNames = new List<string>(spriteNames); Addressables.Release<SpriteAtlas>(spriteAtlasHandle); }
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.
I have three questions regarding the thread and your answer: 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>")? 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) 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.