43 lines
949 B
C#
43 lines
949 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
//async
|
|
public class UnityMainThreadDispatcher : MonoBehaviour
|
|
{
|
|
private static UnityMainThreadDispatcher instance;
|
|
private readonly Queue<Action> actions = new Queue<Action>();
|
|
|
|
public static UnityMainThreadDispatcher Instance
|
|
{
|
|
get
|
|
{
|
|
if (instance == null)
|
|
{
|
|
var go = new GameObject("MainThreadDispatcher");
|
|
instance = go.AddComponent<UnityMainThreadDispatcher>();
|
|
DontDestroyOnLoad(go);
|
|
}
|
|
return instance;
|
|
}
|
|
}
|
|
|
|
public void Enqueue(Action action)
|
|
{
|
|
lock (actions)
|
|
{
|
|
actions.Enqueue(action);
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
lock (actions)
|
|
{
|
|
while (actions.Count > 0)
|
|
{
|
|
actions.Dequeue()?.Invoke();
|
|
}
|
|
}
|
|
}
|
|
} |