EffectPlayer.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Coffee.UIEffects
  5. {
  6. /// <summary>
  7. /// Effect player.
  8. /// </summary>
  9. [Serializable]
  10. public class EffectPlayer
  11. {
  12. //################################
  13. // Public Members.
  14. //################################
  15. /// <summary>
  16. /// Gets or sets a value indicating whether is playing.
  17. /// </summary>
  18. [Header("Effect Player")] [Tooltip("Playing.")]
  19. public bool play = false;
  20. /// <summary>
  21. /// Gets or sets the delay before looping.
  22. /// </summary>
  23. [Tooltip("Initial play delay.")] [Range(0f, 10f)]
  24. public float initialPlayDelay = 0;
  25. /// <summary>
  26. /// Gets or sets the duration.
  27. /// </summary>
  28. [Tooltip("Duration.")] [Range(0.01f, 10f)]
  29. public float duration = 1;
  30. /// <summary>
  31. /// Gets or sets a value indicating whether can loop.
  32. /// </summary>
  33. [Tooltip("Loop.")] public bool loop = false;
  34. /// <summary>
  35. /// Gets or sets the delay before looping.
  36. /// </summary>
  37. [Tooltip("Delay before looping.")] [Range(0f, 10f)]
  38. public float loopDelay = 0;
  39. /// <summary>
  40. /// Gets or sets the update mode.
  41. /// </summary>
  42. [Tooltip("Update mode")] public AnimatorUpdateMode updateMode = AnimatorUpdateMode.Normal;
  43. static List<Action> s_UpdateActions;
  44. /// <summary>
  45. /// Register player.
  46. /// </summary>
  47. public void OnEnable(Action<float> callback = null)
  48. {
  49. if (s_UpdateActions == null)
  50. {
  51. s_UpdateActions = new List<Action>();
  52. Canvas.willRenderCanvases += () =>
  53. {
  54. var count = s_UpdateActions.Count;
  55. for (int i = 0; i < count; i++)
  56. {
  57. s_UpdateActions[i].Invoke();
  58. }
  59. };
  60. }
  61. s_UpdateActions.Add(OnWillRenderCanvases);
  62. if (play)
  63. {
  64. _time = -initialPlayDelay;
  65. }
  66. else
  67. {
  68. _time = 0;
  69. }
  70. _callback = callback;
  71. }
  72. /// <summary>
  73. /// Unregister player.
  74. /// </summary>
  75. public void OnDisable()
  76. {
  77. _callback = null;
  78. s_UpdateActions.Remove(OnWillRenderCanvases);
  79. }
  80. /// <summary>
  81. /// Start playing.
  82. /// </summary>
  83. public void Play(bool reset, Action<float> callback = null)
  84. {
  85. if (reset)
  86. {
  87. _time = 0;
  88. }
  89. play = true;
  90. if (callback != null)
  91. {
  92. _callback = callback;
  93. }
  94. }
  95. /// <summary>
  96. /// Stop playing.
  97. /// </summary>
  98. public void Stop(bool reset)
  99. {
  100. if (reset)
  101. {
  102. _time = 0;
  103. if (_callback != null)
  104. {
  105. _callback(_time);
  106. }
  107. }
  108. play = false;
  109. }
  110. //################################
  111. // Private Members.
  112. //################################
  113. float _time = 0;
  114. Action<float> _callback;
  115. void OnWillRenderCanvases()
  116. {
  117. if (!play || !Application.isPlaying || _callback == null)
  118. {
  119. return;
  120. }
  121. _time += updateMode == AnimatorUpdateMode.UnscaledTime
  122. ? Time.unscaledDeltaTime
  123. : Time.deltaTime;
  124. var current = _time / duration;
  125. if (duration <= _time)
  126. {
  127. play = loop;
  128. _time = loop ? -loopDelay : 0;
  129. }
  130. _callback(current);
  131. }
  132. }
  133. }