-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathAdjustThreadDispatcher.cs
49 lines (44 loc) · 1.14 KB
/
AdjustThreadDispatcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using UnityEngine;
public class AdjustThreadDispatcher : MonoBehaviour
{
private static readonly Queue<Action> executionQueue = new Queue<Action>();
private static AdjustThreadDispatcher instance;
public static void RunOnMainThread(Action action)
{
if (action == null)
{
return;
}
lock (executionQueue)
{
executionQueue.Enqueue(action);
}
}
private void Update()
{
while (executionQueue.Count > 0)
{
Action action;
lock (executionQueue)
{
action = executionQueue.Dequeue();
}
if (action != null)
{
action.Invoke();
}
}
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void Initialize()
{
if (instance == null)
{
GameObject obj = new GameObject("AdjustThreadDispatcher");
instance = obj.AddComponent<AdjustThreadDispatcher>();
DontDestroyOnLoad(obj);
}
}
}