x


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: alt text

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

more ▼

asked Feb 21 '12 at 12:45 AM

4illeen gravatar image

4illeen
63 33 35 38

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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);
}
more ▼

answered Feb 21 '12 at 09:47 AM

James Tima gravatar image

James Tima
36 1

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.

Feb 21 '12 at 10:25 AM POLYGAMe

"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

Feb 21 '12 at 12:00 PM 4illeen
(comments are locked)
10|3000 characters needed characters left

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_Tiledtexture-_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).

more ▼

answered Mar 25 at 04:22 PM

robertbu gravatar image

robertbu
14.6k 3 5 16

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1036
x354
x213

asked: Feb 21 '12 at 12:45 AM

Seen: 1269 times

Last Updated: Mar 25 at 07:15 PM