UI2DSpriteAnimation.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using UnityEngine;
  3. public class UI2DSpriteAnimation : MonoBehaviour
  4. {
  5. public bool isPlaying
  6. {
  7. get
  8. {
  9. return base.enabled;
  10. }
  11. }
  12. public int framesPerSecond
  13. {
  14. get
  15. {
  16. return this.framerate;
  17. }
  18. set
  19. {
  20. this.framerate = value;
  21. }
  22. }
  23. public void Play()
  24. {
  25. if (this.frames != null && this.frames.Length > 0)
  26. {
  27. if (!base.enabled && !this.loop)
  28. {
  29. int num = (this.framerate <= 0) ? (this.mIndex - 1) : (this.mIndex + 1);
  30. if (num < 0 || num >= this.frames.Length)
  31. {
  32. this.mIndex = ((this.framerate >= 0) ? 0 : (this.frames.Length - 1));
  33. }
  34. }
  35. base.enabled = true;
  36. this.UpdateSprite();
  37. }
  38. }
  39. public void Pause()
  40. {
  41. base.enabled = false;
  42. }
  43. public void ResetToBeginning()
  44. {
  45. this.mIndex = ((this.framerate >= 0) ? 0 : (this.frames.Length - 1));
  46. this.UpdateSprite();
  47. }
  48. private void Start()
  49. {
  50. this.Play();
  51. }
  52. private void Update()
  53. {
  54. if (this.frames == null || this.frames.Length == 0)
  55. {
  56. base.enabled = false;
  57. }
  58. else if (this.framerate != 0)
  59. {
  60. float num = (!this.ignoreTimeScale) ? Time.time : RealTime.time;
  61. if (this.mUpdate < num)
  62. {
  63. this.mUpdate = num;
  64. int num2 = (this.framerate <= 0) ? (this.mIndex - 1) : (this.mIndex + 1);
  65. if (!this.loop && (num2 < 0 || num2 >= this.frames.Length))
  66. {
  67. base.enabled = false;
  68. return;
  69. }
  70. this.mIndex = NGUIMath.RepeatIndex(num2, this.frames.Length);
  71. this.UpdateSprite();
  72. }
  73. }
  74. }
  75. private void UpdateSprite()
  76. {
  77. if (this.mUnitySprite == null && this.mNguiSprite == null)
  78. {
  79. this.mUnitySprite = base.GetComponent<SpriteRenderer>();
  80. this.mNguiSprite = base.GetComponent<UI2DSprite>();
  81. if (this.mUnitySprite == null && this.mNguiSprite == null)
  82. {
  83. base.enabled = false;
  84. return;
  85. }
  86. }
  87. float num = (!this.ignoreTimeScale) ? Time.time : RealTime.time;
  88. if (this.framerate != 0)
  89. {
  90. this.mUpdate = num + Mathf.Abs(1f / (float)this.framerate);
  91. }
  92. if (this.mUnitySprite != null)
  93. {
  94. this.mUnitySprite.sprite = this.frames[this.mIndex];
  95. }
  96. else if (this.mNguiSprite != null)
  97. {
  98. this.mNguiSprite.nextSprite = this.frames[this.mIndex];
  99. }
  100. }
  101. [SerializeField]
  102. protected int framerate = 20;
  103. public bool ignoreTimeScale = true;
  104. public bool loop = true;
  105. public Sprite[] frames;
  106. private SpriteRenderer mUnitySprite;
  107. private UI2DSprite mNguiSprite;
  108. private int mIndex;
  109. private float mUpdate;
  110. }