- Home /
Instantiate Sprite from Texture Atlas in code?
So I have 100 items, they are images, they all have the same Packing Tag. I also have a menu and when you select something from it, it will set the Sprite component on a button. Now I can do this:
button.GetComponent().sprite = Resources.Load("images/items/imageName");
But I think that loads the source image rather than creating an instance from the texture atlas. Or does it? I'm not sure. Loading individual images will end up in loads of draw calls so I want to avoid that.
Does anyone know the correct way to set the sprite using just code?
Answer by hexagonius · Feb 25, 2015 at 04:00 PM
The packing tag alone doesn't pack your sprites into an atlas. Read about SpritePacker in Unity manuals. Sprites located in resources will not be packed because it would generate duplicates(single sprites and the atlas). For loading via code you can't access that generated atlas directly. Instead create a GameObject holding references to all sprites and use it as your sprite manager.
Your answer
![](https://koobas.hobune.stream/wayback/20220613184256im_/https://answers.unity.com/themes/thub/images/avi.jpg)