using UnityEngine;
using System.Collections.Concurrent;
using System;

public class UnityMainThreadDispatcher : MonoBehaviour
{
    private static readonly ConcurrentQueue<Action> ExecutionQueue = new ConcurrentQueue<Action>();

    public static void Enqueue(Action action)
    {
        ExecutionQueue.Enqueue(action);
    }

    void Update()
    {
        while (ExecutionQueue.TryDequeue(out var action))
        {
            action.Invoke();
        }
    }
}
