123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
-
- namespace NRKernal
- {
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using UnityEngine;
-
- [ExecuteInEditMode]
- public class MainThreadDispather : MonoBehaviour
- {
-
- public class DelayedQueueItem
- {
-
- public float time;
-
- public Action action;
- }
-
- private static MainThreadDispather m_Current;
-
- private int m_Count;
-
- private static float m_CurrentTime;
-
- private static bool m_Initialized;
-
- private static int m_ThreadId = -1;
-
- private List<Action> m_Actions = new List<Action>();
-
- private List<MainThreadDispather.DelayedQueueItem> m_Delayed = new List<MainThreadDispather.DelayedQueueItem>();
-
-
- public static MainThreadDispather Current
- {
- get
- {
- if (!MainThreadDispather.m_Initialized)
- {
- MainThreadDispather.Initialize();
- }
- return MainThreadDispather.m_Current;
- }
- }
-
- public static void Initialize()
- {
- bool flag = !MainThreadDispather.m_Initialized;
- if (flag && MainThreadDispather.m_ThreadId != -1 && MainThreadDispather.m_ThreadId != Thread.CurrentThread.ManagedThreadId)
- {
- return;
- }
- if (flag)
- {
- GameObject gameObject = new GameObject("MainThreadDispather");
- gameObject.hideFlags = HideFlags.DontSave;
- UnityEngine.Object.DontDestroyOnLoad(gameObject);
- if (MainThreadDispather.m_Current)
- {
- if (Application.isPlaying)
- {
- UnityEngine.Object.Destroy(MainThreadDispather.m_Current.gameObject);
- }
- else
- {
- UnityEngine.Object.DestroyImmediate(MainThreadDispather.m_Current.gameObject);
- }
- }
- MainThreadDispather.m_Current = gameObject.AddComponent<MainThreadDispather>();
- UnityEngine.Object.DontDestroyOnLoad(MainThreadDispather.m_Current);
- MainThreadDispather.m_Initialized = true;
- MainThreadDispather.m_ThreadId = Thread.CurrentThread.ManagedThreadId;
- MainThreadDispather.m_CurrentTime = Time.time;
- }
- }
-
- private void OnDestroy()
- {
- MainThreadDispather.m_Initialized = false;
- }
-
-
- public static void QueueOnMainThread(Action action)
- {
- MainThreadDispather.QueueOnMainThread(action, 0f);
- }
-
-
-
- public static void QueueOnMainThread(Action action, float time)
- {
- if (time != 0f)
- {
- List<MainThreadDispather.DelayedQueueItem> delayed = MainThreadDispather.Current.m_Delayed;
- lock (delayed)
- {
- MainThreadDispather.Current.m_Delayed.Add(new MainThreadDispather.DelayedQueueItem
- {
- time = m_CurrentTime + time,
- action = action
- });
- }
- }
- else
- {
- List<Action> actions = MainThreadDispather.Current.m_Actions;
- lock (actions)
- {
- MainThreadDispather.Current.m_Actions.Add(action);
- }
- }
- }
-
-
- public static void RunAsync(Action action)
- {
- new Thread(new ParameterizedThreadStart(MainThreadDispather.RunAction))
- {
- Priority = System.Threading.ThreadPriority.Normal
- }.Start(action);
- }
-
-
- private static void RunAction(object action)
- {
- ((Action)action)?.Invoke();
- }
-
- private void Update()
- {
- MainThreadDispather.m_CurrentTime = Time.time;
- List<Action> actions = this.m_Actions;
- if (actions.Count > 0)
- {
- lock (actions)
- {
- for (int i = 0; i < this.m_Actions.Count; i++)
- {
- this.m_Actions[i]();
- }
- this.m_Actions.Clear();
- }
- }
- List<MainThreadDispather.DelayedQueueItem> delayed = this.m_Delayed;
- if (delayed.Count > 0)
- {
- lock (delayed)
- {
- for (int j = 0; j < this.m_Delayed.Count; j++)
- {
- MainThreadDispather.DelayedQueueItem delayedQueueItem = this.m_Delayed[j];
- if (delayedQueueItem.time <= MainThreadDispather.m_CurrentTime)
- {
- delayedQueueItem.action();
- this.m_Delayed.RemoveAt(j);
- j--;
- }
- }
- }
- }
- }
- }
- }
|