using UnityEngine;
using System.Collections;
public class Test1: MonoBehaviour {
public int uvAnimationTileX = 1;
public int uvAnimationTileY = 1;
public float framesPerSecond = 10.0f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
// Calculate index
int index = (int)(Time.time * framesPerSecond);
// repeat when exhausting all frames
index = index % (uvAnimationTileX * uvAnimationTileY);
// Size of every tile
Vector2 size = new Vector2 ((float)1.0 / uvAnimationTileX, (float)1.0 / uvAnimationTileY);
// split into horizontal and vertical index
int uIndex = (int) index % uvAnimationTileX;
int vIndex = (int) index / uvAnimationTileX;
// build offset
// v coordinate is the bottom of the image in opengl so we need to invert.
Vector2 offset = new Vector2 ((float)uIndex * size.x,(float)1.0 - size.y - vIndex * size.y);
renderer.material.SetTextureOffset ("_MainTex", offset);
renderer.material.SetTextureScale ("_MainTex", size);
}
}