播放动画的代码

 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 }

 

转载于:https://www.cnblogs.com/fuperfun/p/5341322.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值