栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

Unity代码实现序列帧动画播放器

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity代码实现序列帧动画播放器

序列帧动画经常用到,最直接的方式就是用Animation录制。但某些情况下这种方式并不是太友好,需要靠代码的方式进行序列帧动画的实现。

代码实现序列帧动画,基本的思路是定义一个序列帧的数组/列表,根据时间的流逝来确定使用哪一帧并更新显示。

NGUI的UI2DSpriteAnimation已经实现了此功能,但是它支持的目标只有Native2D的SpriteRenderer组件或者NGUI自身的UI2DSprite组件,并不支持UGUI的Image组件。

当然可以通过改写源码的方式来添加对Image组件的支持,不过秉着学习的目的,我这里重新写了一个同时支持Image组件和SpriteRenderer组件的序列帧动画播放器。

代码如下,注释写的很详细了,不再赘述。

using UnityEngine;
using UnityEngine.UI;
using System;

/// 
/// 序列帧动画播放器
/// 支持UGUI的Image和Unity2D的SpriteRenderer
/// 
public class frameAnimator : MonoBehaviour
{
 /// 
 /// 序列帧
 /// 
 public Sprite[] frames{ get { return frames; } set { frames = value; } }

 [SerializeField]private Sprite[] frames = null;

 /// 
 /// 帧率,为正时正向播放,为负时反向播放
 /// 
 public float framerate { get { return framerate; } set { framerate = value; } }

 [SerializeField] private float framerate = 20.0f;

 /// 
 /// 是否忽略timeScale
 /// 
 public bool IgnoreTimeScale{ get { return ignoreTimeScale; } set { ignoreTimeScale = value; } }

 [SerializeField]private bool ignoreTimeScale = true;

 /// 
 /// 是否循环
 /// 
 public bool Loop{ get { return loop; } set { loop = value; } }

 [SerializeField]private bool loop = true;

 //动画曲线
 [SerializeField]private AnimationCurve curve = new AnimationCurve (new Keyframe (0, 1, 0, 0), new Keyframe (1, 1, 0, 0));

 /// 
 /// 结束事件
 /// 在每次播放完一个周期时触发
 /// 在循环模式下触发此事件时,当前帧不一定为结束帧
 /// 
 public event Action FinishEvent;

 //目标Image组件
 private Image image;
 //目标SpriteRenderer组件
 private SpriteRenderer spriteRenderer;
 //当前帧索引
 private int currentframeIndex = 0;
 //下一次更新时间
 private float timer = 0.0f;
 //当前帧率,通过曲线计算而来
 private float currentframerate = 20.0f;

 /// 
 /// 重设动画
 /// 
 public void Reset ()
 {
 currentframeIndex = framerate < 0 ? frames.Length - 1 : 0;
 }

 /// 
 /// 从停止的位置播放动画
 /// 
 public void Play ()
 {
 this.enabled = true;
 }

 /// 
 /// 暂停动画
 /// 
 public void Pause ()
 {
 this.enabled = false;
 }

 /// 
 /// 停止动画,将位置设为初始位置
 /// 
 public void Stop ()
 {
 Pause ();
 Reset ();
 }
 
 //自动开启动画
 void Start ()
 {
 image = this.GetComponent ();
 spriteRenderer = this.GetComponent ();
 #if UNITY_EDITOR
 if (image == null && spriteRenderer == null) {
 Debug.LogWarning ("No available component found. 'Image' or 'SpriteRenderer' required.", this.gameObject);
 }
 #endif
 }

 void Update ()
 {
 //帧数据无效,禁用脚本
 if (frames == null || frames.Length == 0) {
 this.enabled = false;
 } else {
 //从曲线值计算当前帧率
 float curvevalue = curve.evaluate ((float)currentframeIndex / frames.Length);
 float curvedframerate = curvevalue * framerate;
 //帧率有效
 if (curvedframerate != 0) {
 //获取当前时间
 float time = ignoreTimeScale ? Time.unscaledTime : Time.time;
 //计算帧间隔时间
 float interval = Mathf.Abs (1.0f / curvedframerate);
 //满足更新条件,执行更新操作
 if (time - timer > interval) {
 //执行更新操作
 DoUpdate ();
 }
 }
 #if UNITY_EDITOR
 else {
 Debug.LogWarning ("framerate got '0' value, animation stopped.");
 }
 #endif
 }
 }

 //具体更新操作
 private void DoUpdate ()
 {
 //计算新的索引
 int nextIndex = currentframeIndex + (int)Mathf.Sign (currentframerate);
 //索引越界,表示已经到结束帧
 if (nextIndex < 0 || nextIndex >= frames.Length) {
 //广播事件
 if (FinishEvent != null) {
 FinishEvent ();
 }
 //非循环模式,禁用脚本
 if (loop == false) {
 currentframeIndex = Mathf.Clamp (currentframeIndex, 0, frames.Length - 1);
 this.enabled = false;
 return;
 }
 }
 //钳制索引
 currentframeIndex = nextIndex % frames.Length;
 //更新图片
 if (image != null) {
 image.sprite = frames [currentframeIndex];
 } else if (spriteRenderer != null) {
 spriteRenderer.sprite = frames [currentframeIndex];
 }
 //设置计时器为当前时间
 timer = ignoreTimeScale ? Time.unscaledTime : Time.time;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/122628.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号