Skip to content

Commit a78a23f

Browse files
author
Pim De Witte
committed
Initial commit
1 parent c087b78 commit a78a23f

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

UnityMainThreadDispatcher.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System;
5+
6+
/// <summary>
7+
/// 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
8+
/// 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
9+
/// </summary>
10+
public class UnityMainThreadDispatcher : MonoBehaviour {
11+
12+
private readonly static Queue<Action> _executionQueue = new Queue<Action>();
13+
14+
public void Update() {
15+
while (_executionQueue.Count > 0)
16+
{
17+
_executionQueue.Dequeue().Invoke();
18+
}
19+
}
20+
21+
/// <summary>
22+
/// Locks the queue and adds the action to the queue
23+
/// </summary>
24+
/// <param name="action">IEnumerator function that will be executed from the main thread.</param>
25+
public void AddAction(IEnumerator action) {
26+
lock (_executionQueue) {
27+
_executionQueue.Enqueue (() => {
28+
StartCoroutine (action);
29+
});
30+
}
31+
}
32+
33+
34+
private static UnityMainThreadDispatcher _instance = null;
35+
private bool _isInitialized = false;
36+
37+
public static bool Exists() {
38+
return _instance != null;
39+
}
40+
41+
public static UnityMainThreadDispatcher Instance() {
42+
if (!Exists ()) {
43+
throw new Exception ("UnityMainThreadDispatcher could not find the UnityMainThreadDispatcher object. Please ensure you have added the MainThreadExecutor Prefab to your scene.");
44+
}
45+
return _instance;
46+
}
47+
48+
49+
void Awake() {
50+
if (_instance == null) {
51+
_instance = this;
52+
DontDestroyOnLoad(this.gameObject);
53+
}
54+
}
55+
56+
void OnDestroy() {
57+
_instance = null;
58+
}
59+
60+
61+
}
62+
63+

UnityMainThreadDispatcher.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityMainThreadDispatcher.prefab

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!1 &184210
4+
GameObject:
5+
m_ObjectHideFlags: 0
6+
m_PrefabParentObject: {fileID: 0}
7+
m_PrefabInternal: {fileID: 100100000}
8+
serializedVersion: 4
9+
m_Component:
10+
- 224: {fileID: 22414968}
11+
- 114: {fileID: 11464956}
12+
m_Layer: 0
13+
m_Name: UnityMainThreadDispatcher
14+
m_TagString: Untagged
15+
m_Icon: {fileID: 0}
16+
m_NavMeshLayer: 0
17+
m_StaticEditorFlags: 0
18+
m_IsActive: 1
19+
--- !u!114 &11464956
20+
MonoBehaviour:
21+
m_ObjectHideFlags: 1
22+
m_PrefabParentObject: {fileID: 0}
23+
m_PrefabInternal: {fileID: 100100000}
24+
m_GameObject: {fileID: 184210}
25+
m_Enabled: 1
26+
m_EditorHideFlags: 0
27+
m_Script: {fileID: 11500000, guid: 16173428f7358476fa784f899e396692, type: 3}
28+
m_Name:
29+
m_EditorClassIdentifier:
30+
--- !u!224 &22414968
31+
RectTransform:
32+
m_ObjectHideFlags: 1
33+
m_PrefabParentObject: {fileID: 0}
34+
m_PrefabInternal: {fileID: 100100000}
35+
m_GameObject: {fileID: 184210}
36+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
37+
m_LocalPosition: {x: 0, y: 0, z: 0}
38+
m_LocalScale: {x: 0, y: 0, z: 0}
39+
m_Children: []
40+
m_Father: {fileID: 0}
41+
m_RootOrder: 0
42+
m_AnchorMin: {x: 0.5, y: 0.5}
43+
m_AnchorMax: {x: 0.5, y: 0.5}
44+
m_AnchoredPosition: {x: -50, y: -50}
45+
m_SizeDelta: {x: 100, y: 100}
46+
m_Pivot: {x: 0, y: 0}
47+
--- !u!1001 &100100000
48+
Prefab:
49+
m_ObjectHideFlags: 1
50+
serializedVersion: 2
51+
m_Modification:
52+
m_TransformParent: {fileID: 0}
53+
m_Modifications: []
54+
m_RemovedComponents: []
55+
m_ParentPrefab: {fileID: 0}
56+
m_RootGameObject: {fileID: 184210}
57+
m_IsPrefabParent: 1

UnityMainThreadDispatcher.prefab.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)