You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
795 B
37 lines
795 B
using UnityEngine; |
|
using System.Collections.Generic; |
|
using System; |
|
|
|
public class UnityMainThreadDispatcher : MonoBehaviour |
|
{ |
|
private static readonly Queue<Action> _executionQueue = new Queue<Action>(); |
|
public static UnityMainThreadDispatcher Instance { get; private set; } |
|
|
|
void Awake() |
|
{ |
|
if (Instance == null) |
|
{ |
|
Instance = this; |
|
DontDestroyOnLoad(this.gameObject); |
|
} |
|
} |
|
|
|
void Update() |
|
{ |
|
lock (_executionQueue) |
|
{ |
|
while (_executionQueue.Count > 0) |
|
{ |
|
_executionQueue.Dequeue().Invoke(); |
|
} |
|
} |
|
} |
|
|
|
public void Enqueue(Action action) |
|
{ |
|
lock (_executionQueue) |
|
{ |
|
_executionQueue.Enqueue(action); |
|
} |
|
} |
|
} |