Hi there,
I’ve packed my Sprites into a sprite atlas but I’m confusing in the way of load a Sprite into scene.
In my mind, I have 2 way to load:
-
1st way: Store all sprite in a SerializeField in Inspector on hierarchy and call it when I need to use that Sprite.

-
2nd way: Not to store in hierarchy, just call SpriteAtlas.GetSprite to get the Sprite.
I prefer the 1st way, when I have a big amount of sprites, I can use the SerializeField array to store in Inspector, then call by ID for use, it is the easiest way in my mind. The 2nd way need to use name of Sprite to get Sprite, it is inconvenient for me.
My question is:
- Which is the best way for performance?
- If I can use Sprite atlas follow my 1st way and have no damage to performance, is it a redundant method “SpriteAtlas.GetSprite”?
Thank you!
There are different usescases for the spriteAtlas.
One approach is to treat sprite Atlas purely as an optimization tool. In that case the approach 1 is perfectly fine. None of your code needs to know whether sprite is in an atlas or not. You can create the whole game, and add sprite atlases later without changing anything in the rest of your game (mostly) .
There can also be situations where for certain effects you need stuff to be in single texture and you thus use a sprite atlas to achieve that. In some (but not all) of those cases it might be more convenient to use SpriteAtlas.GetSprite api.
SpriteAtlas.GetSprite can also be convenient if you need to reference from file which can’t directly store a reference to Unity asset. For example you might have a .json or other text based files for describing the content of game. Again use of SpriteAtlas.GetSprite isn’t the only way it can be solved, you could also create your own string->sprite mapping and store it in a serialized object, or use addressable assets or many other ways.
Third potentially most important reason for having SpriteAtlas.GetSprite API (also one many users will never touch) is that unity API isn’t just for runtime. For editor tooling it can be useful to know which exact sprites are within sprite atlas and similar stuff. Also editor API is not just for you and asset store creators (but if you are making a larger game definitely consider learning about it to improve your workflow) but it is also partially for first party Unity packages and interactions between them. While a large portion of unity code is either written in C++ or using internal C# APIs, quite a few unity modules are using more or less the same public C# API available to everyone. Which means that it could have been implemented you or some asset store creator, or that it’s possible for someone to create an alternative implementation which works as well as one provided by Unity.
One more consideration is how tightly memory constrained your game is. For a small game made by one or two people especially with pixelart it is possible the total amount of sprites take barely any memory so it isn’t (doesn’t mean it’s impossible to waste it all if you are ignorant about it and don’t pay attention). But for more complex games with large levels pushing the limits or when targeting more constrained devices available memory might be an issue. Such games might choose to implement a somewhat more manual memory management strategy on top of unity to have a better control of what gets used and how. Again I am not suggesting that use of one or another API will magically improve things or that it might be best tool available for those specific examples (in most cases it wont), but that it could be used to create more complex solutions which might not be obvious to beginners. Also you need it hit a specific combination of project type, team size and skills for it to make sense spending time making such things.
- Which is the best way for performance?
Rule 1) of any performance considerations is do profiling and measure the impact on your actual game.
Although I highly doubt that approach 1 will be slower than 2 (unless you hit a very specific edge case, with there being equally many ore more edge cases in opposite direction). A general rule of thumb of mine is avoid any APIs that involve querying stuff by strings if there are alternatives that allow you to reference things directly. (But don’t overdo it if there are no suitable alternatives, “avoiding” strings by tripling the code and manually calling the same methods which string methods calls internally doesn’t help) Doing a lookup by name has some nonzero cost and is simply error prone. Although unless you are doing it tens of thousands of times every frame, it is very likely that lookup by name won’t be a problem anyway.
2 Likes
karliss_coldwild
Clearly and helpful. Thank you for your time!
Just more few minutes, my current project does not use addressable, I currently use [SerializeField] List to store all icons and call by Id to get Sprite (see attach image).
Could you please share with me the way (that can avoid string querying) I can build my Resource Helper to get Sprite with SpriteAtlas.GetSprite.
In my mind, code is like this:
private string[] spriteNameList =
{
"icon0",
"icon1",
"icon2",
"icon3",
"icon4",
.......
};
private void GetSpriteFromAtlas(int iconId)
{
atlas.GetSprite(spriteNameList[iconId]);
}
But this is hard code with the name list, when the sprite name is changed, there will be error. Is there any way better?
Thanks for your help!
Why are you so obsessed with the idea of GetSprite? You already know a way how to do it without it by just having List why aren’t you just using that?
In case there is misunderstanding about how sprite atlas in unity work. You don’t need to query sprite from sprite atlas for sprite altas to have an effect. If you have a reference to sprite like in your case of List and those Sprites are added to atlas Unity will automatically take sprites from atlas texture.
1 Like
Yeh, I’ve already know Sprite atlas worked but as my Title of this post, I want to find some useful of GetSprite, I also just want to know another way just for more knowledge, as you say, the approach 2 may be faster, then I think there is another way to try better. If there will be no way better, in my case, I only have 1 way to get Sprite. It’s also ok.