unity3d序列帧动画

序列帧动画的实质是uv信息的变化,理解Tiling(图片占得比例大小)和Offset(图片的偏移量)。默认左下角offset为0 0 Tiling为小图/大图

                   

代码如下:

public int rowNum ;

public int lineNum ;

 

public float iconwidth ;

public float iconheight ;

public int tileNum=6;

public float texWidth ;

public float texHeight ;

int achievementIndex=0;

float uWidth = 0;

float vHeight = 0;

// Use this for initialization

void Start () {

 

 

    uWidth=iconwidth/texWidth;

vHeight=iconheight/texHeight;

    InvokeRepeating("AnimationTexture",0,0.1f);

}

 

void AnimationTexture()

{   

if(achievementIndex>tileNum)

{

achievementIndex=0;

}

int rowIndex=achievementIndex/rowNum;

int lineIndex=achievementIndex%lineNum;

 

float uNums=lineIndex*uWidth;

float vNums=1-rowIndex*vHeight;

Vector2 size=new Vector2(uWidth,vHeight);

renderer.material.SetTextureOffset("_MainTex",new Vector2(uNums,vNums));

renderer.material.SetTextureScale("_MainTex",size);

achievementIndex++;

 

}