- Home /
loading sprite from atlas in script
there are questions already on this, but as far as i can see none of them have an answer that works..
how can a sprite packed in an atlas (packed by giving a packing tag to the sprites) be loaded in code?
we can't use Resources.Load, because the sprites can't be in a resources folder (they won't pack into an atlas if they're in a resources folder).
we can't use AssetDatabase, because it only works when running in the editor.
the idea is to avoid prefabs, and load the sprite directly, because i need to do a lot of stuff dynamically, and also don't want to have to create prefabs for hundreds of sprites.
any way to do this?
Can't be done. Unless something has changed drastically, you have to have a reference to your sprites somewhere set up from in the editor.
Ok thanks. Seems surprising! $$anonymous$$aybe i should somehow use the editor to add a single reference to each sprite in my singleton main scene, then i can use those references throughout the rest of the game? Or can it just be one of the sprites from the atlas, which can somehow give me access to the rest of the atlas?
just to bump this up - is there really no way to load a sprite from an atlas in a script?
does anyone know of a way to do it manually, e.g. getting all the uv rect data and manually cutting the sprites out.. or does that defeat the point of using atlases at all?
for the moment i've written an editor script to go through every sprite in the asset database, and create and save a prefab for it, so i've got a few thousand prefabs. then in the game i can load the prefab, take the sprite out of it, and then use that the way i need to.
it's an incredibly clunky workflow - every time i add new sprites to the game i have to run a few different editor scripts, wait for importing to happen, etc etc. surely i'm missing something obvious...
Answer by DanSuperGP · Jan 23, 2015 at 09:44 PM
Rather than making prefabs, you can make a game object with a public list of sprites... and then drag all your sprites into that list...
Then, on start... put all the sprites into a public dictionary by name and clear the list...
Then you can load them by name from the dictionary.
that's a good idea, i think this will be easier than running my auto prefab maker script all the time...
...but i still think there should be a way of just loading a sprite that's packed into an atlas :)
Yeah, I agree, there definitely SHOULD be a way to load a sprite from the atlas by name.
For that matter, there should be a way to tell Unity to pack your atlas into an asset bundle...
Unfortunately neither are true.
quick update: i'm now thinking this isn't possible, because as long as you have the array/dictionary of sprites around, then that spritesheet will be in memory, so there's no way to unload it when you no longer need it (if you remove the references to sprites in the atlas, in order to get it to unload, then you've got no way to get them back again later). so it has to be prefabs it seems
$$anonymous$$ake a prefab of the game object with the references to all the sprites. Load it when you need it, destroy it when you don't.
Answer by Bentley · Jan 22, 2015 at 09:32 PM
Maybe you're looking for something like this?
public Dictionary<string, Sprite> dictSprites = new Dictionary<string, Sprite>();
void Start()
{
Sprite[] sprites = Resources.LoadAll<Sprite>("SpriteSheet");
foreach (Sprite sprite in sprites)
{
dictSprites.Add(sprite.name, sprite)
}
}
GetComponent<SpriteRenderer>().sprite = dictSprites[name];
You could obviously also leave it in just an array and access through indices, although if there are 10+ sprites per sheet counting an index can get irritating.
Hope this helps,
Bentley
looks potentially like the answer... i'm not sure what "SpriteSheet" refers to though - it has to be a folder in Resources doesn't it? where does the actual atlas/spritesheet go after sprite packer does its thing?
When you import a file with sprites in it and then select "multiple" and slice up your sprites, then when you look at that file in the project files, it will have child files. "SpriteSheet" refers to the path from the resources folder to that parent file.
In this case "SpriteSheet" would be "Weapons/Weapons". Then the array will be populated with "Weapons_0" through "Weapons_11". While you're slicing your sprites you can, however change their names to a specific weapon name ins$$anonymous$$d of "Weapon_X" and so that if you decide to put them in a dictionary you can just say dictSprites["A$$anonymous$$47"] ins$$anonymous$$d of sprites[6]. Again just a matter of preference.
ah i see, i think this is a different workflow - i've got each sprite as an individual image file, and then letting unity pack them into an atlas using the sprite packer.
the individual image files are outside of resources folder, because otherwise the sprite packer won't pack them
You are God to me! At least for this week! This snippet solved a problem I was having! Now I can get an inventory system going!
Answer by SuperUltraHyper · Sep 24, 2015 at 07:13 AM
Without some convoluted method it isn't possible but you can up vote it!! http://feedback.unity3d.com/suggestions/allow-access-to-sprite-packer-atlas-to-retrieve-sprites-by-name
Answer by anagh · Jun 30, 2017 at 12:33 PM
I wrote this code I have 42 png avatars But when i run new Game Object gets initialized 42 times Please help!
public GameObject gridGameObject; public Dictionary dictSprites; void Awake() { dictSprites = new Dictionary (); } // Use this for initialization void Start () { Sprite[] sprites = Resources.LoadAll("Avatars");
foreach (Sprite sprite in sprites)
{
dictSprites.Add(sprite.name, sprite);
NGUITools.AddChild(gridGameObject, sprite);
}
}
Your answer
Follow this Question
Related Questions
How can I use atlas sprite as texture for a material? 2 Answers
Sprite.Create is not working 1 Answer
Unity3d 4.3 get sprite from atlas 4 Answers
Overlay texture on sprite 0 Answers
Replace a sprite by another in every game object using it 1 Answer