- Home /
Texture2D Sprite
I'm about to do some 2D animations. Currently I use a plane with a texture on it, and change the texture via .Renderer.material.mainTexture = otherTexture;
.
I'm curious if it's possible to store all the sprites I need in one file and do something like this:
The big texture contains all the animation states/sprites but only part of it is seen on the Plane surface. Then we 'move' the texture inside the Plane by x pixels so other content is visible. I have a lot of animations and loading every one as a separate Texture2D doesn't seem like the best solution. Thanks in advance
Answer by James Tima · Feb 21, 2012 at 09:47 AM
use animation sheet like this
95link text95
and script is
//vars for the whole sheet
var colCount : int = 4;
var rowCount : int = 4;
//vars for animation
var rowNumber : int = 0; //Zero Indexed
var colNumber : int = 0; //Zero Indexed
var totalCells : int = 4;
var fps : int = 10;
var offset : Vector2; //Maybe this should be a private var
//Update
function Update ()
{
SetSpriteAnimation(colCount,rowCount,rowNumber,colNumber,totalCells,fps);
}
function SetSpriteAnimation(colCount : int,rowCount : int,rowNumber : int,colNumber : int,totalCells : int,fps : int)
{
// Calculate index
var index : int = Time.time * fps;
// Repeat when exhausting all cells
index = index % totalCells;
// Size of every cell
var size = Vector2 (1.0 / colCount, 1.0 / rowCount);
// split into horizontal and vertical index
var uIndex = index % colCount;
var vIndex = index / colCount;
// build offset
// v coordinate is the bottom of the image in opengl so we need to invert.
offset = Vector2 ((uIndex+colNumber) * size.x, (1.0 - size.y) - (vIndex+rowNumber) * size.y);
renderer.material.SetTextureOffset ("_MainTex", offset);
renderer.material.SetTextureScale ("_MainTex", size);
}
You could store each sprite frame as a separate texture in an array and count through with a for loop too. That way you can swap out frames if you need to make changes etc.
"You could store each sprite frame as a separate texture in an array and count through with a for loop too."
this is what I'm doing right now, but I feel like it's bad for performance due to huge amount of textures I have to load
Answer by robertbu · Mar 25, 2013 at 04:22 PM
If you are using the same material multiple places, then going to a "texture atlas" solution will likely reduce draw calls and potentially improve performance. There are a number of different scripts around that implement this functionality both on UnityAnswers and in the Wiki. They animate the UV coordinates of the material. Here is one link:
http://wiki.unity3d.com/index.php?title=Animating_Tiled_texture_-_Extended
Note third-party tools like EZGUI and probably NGUI automate the process of creating animated textures and building the atlas. That is, the frames exist as individual textures in the editor, but the tools build a single atlas/texture which is used at runtime.
To to be clear, in order to get a significant win out of this, you need to have multiple animations using the same animation sheet (and therefore they both would have exactly the same material).