Unity3D simple 2D animation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.