- Home /
Loading a Sprite (Unity 4.3) in resource folder and setting it in sprite renderer
Hey Guys,
I have a SpriteSheet which I imported in Unity so I have a basic Texture2D asset which is separated in multiple sprites which are named like (sprite_0,sprite_1, etc.) Im trying to set the sprite of my SpriteRenderer in code but I want to be able to load a sprite dynamically, not have to link it in the script directly. So I have a Resources and a Textures folder within it. I' trying to get the sprite by doing Sprite s = Resources.Load("Textures/sprite_0") as Sprite. But it is always null. In fact, when I want to load the "Base" sprite named "Sprite", it recognize it as a Texture2D type, but when I want to load a specific sprite in my texture like sprite_0, it just can't find it at all. I have my texture type set to Sprite, and Mode to multiple. Does anybody have an idea ?. Thanks a lot for your time :)
Claude
Answer by Dragon_Mao · Nov 15, 2013 at 12:34 PM
Sprite[] sprites = Resources.LoadAll<Sprite>("Textures");
Only by index, I suppose. It's not a problem, because name looks like "textureName_spriteIndex".
Unfortunately in my case, the names do not contain index. I did so:
Sprite[] textures = Resources.LoadAll<Sprite>("Textures");
string[] names = new string[textures.Length];
for(int ii=0; ii< names.Length; ii++) {
names[ii] = textures[ii].name;
}
Sprite sprite = textures[Array.IndexOf(names, "textureName")];
But maybe there's a more elegant way?
Worked like a charm ! Thanks a lot everybody for your time :)
@Siflou: Hi, could you post the final code, so we could see how did you solve it, because it's not working for me.. thanks.
Answer by Shawn-Halwes · Dec 09, 2013 at 09:41 PM
For multi-sprite textures I found this to work:
UnityEngine.Object[] allSprites = AssetDatabase.LoadAllAssetRepresentationsAtPath("Assets/Textures/" + SpriteTextureName + TextureExtension);
This is pretty great! Saves you the trouble of putting stuff in a Resources folder, too! (I'm neurotic, and feel sooooo much better if I can keep my folders labeled by type)
I know this is an old answer, but you can have your resources folders labelled by type. You can have multiple resources folders, each for each type you want. So you can have Prefabs\Resources, Sprites\Resources, Audio\Resources, etc.
So, bad news, actually -- this only works in the Editor. Turns out AssetDatabase is name-spaced to UnityEditor, and so if you include this elsewhere in your code it won't build.
Correct. The implication was that you use custom editor code to hook up the references ahead of time.
Answer by lxem · Dec 16, 2013 at 09:27 AM
You can also define sprites in advance.
public Sprite[] sprites;
Then you can bind sprites in Unity Editor, and set SpriteRenderer.sprite in runtime like this.
SpriteRenderer sr = GetComponent<SpriteRenderer>();
sr.sprite = this.sprites[Random.Range(0, this.sprites.GetLength(0) - 1)];
For some situation this is enough.
I hope this can help.
Regards.
Answer by fabien0102 · Jun 15, 2016 at 01:37 PM
You can use System.Linq
to find easily sprite into texture ;)
For example:
private Sprite GetLevelSprite(string textureName)
{
Sprite[] textures = Resources.LoadAll<Sprite>("MySpriteSheet");
return textures.Where(t => t.name == textureName).First<Sprite>();
}
GetLevelSprite("plop")
will return the sprite with /Assets/Resources/MySpriteSheet/plop
path ;)
Answer by ArchAngelus · Feb 12, 2014 at 05:43 PM
If you want to access a particular sprite by a more meaningful name like "Rocket" or "Zombie", I would create Prefabs out of the sprites and load them up with Resources.Load().
Your answer
Follow this Question
Related Questions
TexturePacker 0 Answers
Image distorts when created in C# 3 Answers
Draw tilemap with Texture2D.SetPixels: Performance and Memory? 0 Answers
sprite border 0 Answers
Change the color of a black sprite?,How can i change the color of a black sprite? 1 Answer