Loom.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. using System.Threading;
  6. using System.Linq;
  7. namespace Nxr.Internal
  8. {
  9. public class Loom : MonoBehaviour
  10. {
  11. public static int maxThreads = 4;
  12. static int numThreads;
  13. private static Loom _current;
  14. //private int _count;
  15. public static Loom Current
  16. {
  17. get
  18. {
  19. Initialize();
  20. return _current;
  21. }
  22. }
  23. void Awake()
  24. {
  25. _current = this;
  26. initialized = true;
  27. }
  28. static bool initialized;
  29. public static void Initialize()
  30. {
  31. if (!initialized)
  32. {
  33. if (!Application.isPlaying)
  34. return;
  35. initialized = true;
  36. var g = new GameObject("Loom");
  37. _current = g.AddComponent<Loom>();
  38. #if !ARTIST_BUILD
  39. UnityEngine.Object.DontDestroyOnLoad(g);
  40. #endif
  41. }
  42. }
  43. public struct NoDelayedQueueItem
  44. {
  45. public Action<object> action;
  46. public object param;
  47. }
  48. private List<NoDelayedQueueItem> _actions = new List<NoDelayedQueueItem>();
  49. public struct DelayedQueueItem
  50. {
  51. public float time;
  52. public Action<object> action;
  53. public object param;
  54. }
  55. private List<DelayedQueueItem> _delayed = new List<DelayedQueueItem>();
  56. List<DelayedQueueItem> _currentDelayed = new List<DelayedQueueItem>();
  57. public static void QueueOnMainThread(Action<object> taction, object tparam)
  58. {
  59. QueueOnMainThread(taction, tparam, 0f);
  60. }
  61. public static void QueueOnMainThread(Action<object> taction, object tparam, float time)
  62. {
  63. if (time != 0)
  64. {
  65. lock (Current._delayed)
  66. {
  67. Current._delayed.Add(new DelayedQueueItem { time = Time.time + time, action = taction, param = tparam });
  68. }
  69. }
  70. else
  71. {
  72. lock (Current._actions)
  73. {
  74. Current._actions.Add(new NoDelayedQueueItem { action = taction, param = tparam });
  75. }
  76. }
  77. }
  78. public static Thread RunAsync(Action a)
  79. {
  80. Initialize();
  81. while (numThreads >= maxThreads)
  82. {
  83. Thread.Sleep(100);
  84. }
  85. Interlocked.Increment(ref numThreads);
  86. ThreadPool.QueueUserWorkItem(RunAction, a);
  87. return null;
  88. }
  89. private static void RunAction(object action)
  90. {
  91. try
  92. {
  93. ((Action)action)();
  94. }
  95. catch
  96. {
  97. }
  98. finally
  99. {
  100. Interlocked.Decrement(ref numThreads);
  101. }
  102. }
  103. void OnDisable()
  104. {
  105. if (_current == this)
  106. {
  107. _current = null;
  108. }
  109. }
  110. List<NoDelayedQueueItem> _currentActions = new List<NoDelayedQueueItem>();
  111. // Update is called once per frame
  112. void Update()
  113. {
  114. if (_actions.Count > 0)
  115. {
  116. lock (_actions)
  117. {
  118. _currentActions.Clear();
  119. _currentActions.AddRange(_actions);
  120. _actions.Clear();
  121. }
  122. for (int i = 0; i < _currentActions.Count; i++)
  123. {
  124. _currentActions[i].action(_currentActions[i].param);
  125. }
  126. }
  127. if (_delayed.Count > 0)
  128. {
  129. lock (_delayed)
  130. {
  131. _currentDelayed.Clear();
  132. _currentDelayed.AddRange(_delayed.Where(d => d.time <= Time.time));
  133. for (int i = 0; i < _currentDelayed.Count; i++)
  134. {
  135. _delayed.Remove(_currentDelayed[i]);
  136. }
  137. }
  138. for (int i = 0; i < _currentDelayed.Count; i++)
  139. {
  140. _currentDelayed[i].action(_currentDelayed[i].param);
  141. }
  142. }
  143. }
  144. }
  145. }