FunctionTimer.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 UnityEngine;
  12. using System.Collections.Generic;
  13. namespace CodeMonkey.Utils {
  14. /*
  15. * Triggers a Action after a certain time
  16. * */
  17. public class FunctionTimer {
  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<FunctionTimer> timerList; // 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("FunctionTimer_Global");
  32. timerList = new List<FunctionTimer>();
  33. }
  34. }
  35. public static FunctionTimer Create(Action action, float timer) {
  36. return Create(action, timer, "", false, false);
  37. }
  38. public static FunctionTimer Create(Action action, float timer, string functionName) {
  39. return Create(action, timer, functionName, false, false);
  40. }
  41. public static FunctionTimer Create(Action action, float timer, string functionName, bool useUnscaledDeltaTime) {
  42. return Create(action, timer, functionName, useUnscaledDeltaTime, false);
  43. }
  44. public static FunctionTimer Create(Action action, float timer, string functionName, bool useUnscaledDeltaTime, bool stopAllWithSameName) {
  45. InitIfNeeded();
  46. if (stopAllWithSameName) {
  47. StopAllTimersWithName(functionName);
  48. }
  49. GameObject obj = new GameObject("FunctionTimer Object "+functionName, typeof(MonoBehaviourHook));
  50. FunctionTimer funcTimer = new FunctionTimer(obj, action, timer, functionName, useUnscaledDeltaTime);
  51. obj.GetComponent<MonoBehaviourHook>().OnUpdate = funcTimer.Update;
  52. timerList.Add(funcTimer);
  53. return funcTimer;
  54. }
  55. public static void RemoveTimer(FunctionTimer funcTimer) {
  56. InitIfNeeded();
  57. timerList.Remove(funcTimer);
  58. }
  59. public static void StopAllTimersWithName(string functionName) {
  60. InitIfNeeded();
  61. for (int i = 0; i < timerList.Count; i++) {
  62. if (timerList[i].functionName == functionName) {
  63. timerList[i].DestroySelf();
  64. i--;
  65. }
  66. }
  67. }
  68. public static void StopFirstTimerWithName(string functionName) {
  69. InitIfNeeded();
  70. for (int i = 0; i < timerList.Count; i++) {
  71. if (timerList[i].functionName == functionName) {
  72. timerList[i].DestroySelf();
  73. return;
  74. }
  75. }
  76. }
  77. private GameObject gameObject;
  78. private float timer;
  79. private string functionName;
  80. private bool active;
  81. private bool useUnscaledDeltaTime;
  82. private Action action;
  83. public FunctionTimer(GameObject gameObject, Action action, float timer, string functionName, bool useUnscaledDeltaTime) {
  84. this.gameObject = gameObject;
  85. this.action = action;
  86. this.timer = timer;
  87. this.functionName = functionName;
  88. this.useUnscaledDeltaTime = useUnscaledDeltaTime;
  89. }
  90. private void Update() {
  91. if (useUnscaledDeltaTime) {
  92. timer -= Time.unscaledDeltaTime;
  93. } else {
  94. timer -= Time.deltaTime;
  95. }
  96. if (timer <= 0) {
  97. // Timer complete, trigger Action
  98. action();
  99. DestroySelf();
  100. }
  101. }
  102. private void DestroySelf() {
  103. RemoveTimer(this);
  104. if (gameObject != null) {
  105. UnityEngine.Object.Destroy(gameObject);
  106. }
  107. }
  108. /*
  109. * Class to trigger Actions manually without creating a GameObject
  110. * */
  111. public class FunctionTimerObject {
  112. private float timer;
  113. private Action callback;
  114. public FunctionTimerObject(Action callback, float timer) {
  115. this.callback = callback;
  116. this.timer = timer;
  117. }
  118. public bool Update() {
  119. return Update(Time.deltaTime);
  120. }
  121. public bool Update(float deltaTime) {
  122. timer -= deltaTime;
  123. if (timer <= 0) {
  124. callback();
  125. return true;
  126. } else {
  127. return false;
  128. }
  129. }
  130. }
  131. // Create a Object that must be manually updated through Update();
  132. public static FunctionTimerObject CreateObject(Action callback, float timer) {
  133. return new FunctionTimerObject(callback, timer);
  134. }
  135. }
  136. }