123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- /****************************************************************************
- * Copyright 2019 Nreal Techonology Limited. All rights reserved.
- *
- * This file is part of NRSDK.
- *
- * https://www.nreal.ai/
- *
- *****************************************************************************/
- namespace NRKernal
- {
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using UnityEngine;
- /// <summary> A main thread dispather. </summary>
- [ExecuteInEditMode]
- public class MainThreadDispather : MonoBehaviour
- {
- /// <summary> A delayed queue item. </summary>
- public class DelayedQueueItem
- {
- /// <summary> The time. </summary>
- public float time;
- /// <summary> The action. </summary>
- public Action action;
- }
- /// <summary> The current. </summary>
- private static MainThreadDispather m_Current;
- /// <summary> Number of. </summary>
- private int m_Count;
- /// <summary> Current time. </summary>
- private static float m_CurrentTime;
- /// <summary> True once initialization is complete. </summary>
- private static bool m_Initialized;
- /// <summary> Identifier for the thread. </summary>
- private static int m_ThreadId = -1;
- /// <summary> The actions. </summary>
- private List<Action> m_Actions = new List<Action>();
- /// <summary> The delayed. </summary>
- private List<MainThreadDispather.DelayedQueueItem> m_Delayed = new List<MainThreadDispather.DelayedQueueItem>();
- /// <summary> Gets the current. </summary>
- /// <value> The current. </value>
- public static MainThreadDispather Current
- {
- get
- {
- if (!MainThreadDispather.m_Initialized)
- {
- MainThreadDispather.Initialize();
- }
- return MainThreadDispather.m_Current;
- }
- }
- /// <summary> Initializes this object. </summary>
- 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;
- }
- }
- /// <summary> Executes the 'destroy' action. </summary>
- private void OnDestroy()
- {
- MainThreadDispather.m_Initialized = false;
- }
- /// <summary> Queue on main thread. </summary>
- /// <param name="action"> The action.</param>
- public static void QueueOnMainThread(Action action)
- {
- MainThreadDispather.QueueOnMainThread(action, 0f);
- }
- /// <summary> Queue on main thread. </summary>
- /// <param name="action"> The action.</param>
- /// <param name="time"> The time.</param>
- 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);
- }
- }
- }
- /// <summary> Executes the 'asynchronous' operation. </summary>
- /// <param name="action"> The action.</param>
- public static void RunAsync(Action action)
- {
- new Thread(new ParameterizedThreadStart(MainThreadDispather.RunAction))
- {
- Priority = System.Threading.ThreadPriority.Normal
- }.Start(action);
- }
- /// <summary> Executes the action. </summary>
- /// <param name="action"> The action.</param>
- private static void RunAction(object action)
- {
- ((Action)action)?.Invoke();
- }
- /// <summary> Updates this object. </summary>
- 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--;
- }
- }
- }
- }
- }
- }
- }
|