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.

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