- Home /
Equivelant of GUI.DrawSprite() ?
Here's what I want to have happen: I want to use Resource.LoadAll to grab a spritesheet with six cells, and put them into an array, with each cell as its own position in the array, so Array[0] is frame 1, Array[1] is frame 2, so on. Then, I want to be able to draw those images directly to the screen, like with GUI.DrawTexture.
Here's what I have to so far:
Texture2D[] Textures = new Texture2D[6];
Textures = Resources.LoadAll<Texture2D>("filepath");
GUI.DrawTexture (new Rect(x,y,w,l), Textures[0]);
The problem is that the spritesheet is a Texture2D, but I can't figure out how to separate it into its individual frames. I've also heard using Textures = Resources.LoadAll("filepath");
would get the individual sprite frames, but then I can't draw them with GUI.DrawTexture.
Is there someway to either draw sprites, or to import each sprite frame as Texture2Ds?
Here is an approach that may or may not work depending on how regular the presentation of each frame is within the sheet. You can use GUI.DrawTextureWithTexCoords() to select a section of the sprite sheet to display.
Using GUI.DrawTextureWithTexCoords() does not work. Tested repeatedly. Doing GUI.GUI.DrawTexture() using the sprites texture does work fine through, but obviously this does not work well with spritesheets.
Using GUI.DrawTextureWithTexCoords() works fine.
Sprite s = sprites[0];
Texture t = s.texture;
Rect tr = s.textureRect;
Rect r = new Rect(tr.x / t.width, tr.y / t.height, tr.width / t.width, tr.height / t.height );
GUI.DrawTextureWithTexCoords(new Rect(0, 0, tr.width, tr.height), t, r);