- Home /
Load texture from multiple sprite
Hi!
I have a multiple sprite.
I'm trying to load each sprite separately via script.
void OnLevelWasLoaded(int level)
{
Sprite[] sprites = Resources.LoadAll<Sprite>(@"Graphics/levelsSelection");
double step = 1.0 / sprites.Length;
double offset = step;
foreach(var sprite in sprites)
{
var button = new GameObject("Button");
button.AddComponent(typeof(GUITexture));
button.transform.position = new Vector3(0.5f, (float)offset);
button.guiTexture.texture = sprite.texture;
offset += step;
}
}
But that's what I got as a result.
What I do wrong? Why sprites turned out not separated? How to fix it?
Thanks to any one who can help me!
P.S. Please sorry for possible mistakes - english is not my native language.
Answer by ZZZ77 · Feb 24, 2014 at 08:24 PM
OK. I solved this problem. The hint - how to fix it - I found here:
http://answers.unity3d.com/questions/579791/how-do-i-access-the-individual-frames-of-a-sprite.html
In my situation the correct code is:
void OnLevelWasLoaded(int level)
{
Sprite[] sprites = Resources.LoadAll<Sprite>(@"Graphics/levelsSelection");
double step = 1.0 / sprites.Length;
double offset = step;
foreach(var sprite in sprites)
{
var button = new GameObject("Button");
button.AddComponent(typeof(SpriteRenderer));
button.transform.position = new Vector3(0.5f, (float)offset);
var renderer = button.GetComponent<SpriteRenderer>();
renderer.sprite = sprite;
offset += step;
}
}
Your answer
Follow this Question
Related Questions
Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Manually changing between sprites in a spritesheet 0 Answers
Sprites in spritesheet have strange edges in the sprite editor but not in art software 2 Answers
Request Help: Sprite Editor not Automatically Slicing Sprite Sheet 0 Answers