46 lines
1.1 KiB
C#
46 lines
1.1 KiB
C#
using System.Collections.Concurrent;
|
|
using UnityEngine;
|
|
using System;
|
|
|
|
public class UnityMainThreadDispatcher : MonoBehaviour
|
|
{
|
|
private static UnityMainThreadDispatcher instance;
|
|
private readonly ConcurrentQueue<Action> actionQueue = new ConcurrentQueue<Action>();
|
|
|
|
|
|
public static void Initialize()
|
|
{
|
|
if (instance == null)
|
|
{
|
|
var go = new GameObject("UnityMainThreadDispatcher");
|
|
DontDestroyOnLoad(go);
|
|
instance = go.AddComponent<UnityMainThreadDispatcher>();
|
|
}
|
|
}
|
|
public static UnityMainThreadDispatcher Instance
|
|
{
|
|
get
|
|
{
|
|
if (instance == null)
|
|
{
|
|
var go = new GameObject("UnityMainThreadDispatcher");
|
|
DontDestroyOnLoad(go);
|
|
instance = go.AddComponent<UnityMainThreadDispatcher>();
|
|
}
|
|
return instance;
|
|
}
|
|
}
|
|
|
|
public void Enqueue(Action action)
|
|
{
|
|
actionQueue.Enqueue(action);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
while (actionQueue.TryDequeue(out var action))
|
|
{
|
|
action();
|
|
}
|
|
}
|
|
} |