- Home /
Load UnityEngine.sprite from code.
Trying to understand the new sprite system. All is fine, you can easily create and upload sprites from editor and from the code:
// from texture
SpriteRenderer spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
spriteRenderer.sprite = UnityEngine.Sprite.Create(
Resources.Load<Texture2D>("Textures/Interface/pause_button"),
new Rect(0,0,256,256), new Vector2(0.5f, 0.5f), pixelToUnit);
// from sprite in resource
SpriteRenderer spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
spriteRenderer.sprite = Resources.Load<UnityEngine.Sprite>("Textures/Interface/button");
There is a possibility to create a collection of sprites from atlases (via sprite editor). But if you download sprite by texture name, only the first sprite in the collection loading. Is it possible to download the sprites from the atlas by Resource.Load or another function?
Answer by luciano182 · Mar 28, 2015 at 04:58 PM
Here's my solution, using a SpriteRepository as Cache.
public class SpriteRepository
{
public static Dictionary<string, Sprite[]> dict = new Dictionary<string, Sprite[]>();
public static Sprite GetSpriteFromSheet(string name)
{
string query = @"((\w|-|_|\s)+/)*((\w|-|_|\s)+)(_)(\d+)$";
Match match = Regex.Match(name, query);
string sprName = match.Groups[3].Value;
string sprPath = name.Remove(name.LastIndexOf('_'));
int sprIndex = int.Parse(match.Groups[6].Value);
Sprite[] group;
if (!dict.TryGetValue(name, out group))
{
dict[name] = Resources.LoadAll<Sprite>(sprPath);
}
return dict[name][sprIndex];
}
}
Hi luciano182, thanks for your post! Your answer seems really elegant and it's the only solution i found so far without using the ressources ... unfortunately i'm (not yet) a c# pro and so i have no clue how to use your solution :( how do i pass the sprites ( spritesheet and the names of the sprites in o.0
would be awesome if you could explain this to me :) Thanks in advance !
The query should be as follows:
string query = @"((\w|-|_|\s)+\/)*((\w|-|_|\s)+)(_)(\d+)$";
Note that this code will work if you pass to it a string in the form "PathToAtlas/AtlasName_N" where N is the index of the texture you want in AtlasName.
@PBeast, this solution uses the same Resources folder as the others, look in the inner brackets!
ah ! okay sorry ! $$anonymous$$issed the inner brackets somehow -.-' Sorry for that :X Thank you very much for your response !!!
Answer by Mefistofel · Nov 20, 2013 at 06:53 AM
Here people also came to the same:
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")];
Answer by golemsmk · Nov 29, 2013 at 10:25 AM
You should use LoadAll() function. For example, you have a "Button" sprite that inside Image folder has been divided into smaller sprites, and they are "Button_1", "Button_2",... then you do this:
Object[] myTextures = Resources.LoadAll<Sprite>("Image/Button");
You must use Object[], with me, when I tried anything else, I got a error message. Then you can assign myTextures[index] to anything you want, like this:
spriteRenderer.sprite = myTextures[index];
Goodluck, and have fun.
It needs to be:
Sprite[] myTextures = Resources.LoadAll("Image/Button");
not Object[]. It works like a charm then!
You are using C# or Java? With me, this code work on Java.
Answer by MillZzie · Oct 21, 2014 at 01:43 PM
I keep using this and instead of applying the new sprite it just removes the current one.
gameObject.transform.FindChild("Cat_Head").GetComponent().sprite = Resources.Load("Sprites/RedSprite/Head");
Answer by gitstar · Mar 02, 2015 at 02:12 PM
I used the following the way..it`s the usefully code. thanks.
void SetSprite(UI2DSprite uiSprite, Sprite[] sprites, string strKey)
{
foreach (Sprite stexture in sprites)
{
if (stexture.name == strKey)
{
uiSprite.sprite2D = stexture;
break;
}
}
}
///////////
// sprite image load change.
UI2DSprite[] uiSprites = tmpObject.GetComponentsInChildren<UI2DSprite>();
Sprite[] sprites = Resources.LoadAll<Sprite>("Textures");
string resName = "icon_favorite_2";
foreach (UI2DSprite uiSprite in uiSprites)
{
if(uiSprite.name = "icon")
SetSprite(uiSprite, sprites , resName);
}