| 123456789101112131415161718192021222324252627282930313233343536 |
- using System.Collections.Generic;
- using UnityEngine;
- public class UnityMainThreadDispatcher : MonoBehaviour
- {
- private static readonly Queue<System.Action> _executionQueue = new Queue<System.Action>();
- private static UnityMainThreadDispatcher _instance;
- public static UnityMainThreadDispatcher Instance()
- {
- if (_instance == null)
- {
- GameObject go = new GameObject("MainThreadDispatcher");
- _instance = go.AddComponent<UnityMainThreadDispatcher>();
- DontDestroyOnLoad(go);
- }
- return _instance;
- }
- public void Enqueue(System.Action action)
- {
- lock (_executionQueue)
- {
- _executionQueue.Enqueue(action);
- }
- }
- private void Update()
- {
- while (_executionQueue.Count > 0)
- {
- var action = _executionQueue.Dequeue();
- action();
- }
- }
- }
|