纳金网

标题: 序列帧动画的代码 [打印本页]

作者: 王者再临    时间: 2015-10-30 00:07
标题: 序列帧动画的代码
序列帧动画的代码
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.UI;

  5. public class UMovie : MonoBehaviour
  6. {

  7.         public int mFps = 5;//每秒多少帧
  8.         private float mSec; //一帧所需要的时间
  9.         private float mDelta = 0f;
  10.         public List<Sprite> sprites = new List<Sprite>();

  11.         private int curFrame = 0; // 当前帧
  12.         private bool mLoop = true;
  13.         private bool mPlay = false;

  14.         private int mStartIndex;
  15.         private int mEndIndex;

  16.         private Image container;

  17.         // Use this for initialization
  18.         void Start ()
  19.         {
  20.                 container = GetComponent<Image> ();
  21.         }

  22.         public void Play(bool IsLoop = true)
  23.         {
  24.                 if (sprites.Count > 0)
  25.                 {
  26.                         Play (0,sprites.Count-1, IsLoop);       
  27.                 }
  28.         }


  29.         public void Play(int Start, int EndIndex, bool IsLoop)
  30.         {
  31.                 mStartIndex = Start;
  32.                 mEndIndex = EndIndex;
  33.                 mLoop = IsLoop;
  34.                 mSec = 1f / (mFps);

  35.                 if (container == null)
  36.                 {
  37.                         container = GetComponent<Image> ();
  38.                 }
  39.                 container.enabled = true;
  40.                 mPlay = true;
  41.         }

  42.         public void Stop()
  43.         {
  44.                 mPlay = false;
  45.         }

  46.         // Update is called once per frame
  47.         void Update ()
  48.         {
  49.                 if (mPlay)
  50.                 {
  51.                         mDelta += Time.deltaTime;
  52.                         if(mDelta > mSec)
  53.                         {
  54.                                 mDelta = (mSec > 0f )? mDelta - mSec: 0f;
  55.                                 if(++curFrame > mEndIndex - mStartIndex)
  56.                                 {
  57.                                         curFrame = mStartIndex;
  58.                                         mPlay = mLoop;
  59.                                 }
  60.                                 container.sprite = sprites[curFrame];       
  61.                         }
  62.                 }
  63.         }

  64.         public void SetSprite(Sprite sp)
  65.         {
  66.                 mPlay = false;
  67.                 container.sprite = sp;
  68.         }

  69.         public void SetEnable(bool isEnable)
  70.         {
  71.                 container.enabled = isEnable;
  72.                 if (isEnable == false)
  73.                         mPlay = false;
  74.         }
  75. }
复制代码





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5