UISpriteAnimation.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /****************************************************************************
  2. * Copyright (c) 2017 liangxie
  3. *
  4. * http://qframework.io
  5. * https://github.com/liangxiegame/QFramework
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. ****************************************************************************/
  25. using System.Collections.Generic;
  26. using UnityEngine;
  27. using UnityEngine.UI;
  28. namespace QFramework
  29. {
  30. /// <summary>
  31. /// 动画播放控件
  32. /// http://www.cnblogs.com/mrblue/p/5191183.html
  33. /// </summary>
  34. [RequireComponent(typeof(Image))]
  35. public class UISpriteAnimation : MonoBehaviour
  36. {
  37. private Image mImageSource;
  38. private int mCurFrame = 0;
  39. private float mDelta = 0;
  40. public float FPS = 5;
  41. public List<Sprite> SpriteFrames;
  42. public bool IsPlaying = false;
  43. public bool Forward = true;
  44. public bool AutoPlay = false;
  45. public bool Loop = false;
  46. public int FrameCount
  47. {
  48. get { return SpriteFrames.Count; }
  49. }
  50. void Awake()
  51. {
  52. mImageSource = GetComponent<Image>();
  53. }
  54. void Start()
  55. {
  56. if (AutoPlay)
  57. {
  58. Play();
  59. }
  60. else
  61. {
  62. IsPlaying = false;
  63. }
  64. }
  65. private void SetSprite(int idx)
  66. {
  67. mImageSource.sprite = SpriteFrames[idx];
  68. mImageSource.SetNativeSize();
  69. }
  70. public void Play()
  71. {
  72. IsPlaying = true;
  73. Forward = true;
  74. }
  75. public void PlayReverse()
  76. {
  77. IsPlaying = true;
  78. Forward = false;
  79. }
  80. void Update()
  81. {
  82. if (!IsPlaying || 0 == FrameCount)
  83. {
  84. return;
  85. }
  86. mDelta += Time.deltaTime;
  87. if (mDelta > 1 / FPS)
  88. {
  89. mDelta = 0;
  90. if (Forward)
  91. {
  92. mCurFrame++;
  93. }
  94. else
  95. {
  96. mCurFrame--;
  97. }
  98. if (mCurFrame >= FrameCount)
  99. {
  100. if (Loop)
  101. {
  102. mCurFrame = 0;
  103. }
  104. else
  105. {
  106. IsPlaying = false;
  107. return;
  108. }
  109. }
  110. else if (mCurFrame < 0)
  111. {
  112. if (Loop)
  113. {
  114. mCurFrame = FrameCount - 1;
  115. }
  116. else
  117. {
  118. IsPlaying = false;
  119. return;
  120. }
  121. }
  122. SetSprite(mCurFrame);
  123. }
  124. }
  125. public void Pause()
  126. {
  127. IsPlaying = false;
  128. }
  129. public void Resume()
  130. {
  131. if (!IsPlaying)
  132. {
  133. IsPlaying = true;
  134. }
  135. }
  136. public void Stop()
  137. {
  138. mCurFrame = 0;
  139. SetSprite(mCurFrame);
  140. IsPlaying = false;
  141. }
  142. public void Rewind()
  143. {
  144. mCurFrame = 0;
  145. SetSprite(mCurFrame);
  146. Play();
  147. }
  148. }
  149. }