UISpriteAnimation.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [ExecuteInEditMode]
  5. [RequireComponent(typeof(UISprite))]
  6. [AddComponentMenu("NGUI/UI/Sprite Animation")]
  7. public class UISpriteAnimation : MonoBehaviour
  8. {
  9. public int frames
  10. {
  11. get
  12. {
  13. return this.mSpriteNames.Count;
  14. }
  15. }
  16. public int framesPerSecond
  17. {
  18. get
  19. {
  20. return this.mFPS;
  21. }
  22. set
  23. {
  24. this.mFPS = value;
  25. }
  26. }
  27. public string namePrefix
  28. {
  29. get
  30. {
  31. return this.mPrefix;
  32. }
  33. set
  34. {
  35. if (this.mPrefix != value)
  36. {
  37. this.mPrefix = value;
  38. this.RebuildSpriteList();
  39. }
  40. }
  41. }
  42. public bool loop
  43. {
  44. get
  45. {
  46. return this.mLoop;
  47. }
  48. set
  49. {
  50. this.mLoop = value;
  51. }
  52. }
  53. public bool isPlaying
  54. {
  55. get
  56. {
  57. return this.mActive;
  58. }
  59. }
  60. protected virtual void Start()
  61. {
  62. this.RebuildSpriteList();
  63. }
  64. protected virtual void Update()
  65. {
  66. if (this.mActive && this.mSpriteNames.Count > 1 && Application.isPlaying && this.mFPS > 0)
  67. {
  68. this.mDelta += RealTime.deltaTime;
  69. float num = 1f / (float)this.mFPS;
  70. if (num < this.mDelta)
  71. {
  72. this.mDelta = ((num <= 0f) ? 0f : (this.mDelta - num));
  73. if (++this.mIndex >= this.mSpriteNames.Count)
  74. {
  75. this.mIndex = 0;
  76. this.mActive = this.mLoop;
  77. }
  78. if (this.mActive)
  79. {
  80. this.mSprite.spriteName = this.mSpriteNames[this.mIndex];
  81. if (this.mSnap)
  82. {
  83. this.mSprite.MakePixelPerfect();
  84. }
  85. }
  86. }
  87. }
  88. }
  89. public void RebuildSpriteList()
  90. {
  91. if (this.mSprite == null)
  92. {
  93. this.mSprite = base.GetComponent<UISprite>();
  94. }
  95. this.mSpriteNames.Clear();
  96. if (this.mSprite != null && this.mSprite.atlas != null)
  97. {
  98. List<UISpriteData> spriteList = this.mSprite.atlas.spriteList;
  99. int i = 0;
  100. int count = spriteList.Count;
  101. while (i < count)
  102. {
  103. UISpriteData uispriteData = spriteList[i];
  104. if (string.IsNullOrEmpty(this.mPrefix) || uispriteData.name.StartsWith(this.mPrefix))
  105. {
  106. this.mSpriteNames.Add(uispriteData.name);
  107. }
  108. i++;
  109. }
  110. this.mSpriteNames.Sort();
  111. }
  112. }
  113. public void Play()
  114. {
  115. this.mActive = true;
  116. }
  117. public void Pause()
  118. {
  119. this.mActive = false;
  120. }
  121. public void ResetToBeginning()
  122. {
  123. this.mActive = true;
  124. this.mIndex = 0;
  125. if (this.mSprite != null && this.mSpriteNames.Count > 0)
  126. {
  127. this.mSprite.spriteName = this.mSpriteNames[this.mIndex];
  128. if (this.mSnap)
  129. {
  130. this.mSprite.MakePixelPerfect();
  131. }
  132. }
  133. }
  134. [HideInInspector]
  135. [SerializeField]
  136. protected int mFPS = 30;
  137. [HideInInspector]
  138. [SerializeField]
  139. protected string mPrefix = string.Empty;
  140. [HideInInspector]
  141. [SerializeField]
  142. protected bool mLoop = true;
  143. [HideInInspector]
  144. [SerializeField]
  145. protected bool mSnap = true;
  146. protected UISprite mSprite;
  147. protected float mDelta;
  148. protected int mIndex;
  149. protected bool mActive = true;
  150. protected List<string> mSpriteNames = new List<string>();
  151. }