UGUISpriteAnimation.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.UI;
  5. using System;
  6. [RequireComponent(typeof(Image))]
  7. public class UGUISpriteAnimation : MonoBehaviour
  8. {
  9. private Image ImageSource;
  10. private int mCurFrame = 0;
  11. private float mDelta = 0;
  12. public float FPS = 16;
  13. //public List<Sprite> SpriteFrames;
  14. public bool IsPlaying = false;
  15. public bool Foward = true;
  16. public bool AutoPlay = true;
  17. public bool Loop = false;
  18. public Action OnAnimationEndEvent;
  19. private string _containerName;
  20. public int FrameCount
  21. {
  22. get
  23. {
  24. return SpriteFrames.Count;
  25. }
  26. }
  27. /// <summary>
  28. /// 资源
  29. /// </summary>
  30. private Dictionary<string, Sprite> SpriteFrames = new Dictionary<string, Sprite>();
  31. /// <summary>
  32. ///
  33. /// </summary>
  34. /// <param name="frameContainer"></param>
  35. /// <param name="frameCount"></param>
  36. public void Init(string frameContainer, Action onInitFinished)
  37. {
  38. _containerName = frameContainer;
  39. ImageSource = GetComponent<Image>();
  40. ResourceHelper.Instance.LoadAssetBundle(frameContainer, (AssetBundle bundle) =>
  41. {
  42. if (null != bundle)
  43. {
  44. GameObject go = (GameObject)bundle.LoadAsset(frameContainer);
  45. List<Sprite> tempList = go.GetComponent<IconContainer>().mSpriteList;
  46. for (int i = 0; i < tempList.Count; i++)
  47. {
  48. if (null != tempList[i])
  49. {
  50. SpriteFrames[tempList[i].name] = tempList[i];
  51. }
  52. }
  53. //SpriteFrames = go.GetComponent<IconContainer>().mSpriteList;
  54. }
  55. if (onInitFinished != null)
  56. {
  57. onInitFinished();
  58. }
  59. });
  60. }
  61. public void StartPlayAnimation()
  62. {
  63. if (AutoPlay)
  64. {
  65. Play();
  66. }
  67. else
  68. {
  69. IsPlaying = false;
  70. }
  71. }
  72. private void SetSprite(int idx)
  73. {
  74. string resname = "frame (" + (idx + 1) + ")";
  75. ImageSource.sprite = SpriteFrames[resname];
  76. ImageSource.SetNativeSize();
  77. //IconManager.Instance.GetSprite(_containerName, resname, texture =>
  78. //{
  79. // ImageSource.overrideSprite = texture;
  80. // ImageSource.SetNativeSize();
  81. //});
  82. }
  83. public void Play()
  84. {
  85. IsPlaying = true;
  86. Foward = true;
  87. }
  88. public void PlayReverse()
  89. {
  90. IsPlaying = true;
  91. Foward = false;
  92. }
  93. void Update()
  94. {
  95. if (!IsPlaying || 0 == FrameCount)
  96. {
  97. return;
  98. }
  99. mDelta += Time.deltaTime;
  100. if (mDelta > 1 / FPS)
  101. {
  102. mDelta = 0;
  103. if (Foward)
  104. {
  105. mCurFrame++;
  106. }
  107. else
  108. {
  109. mCurFrame--;
  110. }
  111. if (mCurFrame >= FrameCount)
  112. {
  113. InvokeEndEvent();
  114. if (Loop)
  115. {
  116. mCurFrame = 0;
  117. }
  118. else
  119. {
  120. IsPlaying = false;
  121. return;
  122. }
  123. }
  124. else if (mCurFrame < 0)
  125. {
  126. InvokeEndEvent();
  127. if (Loop)
  128. {
  129. mCurFrame = FrameCount - 1;
  130. }
  131. else
  132. {
  133. IsPlaying = false;
  134. return;
  135. }
  136. }
  137. SetSprite(mCurFrame);
  138. }
  139. }
  140. private void InvokeEndEvent()
  141. {
  142. if (OnAnimationEndEvent != null)
  143. {
  144. OnAnimationEndEvent();
  145. }
  146. }
  147. public void Pause()
  148. {
  149. IsPlaying = false;
  150. }
  151. public void Resume()
  152. {
  153. if (!IsPlaying)
  154. {
  155. IsPlaying = true;
  156. }
  157. }
  158. public void Stop()
  159. {
  160. mCurFrame = 0;
  161. SetSprite(mCurFrame);
  162. IsPlaying = false;
  163. }
  164. public void Rewind()
  165. {
  166. mCurFrame = 0;
  167. SetSprite(mCurFrame);
  168. Play();
  169. }
  170. }