- Home /
Sprite sheets in Resource folder
Historically, when I have a sprite sheet in my Sprites folder (not in Resources), and I want to swap in sprites at runtime, I have always dragged the sprites one at a time into my scene, then created prefabs in Resources for every single sprite. Cumbersome and feels wrong.
However, is there a way to just put my entire Sprite sheet in Resources, and use them via script without creating individual sprites for every sliced image in the sheet?
Example: I have a figure that I want to change the head sprite for. I have a sprite sheet in Resources that is sliced up into 20 different head images. How can I select out head_12 from the sprite sheet and use it for my head graphic? Javascript if possible please.
Answer by DiegoSLTS · May 30, 2016 at 12:06 AM
Once you imported your texture as multiple sprites you can load all sprites "inside" that texture using:
Sprite[] sprites = Resources.LoadAll<Sprite>(pathToTexture);
Then you can get any sprite on that array and check it's .name to select the one you want.
I've searched a lot and can't find a way to load only one sprite by it's name if it's inside a texture.
You can write:
Sprite sprite = Resources.Load<Sprite>(pathToTexture);
And you'll get the first sprite in the list.
Answer by TranceDreams · May 02, 2020 at 12:32 AM
You can also make a static function so whenever you want a specific sprite from any script, you can call the function
for example, put this in (any) fooScript.js
static function GetSprite(path : String, spr : String){
sp = Resources.LoadAll.<Sprite>(path);
for(x in sp)
if(x.name.Contains(spr))
return x;
}
now to get a sprite whenever you want, do this
fooScript.GetSprite("spriteFolder/spriteSheetFoo", "fooSPrite");
Your answer
Follow this Question
Related Questions
2d game questions 0 Answers
How do I call the sprite auto-slicer from a script 0 Answers
How to create animated "sprites" in a 3D environment 1 Answer
Need help on skeletal Animation 0 Answers