查看: 476|回复: 0
打印 上一主题 下一主题

[其他] 序列帧动画使用代码

[复制链接]
may    

8830

主题

80

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
52304
精华
343

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2015-9-26 00:21:41 |只看该作者 |倒序浏览
  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. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-6 10:34 , Processed in 0.080697 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部