TapLoom.cs 4.2 KB

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