FunctionUpdater.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. * Calls function on every Update until it returns true
  16. * */
  17. public class FunctionUpdater {
  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<FunctionUpdater> updaterList; // Holds a reference to all active updaters
  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("FunctionUpdater_Global");
  32. updaterList = new List<FunctionUpdater>();
  33. }
  34. }
  35. public static FunctionUpdater Create(Action updateFunc) {
  36. return Create(() => { updateFunc(); return false; }, "", true, false);
  37. }
  38. public static FunctionUpdater Create(Action updateFunc, string functionName) {
  39. return Create(() => { updateFunc(); return false; }, functionName, true, false);
  40. }
  41. public static FunctionUpdater Create(Func<bool> updateFunc) {
  42. return Create(updateFunc, "", true, false);
  43. }
  44. public static FunctionUpdater Create(Func<bool> updateFunc, string functionName) {
  45. return Create(updateFunc, functionName, true, false);
  46. }
  47. public static FunctionUpdater Create(Func<bool> updateFunc, string functionName, bool active) {
  48. return Create(updateFunc, functionName, active, false);
  49. }
  50. public static FunctionUpdater Create(Func<bool> updateFunc, string functionName, bool active, bool stopAllWithSameName) {
  51. InitIfNeeded();
  52. if (stopAllWithSameName) {
  53. StopAllUpdatersWithName(functionName);
  54. }
  55. GameObject gameObject = new GameObject("FunctionUpdater Object " + functionName, typeof(MonoBehaviourHook));
  56. FunctionUpdater functionUpdater = new FunctionUpdater(gameObject, updateFunc, functionName, active);
  57. gameObject.GetComponent<MonoBehaviourHook>().OnUpdate = functionUpdater.Update;
  58. updaterList.Add(functionUpdater);
  59. return functionUpdater;
  60. }
  61. private static void RemoveUpdater(FunctionUpdater funcUpdater) {
  62. InitIfNeeded();
  63. updaterList.Remove(funcUpdater);
  64. }
  65. public static void DestroyUpdater(FunctionUpdater funcUpdater) {
  66. InitIfNeeded();
  67. if (funcUpdater != null) {
  68. funcUpdater.DestroySelf();
  69. }
  70. }
  71. public static void StopUpdaterWithName(string functionName) {
  72. InitIfNeeded();
  73. for (int i = 0; i < updaterList.Count; i++) {
  74. if (updaterList[i].functionName == functionName) {
  75. updaterList[i].DestroySelf();
  76. return;
  77. }
  78. }
  79. }
  80. public static void StopAllUpdatersWithName(string functionName) {
  81. InitIfNeeded();
  82. for (int i = 0; i < updaterList.Count; i++) {
  83. if (updaterList[i].functionName == functionName) {
  84. updaterList[i].DestroySelf();
  85. i--;
  86. }
  87. }
  88. }
  89. private GameObject gameObject;
  90. private string functionName;
  91. private bool active;
  92. private Func<bool> updateFunc; // Destroy Updater if return true;
  93. public FunctionUpdater(GameObject gameObject, Func<bool> updateFunc, string functionName, bool active) {
  94. this.gameObject = gameObject;
  95. this.updateFunc = updateFunc;
  96. this.functionName = functionName;
  97. this.active = active;
  98. }
  99. public void Pause() {
  100. active = false;
  101. }
  102. public void Resume() {
  103. active = true;
  104. }
  105. private void Update() {
  106. if (!active) return;
  107. if (updateFunc()) {
  108. DestroySelf();
  109. }
  110. }
  111. public void DestroySelf() {
  112. RemoveUpdater(this);
  113. if (gameObject != null) {
  114. UnityEngine.Object.Destroy(gameObject);
  115. }
  116. }
  117. }
  118. }