FunctionPeriodic.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. ------------------- Code Monkey -------------------
  3. Thank you for downloading the Code Monkey Utilities
  4. I hope you find them useful in your projects
  5. If you have any questions use the contact form
  6. Cheers!
  7. unitycodemonkey.com
  8. --------------------------------------------------
  9. */
  10. using System;
  11. using System.Collections.Generic;
  12. using UnityEngine;
  13. namespace CodeMonkey.Utils {
  14. /*
  15. * Executes a Function periodically
  16. * */
  17. public class FunctionPeriodic {
  18. /*
  19. * Class to hook Actions into MonoBehaviour
  20. * */
  21. private class MonoBehaviourHook : MonoBehaviour {
  22. public Action OnUpdate;
  23. private void Update() {
  24. if (OnUpdate != null) OnUpdate();
  25. }
  26. }
  27. private static List<FunctionPeriodic> funcList; // Holds a reference to all active timers
  28. private static GameObject initGameObject; // Global game object used for initializing class, is destroyed on scene change
  29. private static void InitIfNeeded() {
  30. if (initGameObject == null) {
  31. initGameObject = new GameObject("FunctionPeriodic_Global");
  32. funcList = new List<FunctionPeriodic>();
  33. }
  34. }
  35. // Persist through scene loads
  36. public static FunctionPeriodic Create_Global(Action action, Func<bool> testDestroy, float timer) {
  37. FunctionPeriodic functionPeriodic = Create(action, testDestroy, timer, "", false, false, false);
  38. MonoBehaviour.DontDestroyOnLoad(functionPeriodic.gameObject);
  39. return functionPeriodic;
  40. }
  41. // Trigger [action] every [timer], execute [testDestroy] after triggering action, destroy if returns true
  42. public static FunctionPeriodic Create(Action action, Func<bool> testDestroy, float timer) {
  43. return Create(action, testDestroy, timer, "", false);
  44. }
  45. public static FunctionPeriodic Create(Action action, float timer) {
  46. return Create(action, null, timer, "", false, false, false);
  47. }
  48. public static FunctionPeriodic Create(Action action, float timer, string functionName) {
  49. return Create(action, null, timer, functionName, false, false, false);
  50. }
  51. public static FunctionPeriodic Create(Action callback, Func<bool> testDestroy, float timer, string functionName, bool stopAllWithSameName) {
  52. return Create(callback, testDestroy, timer, functionName, false, false, stopAllWithSameName);
  53. }
  54. public static FunctionPeriodic Create(Action action, Func<bool> testDestroy, float timer, string functionName, bool useUnscaledDeltaTime, bool triggerImmediately, bool stopAllWithSameName) {
  55. InitIfNeeded();
  56. if (stopAllWithSameName) {
  57. StopAllFunc(functionName);
  58. }
  59. GameObject gameObject = new GameObject("FunctionPeriodic Object " + functionName, typeof(MonoBehaviourHook));
  60. FunctionPeriodic functionPeriodic = new FunctionPeriodic(gameObject, action, timer, testDestroy, functionName, useUnscaledDeltaTime);
  61. gameObject.GetComponent<MonoBehaviourHook>().OnUpdate = functionPeriodic.Update;
  62. funcList.Add(functionPeriodic);
  63. if (triggerImmediately) action();
  64. return functionPeriodic;
  65. }
  66. public static void RemoveTimer(FunctionPeriodic funcTimer) {
  67. InitIfNeeded();
  68. funcList.Remove(funcTimer);
  69. }
  70. public static void StopTimer(string _name) {
  71. InitIfNeeded();
  72. for (int i = 0; i < funcList.Count; i++) {
  73. if (funcList[i].functionName == _name) {
  74. funcList[i].DestroySelf();
  75. return;
  76. }
  77. }
  78. }
  79. public static void StopAllFunc(string _name) {
  80. InitIfNeeded();
  81. for (int i = 0; i < funcList.Count; i++) {
  82. if (funcList[i].functionName == _name) {
  83. funcList[i].DestroySelf();
  84. i--;
  85. }
  86. }
  87. }
  88. public static bool IsFuncActive(string name) {
  89. InitIfNeeded();
  90. for (int i = 0; i < funcList.Count; i++) {
  91. if (funcList[i].functionName == name) {
  92. return true;
  93. }
  94. }
  95. return false;
  96. }
  97. private GameObject gameObject;
  98. private float timer;
  99. private float baseTimer;
  100. private bool useUnscaledDeltaTime;
  101. private string functionName;
  102. public Action action;
  103. public Func<bool> testDestroy;
  104. private FunctionPeriodic(GameObject gameObject, Action action, float timer, Func<bool> testDestroy, string functionName, bool useUnscaledDeltaTime) {
  105. this.gameObject = gameObject;
  106. this.action = action;
  107. this.timer = timer;
  108. this.testDestroy = testDestroy;
  109. this.functionName = functionName;
  110. this.useUnscaledDeltaTime = useUnscaledDeltaTime;
  111. baseTimer = timer;
  112. }
  113. public void SkipTimerTo(float timer) {
  114. this.timer = timer;
  115. }
  116. public void SetBaseTimer(float baseTimer) {
  117. this.baseTimer = baseTimer;
  118. }
  119. public float GetBaseTimer() {
  120. return baseTimer;
  121. }
  122. private void Update() {
  123. if (useUnscaledDeltaTime) {
  124. timer -= Time.unscaledDeltaTime;
  125. } else {
  126. timer -= Time.deltaTime;
  127. }
  128. if (timer <= 0) {
  129. action();
  130. if (testDestroy != null && testDestroy()) {
  131. //Destroy
  132. DestroySelf();
  133. } else {
  134. //Repeat
  135. timer += baseTimer;
  136. }
  137. }
  138. }
  139. public void DestroySelf() {
  140. RemoveTimer(this);
  141. if (gameObject != null) {
  142. UnityEngine.Object.Destroy(gameObject);
  143. }
  144. }
  145. }
  146. }