- Home /
How do you arrange sprites in order by name in Sprite atlas?
Hi
I want to stack each sprite from sprite atlas orderly so that whenever I want, I can get them by number later.
so sprites are named like this x_y. (eg. 0-1, 0-2, 0-3, 1-0, 1-1...)
x is item type and y is the number of the type.
below code is the process of getting type and number from sprite name
for(int i = 0; i < sprites.Length; ++i){
spriteName = sprites[i].name;
splitStandard = spriteName.IndexOf("(");
spriteName = spriteName.Remove(splitStandard);
typeAndNumberHolder = spriteName.Split('-');
StoreSprites(typeAndNumberHolder[0], typeAndNumberHolder[1], sprites[i]);
}
then I store it by type first with switch statement. but I noticed number of type doesn't stack orderly. Because when I pack atlas, it doesn't stack orderly by name in the first place (even if it is sorted well in folder). so stacked sprite arrays like this 3-2, 0-4, 1-2.... should I add some function to tidy up this or is there any way that you can pack sprites by name? I'm a newbie and I'm not sure this approach is right to show up items later in a store when it comes to performance for android game.
Answer by sarvsammatindia · Feb 23, 2020 at 03:48 AM
@migguragi2018 I assume you have sprites with animation and files are named with some numeric index. eg Sprite_1.png Sprite_2.png Sprite_3.png and while packing in sprite atlas the sequence is not in the correct order and if you try to create animation by sequentially swapping the texture over a sprite the animation is not looking in a correct order.
Here is how I do it
public SpriteAtlas loadedAtlas;
public Sprite[] loadedSprites = new Sprite[35];
loadedAtlast.GetSprites(loadedSprites);
spriteCount = loadedAtlast.spriteCount;
System.Array.Sort(loadedSprites, (a, b) => {
if (a == b)
{
return 0;
}
// nulls sort after anything else
else if (a == null)
{
return 1;
}
else if (b == null)
{
return -1;
}
else return a.name.CompareTo(b.name);
});
This code helps me sort the sprite by name in sprite atlas