Skip to content

Commit 1ad4ddc

Browse files
authored
Merge pull request PimDeWitte#30 from SGoerzen/master
Package with asmdef and namespace
2 parents 7406433 + 13415a4 commit 1ad4ddc

File tree

2 files changed

+90
-77
lines changed

2 files changed

+90
-77
lines changed
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
{
2-
"name": "PimDeWitte.UnityMainThreadDispatcher"
2+
"name": "PimDeWitte.UnityMainThreadDispatcher",
3+
"rootNamespace": "PimDeWitte.UnityMainThreadDispatcher",
4+
"references": [],
5+
"includePlatforms": [],
6+
"excludePlatforms": [],
7+
"allowUnsafeCode": false,
8+
"overrideReferences": false,
9+
"precompiledReferences": [],
10+
"autoReferenced": true,
11+
"defineConstraints": [],
12+
"versionDefines": [],
13+
"noEngineReferences": false
314
}

Runtime/UnityMainThreadDispatcher.cs

Lines changed: 78 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -20,100 +20,102 @@ limitations under the License.
2020
using System;
2121
using System.Threading.Tasks;
2222

23-
/// Author: Pim de Witte (pimdewitte.com) and contributors, https://github.com/PimDeWitte/UnityMainThreadDispatcher
24-
/// <summary>
25-
/// A thread-safe class which holds a queue with actions to execute on the next Update() method. It can be used to make calls to the main thread for
26-
/// things such as UI Manipulation in Unity. It was developed for use in combination with the Firebase Unity plugin, which uses separate threads for event handling
27-
/// </summary>
28-
public class UnityMainThreadDispatcher : MonoBehaviour {
29-
30-
private static readonly Queue<Action> _executionQueue = new Queue<Action>();
31-
32-
public void Update() {
33-
lock(_executionQueue) {
34-
while (_executionQueue.Count > 0) {
35-
_executionQueue.Dequeue().Invoke();
23+
namespace PimDeWitte.UnityMainThreadDispatcher {
24+
/// Author: Pim de Witte (pimdewitte.com) and contributors, https://github.com/PimDeWitte/UnityMainThreadDispatcher
25+
/// <summary>
26+
/// A thread-safe class which holds a queue with actions to execute on the next Update() method. It can be used to make calls to the main thread for
27+
/// things such as UI Manipulation in Unity. It was developed for use in combination with the Firebase Unity plugin, which uses separate threads for event handling
28+
/// </summary>
29+
public class UnityMainThreadDispatcher : MonoBehaviour {
30+
31+
private static readonly Queue<Action> _executionQueue = new Queue<Action>();
32+
33+
public void Update() {
34+
lock(_executionQueue) {
35+
while (_executionQueue.Count > 0) {
36+
_executionQueue.Dequeue().Invoke();
37+
}
3638
}
3739
}
38-
}
3940

40-
/// <summary>
41-
/// Locks the queue and adds the IEnumerator to the queue
42-
/// </summary>
43-
/// <param name="action">IEnumerator function that will be executed from the main thread.</param>
44-
public void Enqueue(IEnumerator action) {
45-
lock (_executionQueue) {
46-
_executionQueue.Enqueue (() => {
47-
StartCoroutine (action);
48-
});
41+
/// <summary>
42+
/// Locks the queue and adds the IEnumerator to the queue
43+
/// </summary>
44+
/// <param name="action">IEnumerator function that will be executed from the main thread.</param>
45+
public void Enqueue(IEnumerator action) {
46+
lock (_executionQueue) {
47+
_executionQueue.Enqueue (() => {
48+
StartCoroutine (action);
49+
});
50+
}
4951
}
50-
}
5152

52-
/// <summary>
53-
/// Locks the queue and adds the Action to the queue
54-
/// </summary>
55-
/// <param name="action">function that will be executed from the main thread.</param>
56-
public void Enqueue(Action action)
57-
{
58-
Enqueue(ActionWrapper(action));
59-
}
60-
61-
/// <summary>
62-
/// Locks the queue and adds the Action to the queue, returning a Task which is completed when the action completes
63-
/// </summary>
64-
/// <param name="action">function that will be executed from the main thread.</param>
65-
/// <returns>A Task that can be awaited until the action completes</returns>
66-
public Task EnqueueAsync(Action action)
67-
{
68-
var tcs = new TaskCompletionSource<bool>();
69-
70-
void WrappedAction() {
71-
try
72-
{
73-
action();
74-
tcs.TrySetResult(true);
75-
} catch (Exception ex)
76-
{
77-
tcs.TrySetException(ex);
53+
/// <summary>
54+
/// Locks the queue and adds the Action to the queue
55+
/// </summary>
56+
/// <param name="action">function that will be executed from the main thread.</param>
57+
public void Enqueue(Action action)
58+
{
59+
Enqueue(ActionWrapper(action));
60+
}
61+
62+
/// <summary>
63+
/// Locks the queue and adds the Action to the queue, returning a Task which is completed when the action completes
64+
/// </summary>
65+
/// <param name="action">function that will be executed from the main thread.</param>
66+
/// <returns>A Task that can be awaited until the action completes</returns>
67+
public Task EnqueueAsync(Action action)
68+
{
69+
var tcs = new TaskCompletionSource<bool>();
70+
71+
void WrappedAction() {
72+
try
73+
{
74+
action();
75+
tcs.TrySetResult(true);
76+
} catch (Exception ex)
77+
{
78+
tcs.TrySetException(ex);
79+
}
7880
}
81+
82+
Enqueue(ActionWrapper(WrappedAction));
83+
return tcs.Task;
7984
}
8085

81-
Enqueue(ActionWrapper(WrappedAction));
82-
return tcs.Task;
83-
}
8486

85-
86-
IEnumerator ActionWrapper(Action a)
87-
{
88-
a();
89-
yield return null;
90-
}
87+
IEnumerator ActionWrapper(Action a)
88+
{
89+
a();
90+
yield return null;
91+
}
9192

9293

93-
private static UnityMainThreadDispatcher _instance = null;
94+
private static UnityMainThreadDispatcher _instance = null;
9495

95-
public static bool Exists() {
96-
return _instance != null;
97-
}
96+
public static bool Exists() {
97+
return _instance != null;
98+
}
9899

99-
public static UnityMainThreadDispatcher Instance() {
100-
if (!Exists ()) {
101-
throw new Exception ("UnityMainThreadDispatcher could not find the UnityMainThreadDispatcher object. Please ensure you have added the MainThreadExecutor Prefab to your scene.");
100+
public static UnityMainThreadDispatcher Instance() {
101+
if (!Exists ()) {
102+
throw new Exception ("UnityMainThreadDispatcher could not find the UnityMainThreadDispatcher object. Please ensure you have added the MainThreadExecutor Prefab to your scene.");
103+
}
104+
return _instance;
102105
}
103-
return _instance;
104-
}
105106

106107

107-
void Awake() {
108-
if (_instance == null) {
109-
_instance = this;
110-
DontDestroyOnLoad(this.gameObject);
108+
void Awake() {
109+
if (_instance == null) {
110+
_instance = this;
111+
DontDestroyOnLoad(this.gameObject);
112+
}
111113
}
112-
}
113114

114-
void OnDestroy() {
115-
_instance = null;
116-
}
115+
void OnDestroy() {
116+
_instance = null;
117+
}
117118

118119

120+
}
119121
}

0 commit comments

Comments
 (0)