chunk 1: core gameplay scripts scenes runtime assets
This commit is contained in:
39
Assets/Scripts/Utilities/UnityMainThreadDispatcher.cs
Normal file
39
Assets/Scripts/Utilities/UnityMainThreadDispatcher.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class UnityMainThreadDispatcher : MonoBehaviour
|
||||
{
|
||||
private static readonly Queue<Action> executionQueue = new Queue<Action>();
|
||||
|
||||
private static UnityMainThreadDispatcher instance = null;
|
||||
public static UnityMainThreadDispatcher Instance()
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
var obj = new GameObject("UnityMainThreadDispatcher");
|
||||
instance = obj.AddComponent<UnityMainThreadDispatcher>();
|
||||
DontDestroyOnLoad(obj);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void Enqueue(Action action)
|
||||
{
|
||||
lock (executionQueue)
|
||||
{
|
||||
executionQueue.Enqueue(action);
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
lock (executionQueue)
|
||||
{
|
||||
while (executionQueue.Count > 0)
|
||||
{
|
||||
executionQueue.Dequeue().Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user