Using UnityEngine; using System. collections; public class AniSprite: MonoBehaviour {public int rowCount = 4; // The number of rows in the frame image in the image public int colCount = 4; // public int rowStart = 0 for the number of frames in the image; // The row subscript that starts playing public int colStart = 0; // The subscript public int totalCells = 4; // The total number of frames public int fps = 10; // playback speed // Update void Update () {SetSpriteAnimation (colCount, rowCount, rowStart, colStart, totalCells, fps);} // SetSpriteAnimation void SetSpriteAnimation (int colCount, int rowCount, int rowStart, int colStart, int totalCells, int fps) {// because Time. time is the time when the game runs, so index =, 3, 4... int index = (int) (Time. time * fps); index = index % totalCells; float sizeX = 1.0f/colCount; float sizeY = 1.0f/rowCount; Vector2 size = new Vector2 (sizeX, sizeY ); var uIndex = index % colCount; var vIndex = index/colCount; float offsetX = (uIndex + colStart) * size. x; // The V axis is located at the bottom of the image. Therefore, V must flip the float offsetY = (1.0f-size. y)-(vIndex + rowStart) * size. y; Vector2 offset = new Vector2 (offsetX, offsetY); renderer. material. setTextureOffset ("_ MainTex", offset); renderer. material. setTextureScale ("_ MainTex", size );}}