首页 > 其他 > 详细

播放动画的代码

时间:2016-03-31 16:39:47      阅读:220      评论:0      收藏:0      [点我收藏+]
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class hero : MonoBehaviour
 5 {
 6     private bool animation = true;       //动画开关
 7 
 8     public int frameCountPerSecond = 10; //每秒播放10帧
 9     private float time = 0;              //计时器
10     
11     public Sprite[] sprite;              //存放sprite的数组
12 
13     // Update is called once per frame
14     void Update () {
15         if (animation)                  //如果开关打开的时候
16         {
17             time += Time.deltaTime;     //计时器开始计时
18             int frameIndex = (int)(time / (1f / frameCountPerSecond));  //声明一个指定帧(1,2,3,4......)
19             int frame = frameIndex % 2;                                 //当前只有两个动画所以和2求余,得出0,1,0,1......
20             this.GetComponent<SpriteRenderer>().sprite = sprite[frame]; //得到游戏组件并让它的sprite等于sprite[frame]
21         }
22     }
23 }

 

播放动画的代码

原文:http://www.cnblogs.com/fuperfun/p/5341322.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!