[NGUI]简单改装NGUI的UI sprite animation

在UISpriteAnimationInspector.cs中插入
  1. UISpriteAnimation.AnimationMode animMode = (UISpriteAnimation.AnimationMode)EditorGUILayout.EnumPopup("AnimationMode",anim.animationMode);
  2.   if(anim.animationMode != animMode)
  3.   {
  4.    NGUIEditorTools.RegisterUndo("Sprite Animation Change", anim);
  5.    anim.animationMode = animMode;
  6.    EditorUtility.SetDirty(anim);
  7.   }

把UISpriteAnimation.cs改成
  1. //----------------------------------------------
  2. // NGUI: Next-Gen UI kit
  3. // Copyright ? 2011-2012 Tasharen Entertainment
  4. //----------------------------------------------
  5. using UnityEngine;
  6. using System.Collections.Generic;
  7. /// <summary>
  8. /// Very simple sprite animation. Attach to a sprite and specify a bunch of sprite names and it will cycle through them.
  9. /// </summary>
  10. [ExecuteInEditMode]
  11. [RequireComponent(typeof(UISprite))]
  12. [AddComponentMenu("NGUI/UI/Sprite Animation")]
  13. public class UISpriteAnimation : MonoBehaviour
  14. {
  15. public enum AnimationMode
  16. {
  17. Forword = 0,
  18. Reverse = 1,
  19. PingPong = 2
  20. }
  21. [HideInInspector][SerializeField] int mFPS = 30;
  22. [HideInInspector][SerializeField] string mPrefix = "";
  23. [HideInInspector][SerializeField] bool mLoop = true;
  24. [HideInInspector][SerializeField] AnimationMode animMode = AnimationMode.Forword;
  25. UISprite mSprite;
  26. float mDelta = 0f;
  27. int mIndex = 0;
  28. int mFrames = 0;
  29. int nDelta = 1;
  30. bool mActive = true;
  31. List<string> mSpriteNames = new List<string>();
  32. public AnimationMode animationMode
  33. {
  34. get
  35. {
  36. return animMode;
  37. }
  38. set
  39. {
  40. animMode = value;
  41. }
  42. }
  43. /// <summary>
  44. /// Number of frames in the animation.
  45. /// </summary>
  46. public int frames { get { return mSpriteNames.Count; } }
  47. /// <summary>
  48. /// Animation framerate.
  49. /// </summary>
  50. public int framesPerSecond { get { return mFPS; } set { mFPS = value; } }
  51. /// <summary>
  52. /// Set the name prefix used to filter sprites from the atlas.
  53. /// </summary>
  54. public string namePrefix { get { return mPrefix; } set { if (mPrefix != value) { mPrefix = value; RebuildSpriteList(); } } }
  55. /// <summary>
  56. /// Set the animation to be looping or not
  57. /// </summary>
  58. public bool loop { get { return mLoop; } set { mLoop = value; } }
  59. /// <summary>
  60. /// Returns is the animation is still playing or not
  61. /// </summary>
  62. public bool isPlaying { get { return mActive; } }
  63. /// <summary>
  64. /// Rebuild the sprite list first thing.
  65. /// </summary>
  66. void Start ()
  67. {
  68. RebuildSpriteList();
  69. if(animMode == AnimationMode.Reverse)
  70. {
  71. SetFrame(mFrames-1);
  72. nDelta = -nDelta;
  73. }
  74. else
  75. {
  76. SetFrame(0);
  77. }
  78. }
  79. /// <summary>
  80. /// Advance the sprite animation process.
  81. /// </summary>
  82. void Update ()
  83. {
  84. if(mActive && mFrames > 1 && Application.isPlaying && mFPS > 0f)
  85. {
  86. mDelta += Time.deltaTime;
  87. float rate = 1f/mFPS;
  88. if(mDelta > rate)
  89. {
  90. mDelta = (rate > 0f) ? mDelta - rate : 0f;
  91. if(mIndex + nDelta >= mFrames)
  92. {
  93. if(mLoop)
  94. {
  95. switch(animMode)
  96. {
  97. case AnimationMode.PingPong:
  98. nDelta = -nDelta;
  99. break;
  100. }
  101. }
  102. else
  103. {
  104. mActive = false;
  105. }
  106. }
  107. else if(mIndex + nDelta < 0)
  108. {
  109. if(mLoop)
  110. {
  111. switch(animMode)
  112. {
  113. case AnimationMode.PingPong:
  114. nDelta = -nDelta;
  115. break;
  116. }
  117. }
  118. else
  119. {
  120. mActive = false;
  121. }
  122. }
  123. SetFrame(mIndex + nDelta);
  124. }
  125. }
  126. }
  127. public void SetFrame(int _frame)
  128. {
  129. if (mActive)
  130. {
  131. int m = _frame%mFrames;
  132. if(m < 0) m += mFrames;
  133. mIndex = m;
  134. mSprite.spriteName = mSpriteNames[mIndex];
  135. //mSprite.MakePixelPerfect();
  136. }
  137. }
  138. /// <summary>
  139. /// Rebuild the sprite list after changing the sprite name.
  140. /// </summary>
  141. void RebuildSpriteList ()
  142. {
  143. if (mSprite == null) mSprite = GetComponent<UISprite>();
  144. mSpriteNames.Clear();
  145. if (mSprite != null && mSprite.atlas != null)
  146. {
  147. List<UIAtlas.Sprite> sprites = mSprite.atlas.spriteList;
  148. for (int i = 0, imax = sprites.Count; i < imax; ++i)
  149. {
  150. UIAtlas.Sprite sprite = sprites[i];
  151. if (string.IsNullOrEmpty(mPrefix) || sprite.name.StartsWith(mPrefix))
  152. {
  153. mSpriteNames.Add(sprite.name);
  154. }
  155. }
  156. mSpriteNames.Sort();
  157. }
  158. mFrames = mSpriteNames.Count;
  159. }
  160. /// <summary>
  161. /// Reset the animation to frame 0 and activate it.
  162. /// </summary>
  163. public void Reset()
  164. {
  165. mActive = true;
  166. mIndex = 0;
  167. }
  168. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值