SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
5.8 KiB

4 years ago
  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. void Update() {
  117. if (useUnscaledDeltaTime) {
  118. timer -= Time.unscaledDeltaTime;
  119. } else {
  120. timer -= Time.deltaTime;
  121. }
  122. if (timer <= 0) {
  123. action();
  124. if (testDestroy != null && testDestroy()) {
  125. //Destroy
  126. DestroySelf();
  127. } else {
  128. //Repeat
  129. timer += baseTimer;
  130. }
  131. }
  132. }
  133. public void DestroySelf() {
  134. RemoveTimer(this);
  135. if (gameObject != null) {
  136. UnityEngine.Object.Destroy(gameObject);
  137. }
  138. }
  139. }
  140. }