Nex92

Untitled

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