UISpriteAnimationWithPingPong.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //----------------------------------------------
  2. // NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2013 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. /// NA: This is an updated version of UISpriteAnimation from NGUI that supports ping-pong animation
  10. /// </summary>
  11. [ExecuteInEditMode]
  12. [RequireComponent(typeof(UISprite))]
  13. [AddComponentMenu("NGUI/UI/Sprite Animation")]
  14. public class UISpriteAnimationWithPingPong : MonoBehaviour
  15. {
  16. [HideInInspector][SerializeField] int mFPS = 30;
  17. [HideInInspector][SerializeField] string mPrefix = "";
  18. [HideInInspector][SerializeField] bool mLoop = true;
  19. [HideInInspector][SerializeField] bool mPingPong = true;
  20. UISprite mSprite;
  21. float mDelta = 0f;
  22. int mIndex = 0;
  23. bool mActive = true;
  24. List<string> mSpriteNames = new List<string>();
  25. bool isInReverse;
  26. /// <summary>
  27. /// Number of frames in the animation.
  28. /// </summary>
  29. public int frames { get { return mSpriteNames.Count; } }
  30. /// <summary>
  31. /// Animation framerate.
  32. /// </summary>
  33. public int framesPerSecond { get { return mFPS; } set { mFPS = value; } }
  34. /// <summary>
  35. /// Set the name prefix used to filter sprites from the atlas.
  36. /// </summary>
  37. public string namePrefix { get { return mPrefix; } set { if (mPrefix != value) { mPrefix = value; RebuildSpriteList(); } } }
  38. /// <summary>
  39. /// Set the animation to be looping or not
  40. /// </summary>
  41. public bool loop { get { return mLoop; } set { mLoop = value; } }
  42. public bool pingPong { get { return mPingPong; } set { mPingPong = value; } }
  43. /// <summary>
  44. /// Returns is the animation is still playing or not
  45. /// </summary>
  46. public bool isPlaying { get { return mActive; } }
  47. /// <summary>
  48. /// Rebuild the sprite list first thing.
  49. /// </summary>
  50. void Start () { RebuildSpriteList(); }
  51. /// <summary>
  52. /// Advance the sprite animation process.
  53. /// </summary>
  54. void Update ()
  55. {
  56. if (mActive && mSpriteNames.Count > 1 && Application.isPlaying && mFPS > 0f)
  57. {
  58. mDelta += Time.deltaTime;
  59. float rate = 1f / mFPS;
  60. if (rate < mDelta)
  61. {
  62. mDelta = (rate > 0f) ? mDelta - rate : 0f;
  63. if (isInReverse)
  64. {
  65. if (--mIndex < 0)
  66. {
  67. mIndex = 0;
  68. isInReverse = false;
  69. mActive = loop;
  70. }
  71. } else
  72. {
  73. if (++mIndex >= mSpriteNames.Count)
  74. {
  75. if (mPingPong)
  76. {
  77. mIndex = mSpriteNames.Count - 1;
  78. isInReverse = true;
  79. } else
  80. {
  81. mIndex = 0;
  82. }
  83. mActive = loop;
  84. }
  85. }
  86. if (mActive)
  87. {
  88. mSprite.spriteName = mSpriteNames[mIndex];
  89. mSprite.MakePixelPerfect();
  90. //Nev: Since MakePixelPerfect is important for getting the right aspect ratios
  91. //(sprites in the animation might have different dimensions). We have to multiply
  92. //according to the current resolution, otherwise in an SD they would be small
  93. //and in SHD they would be big
  94. //Remember that we are designing the game in HD resolution.
  95. if (ResolutionSwitchController.IsSD)
  96. {
  97. transform.localScale *= 2;
  98. }
  99. if (ResolutionSwitchController.IsSHD)
  100. {
  101. transform.localScale /= 2;
  102. }
  103. }
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// Rebuild the sprite list after changing the sprite name.
  109. /// </summary>
  110. void RebuildSpriteList ()
  111. {
  112. if (mSprite == null) mSprite = GetComponent<UISprite>();
  113. mSpriteNames.Clear();
  114. if (mSprite != null && mSprite.atlas != null)
  115. {
  116. List<UIAtlas.Sprite> sprites = mSprite.atlas.spriteList;
  117. for (int i = 0, imax = sprites.Count; i < imax; ++i)
  118. {
  119. UIAtlas.Sprite sprite = sprites[i];
  120. if (string.IsNullOrEmpty(mPrefix) || sprite.name.StartsWith(mPrefix))
  121. {
  122. mSpriteNames.Add(sprite.name);
  123. }
  124. }
  125. mSpriteNames.Sort();
  126. }
  127. }
  128. /// <summary>
  129. /// Reset the animation to frame 0 and activate it.
  130. /// </summary>
  131. public void Reset()
  132. {
  133. mActive = true;
  134. mIndex = 0;
  135. isInReverse = false;
  136. if (mSprite != null && mSpriteNames.Count > 0)
  137. {
  138. mSprite.spriteName = mSpriteNames[mIndex];
  139. mSprite.MakePixelPerfect();
  140. }
  141. }
  142. }