- Home /
Load a SpriteAtlas, or access SpriteAtlas of a Sprite
I have a SpriteManager that has several lists of sprites. I am using it to change sprite textures during runtime.
I have a folder with all the sprite atlases in it. Because all the "tree" sprites have the same parts, they use the same layout in their atlases. So [oak] has an [oak SpriteAtlas] with a [Limb] sprite, and [pine] has an [pine SpriteAtlas] with a [Limb] sprite.
When I load the sprites, I get [Limb] and [Limb] but do not know how to tell which atlas they came from.
So I want a way to load the sprite atlas as a whole, then load the sprites from that atlas. I do not want to have to specify the name of each tree, as there are too many.
Here is the code currently:
public IEnumerator LoadSprites()
{
Debug.Log("Loading sprites");
//first, we can find all the sprites
allSpriteObjects = Resources.LoadAll("Sprites", typeof(Sprite));
//then we need a list for each atlas
allSpriteAtlases = new List<Sprite[]>();
//now we need to put each sprite in the correct atlas entry
foreach (Object o in allSpriteObjects)
{
Debug.Log("Loaded " + o.name);
//HERE: How do I know what atlas this sprite was pulled from?
yield return new WaitForSeconds(0.1f);
}
}
My end goal is to have a List<List<Sprite>
, or a List<Sprite[]>
or a List<SpriteAtlas>
, so that I can find the [Limb Sprite] of [pine SpriteAtlas] from the manager.
Answer by FedericoDeBona · Jul 03, 2019 at 01:22 PM
Hi,
I putted toghether a fast solution, tell me if it works for you. You also need the following namespaces:
-System.Collections;
-UnityEngine;
-UnityEngine.U2D;
public class SpriteLoader : MonoBehaviour
{
Hashtable spriteTable;
public void Start()
{
spriteTable = GetSprites();
//use this to loop through the elements
foreach (DictionaryEntry element in spriteTable)
{
Sprite[] sprites = (Sprite[])element.Value;
foreach (var sprite in sprites)
Debug.Log("Sprite in '" + element.Key + "': " + sprite.name);
}
}
public Hashtable GetSprites()
{
//Insthed of list of list, i used an Hashtable, with key as sprite atlas name
//and value as array of the sprites of the curren sprite atlas
Hashtable table = new Hashtable();
//Loading sprite atlases
SpriteAtlas[] allSpriteAtlases = Resources.LoadAll<SpriteAtlas>("Sprites");
foreach (var element in allSpriteAtlases)
{
Sprite[] sprites = new Sprite[element.spriteCount];
//Clone the sprites of the curren sprite atlas
//in the 'sprites' array
element.GetSprites(sprites);
/* this is the structure of the hashtable:
* key->string(atlas name) value->Sprite[](array of the sprite in the current atlas)
*
*
* To access values:
* table["atlas name"] --> will return the array of sprites of the atlas selected
* so, for instance if the atlas is "hosue"
* Sprite[] houseSprites = table["house"];
*/
table.Add(element.name, sprites);
}
return table;
}
}
Your answer
Follow this Question
Related Questions
How to access sliced sprites? 0 Answers
convert Resources.LoadAll("RPGOBJ", GameObject) to gameobject array? 0 Answers
Loading a large number of image resources 0 Answers
Increase Draw Calls at using AssetBundles 0 Answers
What's the best way to line up sprites between animation states when they aren't symmetrical? 1 Answer