- Home /
How to load sprite at runtime from atlas?
Good day to all.
At the moment I am creating the 2d game with quite a lot of art. It is more than 1k sprites there. It should be possible to load any combination of pictures at runtime. I'd already implemented all the logic, but have a trouble that is annoying me.
In the current version all the sprites are inside Resources folder and are loaded using
Sprite sp = Resources.Load("name", typeof(Sprite)) as Sprite;
It works fine.
There are almost no POT-size images, so without packing to atlases there is a huge memory overhead. iOS build takes about 500 Mb on device. That's why I want to pack my sprites to atlases. But there is a couple of troubles with it.
First of all, after enabling, unity's Sprite packer do not wanna pack my images to atlases (I specified tags for my sprites). There is nothing happens after clicking "Pack"/"Repack". I thought that it is because location of my sprites (Resources folder), but even after moving them to different folder, Sprite packer works strange. Anyway, at the moment I created test project, copied all the pictures there and packed them (still cannot pack them in the original project).
The next trouble, that I cannot find how to load sprites from created atlases at runtime without assigning them to variables in my scripts.
I cannot find enough info in the official reference and on the forums, as this is quite a new feature, but hope that someone here can give me needed answers.
Have a nice day and thanks for reading =)
Hi, did you find out how to do this? I'm having the same problem; I don't know how to access the sprites from the atlas at runtime...
Bumping this question. I'm in the exact same situation: lots of non-POT art, developing for iOS, would like to use atlases but don't know how to reference them at runtime.
I guess my workaround will be to create a gameobject that has either an array or dictionary of sprites. Then, have an editor script that automatically populates the dictionary with all of the sprites in my project. The serious downside of this is that the sprites will always be loaded when the scene starts, rather than a controlled Resources.Load
Yeah, sorry. Didn't post that I implemented the same logic as BJO said. And yeah, I actually don't like it =\ But don't know any other solution.
Answer by pavlito · Oct 23, 2015 at 11:29 AM
I create a SpriteCollection class
using UnityEngine;
using System.Collections.Generic;
public class SpriteCollection : MonoBehaviour {
public List<Sprite> sprites = new List<Sprite>();
public Sprite GetSprite(int index)
{
if (index >= 0 && index < sprites.Count)
return sprites[index];
return null;
}
public Sprite GetSprite(string spriteName)
{
foreach(Sprite sprite in sprites)
{
if (sprite.name == spriteName)
return sprite;
}
return null;
}
}
attach it to a game object and save it as prefab. Assign desired sprites to each prefab and then Resource.Load(prefab). Hope this helps!
Answer by ForceMagic · May 21, 2015 at 06:15 AM
This is currently not possible, unfortunately.
You might want to help and vote on this feedback issue "Allow access to sprite packer atlas to retrieve sprites by name"
Other related post/answers
Your answer
![](https://koobas.hobune.stream/wayback/20220613152636im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Sprite Atlas not working 0 Answers
Sprite Packer Causes Unity to Hang on 5.5.0f3 0 Answers
sprite power of two and sprite packer 0 Answers
Why is atlas and individual sprites included in package? 0 Answers