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

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

目前成都創(chuàng)新互聯(lián)已為數(shù)千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計、鎮(zhèn)坪網(wǎng)站維護等服務(wù),公司將堅持客戶導向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

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>
 /// 幀率,為正時正向播放,為負時反向播放
 /// </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ā)此事件時,當前幀不一定為結(jié)束幀
 /// </summary>
 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;

 /// <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 {
 //從曲線值計算當前幀率
 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);
 //滿足更新條件,執(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è)置計時器為當前時間
 timer = ignoreTimeScale ? Time.unscaledTime : Time.time;
 }
}

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

網(wǎng)站標題:使用Unity怎么實現(xiàn)序列幀動畫播放器
當前網(wǎng)址:http://bm7419.com/article14/gejcge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、Google、網(wǎng)站策劃品牌網(wǎng)站建設(shè)、品牌網(wǎng)站制作外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名