Loom.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using UnityEngine;
  5. namespace Rokid.UXR.Utility
  6. {
  7. public class Loom : MonoBehaviour
  8. {
  9. /// <summary> A delayed queue item. </summary>
  10. public class DelayedQueueItem
  11. {
  12. /// <summary> The time. </summary>
  13. public float time;
  14. /// <summary> The action. </summary>
  15. public Action action;
  16. }
  17. /// <summary> The current. </summary>
  18. private static Loom m_Current;
  19. /// <summary> Number of. </summary>
  20. private int m_Count;
  21. /// <summary> True once initialization is complete. </summary>
  22. private static bool m_Initialized;
  23. /// <summary> Identifier for the thread. </summary>
  24. private static int m_ThreadId = -1;
  25. /// <summary> The actions. </summary>
  26. private List<Action> m_Actions = new List<Action>();
  27. /// <summary> The delayed. </summary>
  28. private List<Loom.DelayedQueueItem> m_Delayed = new List<Loom.DelayedQueueItem>();
  29. /// <summary> Gets the current. </summary>
  30. /// <value> The current. </value>
  31. public static Loom Current
  32. {
  33. get
  34. {
  35. if (!Loom.m_Initialized)
  36. {
  37. Loom.Initialize();
  38. }
  39. return Loom.m_Current;
  40. }
  41. }
  42. /// <summary> Initializes this object. </summary>
  43. public static void Initialize()
  44. {
  45. bool flag = !Loom.m_Initialized;
  46. if (flag && Loom.m_ThreadId != -1 && Loom.m_ThreadId != Thread.CurrentThread.ManagedThreadId)
  47. {
  48. return;
  49. }
  50. if (flag)
  51. {
  52. GameObject gameObject = new GameObject("MainThreadDispather");
  53. gameObject.hideFlags = HideFlags.DontSave;
  54. UnityEngine.Object.DontDestroyOnLoad(gameObject);
  55. if (Loom.m_Current)
  56. {
  57. if (Application.isPlaying)
  58. {
  59. UnityEngine.Object.Destroy(Loom.m_Current.gameObject);
  60. }
  61. else
  62. {
  63. UnityEngine.Object.DestroyImmediate(Loom.m_Current.gameObject);
  64. }
  65. }
  66. Loom.m_Current = gameObject.AddComponent<Loom>();
  67. UnityEngine.Object.DontDestroyOnLoad(Loom.m_Current);
  68. Loom.m_Initialized = true;
  69. Loom.m_ThreadId = Thread.CurrentThread.ManagedThreadId;
  70. }
  71. }
  72. /// <summary> Executes the 'destroy' action. </summary>
  73. private void OnDestroy()
  74. {
  75. Loom.m_Initialized = false;
  76. }
  77. /// <summary> Queue on main thread. </summary>
  78. /// <param name="action"> The action.</param>
  79. public static void QueueOnMainThread(Action action)
  80. {
  81. Loom.QueueOnMainThread(action, 0f);
  82. }
  83. /// <summary> Queue on main thread. </summary>
  84. /// <param name="action"> The action.</param>
  85. /// <param name="time"> The time.</param>
  86. public static void QueueOnMainThread(Action action, float time)
  87. {
  88. if (time != 0f)
  89. {
  90. List<Loom.DelayedQueueItem> delayed = Loom.Current.m_Delayed;
  91. lock (delayed)
  92. {
  93. Loom.Current.m_Delayed.Add(new Loom.DelayedQueueItem
  94. {
  95. time = Time.time + time,
  96. action = action
  97. });
  98. }
  99. }
  100. else
  101. {
  102. List<Action> actions = Loom.Current.m_Actions;
  103. lock (actions)
  104. {
  105. Loom.Current.m_Actions.Add(action);
  106. }
  107. }
  108. }
  109. /// <summary> Executes the 'asynchronous' operation. </summary>
  110. /// <param name="action"> The action.</param>
  111. public static void RunAsync(Action action)
  112. {
  113. new Thread(new ParameterizedThreadStart(Loom.RunAction))
  114. {
  115. Priority = System.Threading.ThreadPriority.Normal
  116. }.Start(action);
  117. }
  118. /// <summary> Executes the action. </summary>
  119. /// <param name="action"> The action.</param>
  120. private static void RunAction(object action)
  121. {
  122. ((Action)action)?.Invoke();
  123. }
  124. /// <summary> Updates this object. </summary>
  125. private void Update()
  126. {
  127. List<Action> actions = this.m_Actions;
  128. if (actions.Count > 0)
  129. {
  130. lock (actions)
  131. {
  132. for (int i = 0; i < this.m_Actions.Count; i++)
  133. {
  134. this.m_Actions[i]();
  135. }
  136. this.m_Actions.Clear();
  137. }
  138. }
  139. List<Loom.DelayedQueueItem> delayed = this.m_Delayed;
  140. if (delayed.Count > 0)
  141. {
  142. lock (delayed)
  143. {
  144. for (int j = 0; j < this.m_Delayed.Count; j++)
  145. {
  146. Loom.DelayedQueueItem delayedQueueItem = this.m_Delayed[j];
  147. if (delayedQueueItem.time <= Time.time)
  148. {
  149. delayedQueueItem.action();
  150. this.m_Delayed.RemoveAt(j);
  151. j--;
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }