# Could you explain this calculation of tiling and offset?

I don't understand why we add +colNumber when we calculate the offset = Vector2 ((uIndex+colNumber) * size.x, which gives us x=3 for the index 13, which is the square located at the coordinates x:1, y:0 on the image (one square to the right, and "v" starts from the bottom). If the UV of the big image is on [0,1] for U and for V, why do we reach 3 for uIndex? And why don't we only do :

u = uIndex * sizeUV.x = 1 * 0.25 = 0.25
v = vIndex * sizeUV.y = 0 * 0.25 = 0


Here is the code from http://wiki.unity3d.com/index.php/Animating_Tiled_texture_-_Extended :

// 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);


• How did you get index 13? It's modulated by TotalCells, which is 4 (so it has to be 0,1,2 or 3). And I think that "+colNumber" is there just in case you want to start from some other column than 0. It's just starting offset. Apr 29, 2014 at 19:51
• @zacharmarz you're right, let's take 11, 11%16 = 11, then 11 % 4 (colCount) = 3, then (3+4) * 0.25 = 1.75 ... So the fourth blue head is at offsetX = 1.75 ? I don't understand.
– Paul
Apr 29, 2014 at 20:06
• @zacharmarz thanks, with totalCells being 4, it works indeed, thanks for the comment
– Paul
Apr 30, 2014 at 16:30

Here is how I understand it (I'm not saying that it's also how author of that script meant it):

Column count is total number of tiles in sheet. (the same for row)

Column number is number of a tile, where your animation begins (in picture in your question, it should be always 0, but you could also have for example 2 rows and 8 columns - 2 colors in one row).

Total cells is total number of tiles in animation.

Lets say you have the picture as is in your question, but you only want to animate last two faces (quick blinking) - these two: 8-) and :-)

So you will call function as this: SetSpriteAnimation(4, 4, 2, 2, 2, 10);

ColCount = 4

RowCount = 4

RowNumber = 2 (zero based and it's calculated from the top, because in the code author is inverting them)

ColNumber = 2 (zero based, so this is 3th column)

TotalCells = 2 (our animation has only 2 tiles)

FPS = 10 (doesn't matter)

If you try to put these values in script, index can be only 0 or 1 after line index = index % totalCells and OffsetX can be 0.5 or 0.75.

Lets say you want to animate blue faces from 2nd row (1 based indexing).

What is important: Your ColNumber has to be number of first tile of animation. TotalCells is total count of tiles in animation.

=============================