com.wallstop-studios.unity-helpers 2.0.0-rc53 → 2.0.0-rc54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -2,18 +2,66 @@
|
|
|
2
2
|
{
|
|
3
3
|
using System;
|
|
4
4
|
using System.Collections.Concurrent;
|
|
5
|
+
using UnityEditor;
|
|
5
6
|
using UnityEngine;
|
|
6
7
|
using Utils;
|
|
7
8
|
|
|
9
|
+
[ExecuteAlways]
|
|
8
10
|
public sealed class UnityMainThreadDispatcher : RuntimeSingleton<UnityMainThreadDispatcher>
|
|
9
11
|
{
|
|
10
12
|
private readonly ConcurrentQueue<Action> _actions = new();
|
|
11
13
|
|
|
14
|
+
#if UNITY_EDITOR
|
|
15
|
+
private readonly EditorApplication.CallbackFunction _update;
|
|
16
|
+
private bool _attachedEditorUpdate;
|
|
17
|
+
#endif
|
|
18
|
+
|
|
19
|
+
public UnityMainThreadDispatcher()
|
|
20
|
+
{
|
|
21
|
+
#if UNITY_EDITOR
|
|
22
|
+
_update = Update;
|
|
23
|
+
#endif
|
|
24
|
+
}
|
|
25
|
+
|
|
12
26
|
public void RunOnMainThread(Action action)
|
|
13
27
|
{
|
|
14
28
|
_actions.Enqueue(action);
|
|
15
29
|
}
|
|
16
30
|
|
|
31
|
+
private void OnEnable()
|
|
32
|
+
{
|
|
33
|
+
#if UNITY_EDITOR
|
|
34
|
+
if (!_attachedEditorUpdate)
|
|
35
|
+
{
|
|
36
|
+
EditorApplication.update += _update;
|
|
37
|
+
_attachedEditorUpdate = true;
|
|
38
|
+
}
|
|
39
|
+
#endif
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private void OnDisable()
|
|
43
|
+
{
|
|
44
|
+
#if UNITY_EDITOR
|
|
45
|
+
if (_attachedEditorUpdate)
|
|
46
|
+
{
|
|
47
|
+
EditorApplication.update -= _update;
|
|
48
|
+
_attachedEditorUpdate = false;
|
|
49
|
+
}
|
|
50
|
+
#endif
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
protected override void OnDestroy()
|
|
54
|
+
{
|
|
55
|
+
#if UNITY_EDITOR
|
|
56
|
+
if (_attachedEditorUpdate)
|
|
57
|
+
{
|
|
58
|
+
EditorApplication.update -= _update;
|
|
59
|
+
_attachedEditorUpdate = false;
|
|
60
|
+
}
|
|
61
|
+
#endif
|
|
62
|
+
base.OnDestroy();
|
|
63
|
+
}
|
|
64
|
+
|
|
17
65
|
private void Update()
|
|
18
66
|
{
|
|
19
67
|
while (_actions.TryDequeue(out Action action))
|