目标: 完成小鸟在地面上眨眼睛的动画
1. 新建一个Plane来制作小鸟动画,将包含3帧小鸟动画的图片贴到Plane上,然后设置下Plane的Transform属性。
为了让这个Plane只显示一个小鸟,将Tiling的x值改为0.33.
2. 下面写个脚本来实现小鸟序列帧动画。
- #pragma strict
- var rowCount : int = 1;
- var colCount : int = 3;
- var fps : int = 2;
- var isLoop : boolean = true;
- private var curTime : float = 0.0f;
- private var lastTime : float = 0.0f;
- private var curRow : int = 0;
- private var curCol : int = 0;
- private var isEnd : boolean = false;
- function Start () {
- renderer.material.SetTextureScale("_MainTex", new Vector2(1.0 / colCount, 1.0 / rowCount));
- renderer.material.SetTextureOffset("_MainTex", new Vector2(0, 0));
- }
- function Update () {
- if (isEnd) {
- return;
- }
- if (curTime >= lastTime + 1.0f / fps) {
- curCol++;
- if (curCol >= colCount) {
- curCol = 0;
- curRow++;
- if (curRow >= rowCount) {
- if (!isLoop) {
- isEnd = true;
- return;
- }
- curRow = 0;
- }
- }
- renderer.material.SetTextureOffset("_MainTex", new Vector2(curCol * 1.0 / colCount, curRow * 1.0 / rowCount));
- lastTime = curTime;
- }
- else {
- curTime += Time.deltaTime;
- }
- }