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(“MyAddressableLabel”) or Sub-Assets using Addressables.LoadAsset<IList>(“MyAddressableLabel”)
My failed attempt getting a list of Texture2D assets using the label I created.
UnityEngine.ResourceManagement.ResourceManager+CompletedOperation1[System.Collections.Generic.IList1[UnityEngine.Texture2D]], result=‘’, status=‘Failed’: Exception of type ‘UnityEngine.AddressableAssets.InvalidKeyException’ was thrown., Key=CardFronts, Type=UnityEngine.Texture2D
```csharp
*public static List 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._ *csharp
- private AsyncOperationHandle spriteAtlasHandle;
public static List spriteAtlasFrontNames;
void Start()
{
AddressablesSpriteAtlas("Faces");
}
public void AddressablesSpriteAtlas(String spriteAtlasName)
{
Addressables.LoadAssetAsync<SpriteAtlas>(spriteAtlasName).Completed += OnSpriteAtlasLoaded;
}
private void OnSpriteAtlasLoaded(AsyncOperationHandle 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);
}*
```