unity3d 帧动画主要介绍序列帧动画,下面上图和源代码:

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

代码如下:

  1. public int rowNum ;

  2. public int lineNum ;

  3.  

  4. public float iconwidth ;

  5. public float iconheight ;

  6. public int tileNum=6;

  7. public float texWidth ;

  8. public float texHeight ;

  9. int achievementIndex=0;

  10. float uWidth = 0;

  11. float vHeight = 0;

  12. // Use this for initialization

  13. void Start () {

  14.  

  15.  

  16.     uWidth=iconwidth/texWidth;

  17. vHeight=iconheight/texHeight;

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

  19. }

  20.  

  21. void AnimationTexture()

  22. {   

  23. if(achievementIndex>tileNum)

  24. {

  25. achievementIndex=0;

  26. }

  27. int rowIndex=achievementIndex/rowNum;

  28. int lineIndex=achievementIndex%lineNum;

  29.  

  30. float uNums=lineIndex*uWidth;

  31. float vNums=1-rowIndex*vHeight;

  32. Vector2 size=new Vector2(uWidth,vHeight);

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

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

  35. achievementIndex++;

  36.  

  37. }