溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用Unity怎么實現(xiàn)序列幀動畫播放器

發(fā)布時間:2021-05-31 17:56:22 來源:億速云 閱讀:374 作者:Leah 欄目:編程語言

使用Unity怎么實現(xiàn)序列幀動畫播放器?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

using UnityEngine;
using UnityEngine.UI;
using System;

/// <summary>
/// 序列幀動畫播放器
/// 支持UGUI的Image和Unity2D的SpriteRenderer
/// </summary>
public class FrameAnimator : MonoBehaviour
{
 /// <summary>
 /// 序列幀
 /// </summary>
 public Sprite[] Frames{ get { return frames; } set { frames = value; } }

 [SerializeField]private Sprite[] frames = null;

 /// <summary>
 /// 幀率,為正時正向播放,為負(fù)時反向播放
 /// </summary>
 public float Framerate { get { return framerate; } set { framerate = value; } }

 [SerializeField] private float framerate = 20.0f;

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

 [SerializeField]private bool ignoreTimeScale = true;

 /// <summary>
 /// 是否循環(huán)
 /// </summary>
 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));

 /// <summary>
 /// 結(jié)束事件
 /// 在每次播放完一個周期時觸發(fā)
 /// 在循環(huán)模式下觸發(fā)此事件時,當(dāng)前幀不一定為結(jié)束幀
 /// </summary>
 public event Action FinishEvent;

 //目標(biāo)Image組件
 private Image image;
 //目標(biāo)SpriteRenderer組件
 private SpriteRenderer spriteRenderer;
 //當(dāng)前幀索引
 private int currentFrameIndex = 0;
 //下一次更新時間
 private float timer = 0.0f;
 //當(dāng)前幀率,通過曲線計算而來
 private float currentFramerate = 20.0f;

 /// <summary>
 /// 重設(shè)動畫
 /// </summary>
 public void Reset ()
 {
 currentFrameIndex = framerate < 0 ? frames.Length - 1 : 0;
 }

 /// <summary>
 /// 從停止的位置播放動畫
 /// </summary>
 public void Play ()
 {
 this.enabled = true;
 }

 /// <summary>
 /// 暫停動畫
 /// </summary>
 public void Pause ()
 {
 this.enabled = false;
 }

 /// <summary>
 /// 停止動畫,將位置設(shè)為初始位置
 /// </summary>
 public void Stop ()
 {
 Pause ();
 Reset ();
 }
 
 //自動開啟動畫
 void Start ()
 {
 image = this.GetComponent<Image> ();
 spriteRenderer = this.GetComponent<SpriteRenderer> ();
 #if UNITY_EDITOR
 if (image == null && spriteRenderer == null) {
 Debug.LogWarning ("No available component found. 'Image' or 'SpriteRenderer' required.", this.gameObject);
 }
 #endif
 }

 void Update ()
 {
 //幀數(shù)據(jù)無效,禁用腳本
 if (frames == null || frames.Length == 0) {
 this.enabled = false;
 } else {
 //從曲線值計算當(dāng)前幀率
 float curveValue = curve.Evaluate ((float)currentFrameIndex / frames.Length);
 float curvedFramerate = curveValue * framerate;
 //幀率有效
 if (curvedFramerate != 0) {
 //獲取當(dāng)前時間
 float time = ignoreTimeScale ? Time.unscaledTime : Time.time;
 //計算幀間隔時間
 float interval = Mathf.Abs (1.0f / curvedFramerate);
 //滿足更新條件,執(zhí)行更新操作
 if (time - timer > interval) {
 //執(zhí)行更新操作
 DoUpdate ();
 }
 }
 #if UNITY_EDITOR
 else {
 Debug.LogWarning ("Framerate got '0' value, animation stopped.");
 }
 #endif
 }
 }

 //具體更新操作
 private void DoUpdate ()
 {
 //計算新的索引
 int nextIndex = currentFrameIndex + (int)Mathf.Sign (currentFramerate);
 //索引越界,表示已經(jīng)到結(jié)束幀
 if (nextIndex < 0 || nextIndex >= frames.Length) {
 //廣播事件
 if (FinishEvent != null) {
 FinishEvent ();
 }
 //非循環(huán)模式,禁用腳本
 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];
 }
 //設(shè)置計時器為當(dāng)前時間
 timer = ignoreTimeScale ? Time.unscaledTime : Time.time;
 }
}

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI