UnityThread.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #define ENABLE_UPDATE_FUNCTION_CALLBACK
  2. #define ENABLE_LATEUPDATE_FUNCTION_CALLBACK
  3. #define ENABLE_FIXEDUPDATE_FUNCTION_CALLBACK
  4. using System;
  5. using System.Collections;
  6. using UnityEngine;
  7. using System.Collections.Generic;
  8. public class UnityThread : MonoBehaviour
  9. {
  10. //our (singleton) instance
  11. private static UnityThread instance = null;
  12. ////////////////////////////////////////////////UPDATE IMPL////////////////////////////////////////////////////////
  13. //Holds actions received from another Thread. Will be coped to actionCopiedQueueUpdateFunc then executed from there
  14. private static List<System.Action> actionQueuesUpdateFunc = new List<Action>();
  15. //holds Actions copied from actionQueuesUpdateFunc to be executed
  16. List<System.Action> actionCopiedQueueUpdateFunc = new List<System.Action>();
  17. // Used to know if whe have new Action function to execute. This prevents the use of the lock keyword every frame
  18. private volatile static bool noActionQueueToExecuteUpdateFunc = true;
  19. ////////////////////////////////////////////////LATEUPDATE IMPL////////////////////////////////////////////////////////
  20. //Holds actions received from another Thread. Will be coped to actionCopiedQueueLateUpdateFunc then executed from there
  21. private static List<System.Action> actionQueuesLateUpdateFunc = new List<Action>();
  22. //holds Actions copied from actionQueuesLateUpdateFunc to be executed
  23. List<System.Action> actionCopiedQueueLateUpdateFunc = new List<System.Action>();
  24. // Used to know if whe have new Action function to execute. This prevents the use of the lock keyword every frame
  25. private volatile static bool noActionQueueToExecuteLateUpdateFunc = true;
  26. ////////////////////////////////////////////////FIXEDUPDATE IMPL////////////////////////////////////////////////////////
  27. //Holds actions received from another Thread. Will be coped to actionCopiedQueueFixedUpdateFunc then executed from there
  28. private static List<System.Action> actionQueuesFixedUpdateFunc = new List<Action>();
  29. //holds Actions copied from actionQueuesFixedUpdateFunc to be executed
  30. List<System.Action> actionCopiedQueueFixedUpdateFunc = new List<System.Action>();
  31. // Used to know if whe have new Action function to execute. This prevents the use of the lock keyword every frame
  32. private volatile static bool noActionQueueToExecuteFixedUpdateFunc = true;
  33. //Used to initialize UnityThread. Call once before any function here
  34. public static void initUnityThread(bool visible = false)
  35. {
  36. if (instance != null)
  37. {
  38. return;
  39. }
  40. if (Application.isPlaying)
  41. {
  42. // add an invisible game object to the scene
  43. GameObject obj = new GameObject("MainThreadExecuter");
  44. if (!visible)
  45. {
  46. obj.hideFlags = HideFlags.HideAndDontSave;
  47. }
  48. DontDestroyOnLoad(obj);
  49. instance = obj.AddComponent<UnityThread>();
  50. }
  51. }
  52. public void Awake()
  53. {
  54. DontDestroyOnLoad(gameObject);
  55. }
  56. //////////////////////////////////////////////COROUTINE IMPL//////////////////////////////////////////////////////
  57. #if (ENABLE_UPDATE_FUNCTION_CALLBACK)
  58. public static void executeCoroutine(IEnumerator action)
  59. {
  60. if (instance != null)
  61. {
  62. executeInUpdate(() => instance.StartCoroutine(action));
  63. }
  64. }
  65. ////////////////////////////////////////////UPDATE IMPL////////////////////////////////////////////////////
  66. public static void executeInUpdate(System.Action action)
  67. {
  68. if (action == null)
  69. {
  70. throw new ArgumentNullException("action");
  71. }
  72. lock (actionQueuesUpdateFunc)
  73. {
  74. actionQueuesUpdateFunc.Add(action);
  75. noActionQueueToExecuteUpdateFunc = false;
  76. }
  77. }
  78. public void Update()
  79. {
  80. if (noActionQueueToExecuteUpdateFunc)
  81. {
  82. return;
  83. }
  84. //Clear the old actions from the actionCopiedQueueUpdateFunc queue
  85. actionCopiedQueueUpdateFunc.Clear();
  86. lock (actionQueuesUpdateFunc)
  87. {
  88. //Copy actionQueuesUpdateFunc to the actionCopiedQueueUpdateFunc variable
  89. actionCopiedQueueUpdateFunc.AddRange(actionQueuesUpdateFunc);
  90. //Now clear the actionQueuesUpdateFunc since we've done copying it
  91. actionQueuesUpdateFunc.Clear();
  92. noActionQueueToExecuteUpdateFunc = true;
  93. }
  94. // Loop and execute the functions from the actionCopiedQueueUpdateFunc
  95. for (int i = 0; i < actionCopiedQueueUpdateFunc.Count; i++)
  96. {
  97. actionCopiedQueueUpdateFunc[i].Invoke();
  98. }
  99. }
  100. #endif
  101. ////////////////////////////////////////////LATEUPDATE IMPL////////////////////////////////////////////////////
  102. #if (ENABLE_LATEUPDATE_FUNCTION_CALLBACK)
  103. public static void executeInLateUpdate(System.Action action)
  104. {
  105. if (action == null)
  106. {
  107. throw new ArgumentNullException("action");
  108. }
  109. lock (actionQueuesLateUpdateFunc)
  110. {
  111. actionQueuesLateUpdateFunc.Add(action);
  112. noActionQueueToExecuteLateUpdateFunc = false;
  113. }
  114. }
  115. public void LateUpdate()
  116. {
  117. if (noActionQueueToExecuteLateUpdateFunc)
  118. {
  119. return;
  120. }
  121. //Clear the old actions from the actionCopiedQueueLateUpdateFunc queue
  122. actionCopiedQueueLateUpdateFunc.Clear();
  123. lock (actionQueuesLateUpdateFunc)
  124. {
  125. //Copy actionQueuesLateUpdateFunc to the actionCopiedQueueLateUpdateFunc variable
  126. actionCopiedQueueLateUpdateFunc.AddRange(actionQueuesLateUpdateFunc);
  127. //Now clear the actionQueuesLateUpdateFunc since we've done copying it
  128. actionQueuesLateUpdateFunc.Clear();
  129. noActionQueueToExecuteLateUpdateFunc = true;
  130. }
  131. // Loop and execute the functions from the actionCopiedQueueLateUpdateFunc
  132. for (int i = 0; i < actionCopiedQueueLateUpdateFunc.Count; i++)
  133. {
  134. actionCopiedQueueLateUpdateFunc[i].Invoke();
  135. }
  136. }
  137. #endif
  138. ////////////////////////////////////////////FIXEDUPDATE IMPL//////////////////////////////////////////////////
  139. #if (ENABLE_FIXEDUPDATE_FUNCTION_CALLBACK)
  140. public static void executeInFixedUpdate(System.Action action)
  141. {
  142. if (action == null)
  143. {
  144. throw new ArgumentNullException("action");
  145. }
  146. lock (actionQueuesFixedUpdateFunc)
  147. {
  148. actionQueuesFixedUpdateFunc.Add(action);
  149. noActionQueueToExecuteFixedUpdateFunc = false;
  150. }
  151. }
  152. public void FixedUpdate()
  153. {
  154. if (noActionQueueToExecuteFixedUpdateFunc)
  155. {
  156. return;
  157. }
  158. //Clear the old actions from the actionCopiedQueueFixedUpdateFunc queue
  159. actionCopiedQueueFixedUpdateFunc.Clear();
  160. lock (actionQueuesFixedUpdateFunc)
  161. {
  162. //Copy actionQueuesFixedUpdateFunc to the actionCopiedQueueFixedUpdateFunc variable
  163. actionCopiedQueueFixedUpdateFunc.AddRange(actionQueuesFixedUpdateFunc);
  164. //Now clear the actionQueuesFixedUpdateFunc since we've done copying it
  165. actionQueuesFixedUpdateFunc.Clear();
  166. noActionQueueToExecuteFixedUpdateFunc = true;
  167. }
  168. // Loop and execute the functions from the actionCopiedQueueFixedUpdateFunc
  169. for (int i = 0; i < actionCopiedQueueFixedUpdateFunc.Count; i++)
  170. {
  171. actionCopiedQueueFixedUpdateFunc[i].Invoke();
  172. }
  173. }
  174. #endif
  175. public void OnDisable()
  176. {
  177. if (instance == this)
  178. {
  179. instance = null;
  180. }
  181. }
  182. }