using System.Collections; using System.Collections.Generic; using UnityEngine; public class MainThreadDispatcher : MonoBehaviour { static Queue _coroutineQueue = new Queue(); static object _lock = new object(); public void Update() { while (_coroutineQueue.Count > 0) { StartCoroutine(_coroutineQueue.Dequeue()); } } public static void ExecuteCoroutineOnMainThread(IEnumerator coroutine) { lock (_lock) { _coroutineQueue.Enqueue(coroutine); } } }