u3d 2d序列动画代码

using UnityEngine;

using System.Collections;

 

public class AniSprite : MonoBehaviour {

 

    private float myTime = 0;

    private bool isPlay = true;

    //anisprit.Sprite(4, 6, 0, 0, 20, 24, 1f);的意思是:改图是一个4列6行的动画图,图以坐标(0,0)开始,总帧数给定20帧,帧速率24,总用时1秒

    public bool Sprite(int columnSize,int rowSize, float colFrameStart, float rowFrameStart, int totalFrames, int framesPerSecond, float totalTime)

    {

        myTime += Time.deltaTime;

        if (totalTime != 0 && myTime > totalTime)

        {

            isPlay = false;

            myTime = 0;

            return isPlay;

        }

        int index = (int)(Time.time * framesPerSecond)% totalFrames;

 

        Vector2 size = new Vector2(1.0f/columnSize,1.0f/rowSize);

 

        int u = index % columnSize;

        int v = index / columnSize;

 

        Vector2 offset = new Vector2((u + colFrameStart) * size.x, (1.0f - size.y) - (v + rowFrameStart) * size.y);

        renderer.material.mainTextureScale = size;

        renderer.material.mainTextureOffset = offset;

        return true;

    }

}

 

你可能感兴趣的:(代码)