When working with Unity, all Unity APIs, for example APIs you use to do UI updates, need to happen on the main thread. In the code we'll write however, we get callbacks on other threads. We want to update UI in these callbacks, so we need a way to go from a side thread onto the main thread. To execute code on the main thread from a side thread, we'll use the dispatcher pattern.
Let's add a member variable, dispatchQueue, which is a Queue of Actions. We will push Actions onto the queue, and then dequeue and run the Actions on the main thread.
/// <summary>/// Set this string to the Spatial Anchors account key provided in the Spatial Anchors resource./// </summary>protectedstring SpatialAnchorsAccountKey ="Set me";/// <summary>/// Our queue of actions that will be executed on the main thread./// </summary>privatereadonlyQueue<Action> dispatchQueue =newQueue<Action>();/// <summary>/// Use the recognizer to detect air taps./// </summary>privateGestureRecognizer recognizer;
Next, let's add method to add an Action to the Queue. Add QueueOnUpdate() right after Update() :
/// <summary>/// Queues the specified <seecref="Action"/> on update./// </summary>/// <paramname="updateAction">The update action.</param>protectedvoidQueueOnUpdate(Action updateAction){lock (dispatchQueue) {dispatchQueue.Enqueue(updateAction); }}
Let's now use the Update() loop to check if there is an Action queued. If so, we will dequeue the action and run it.
// Update is called once per framevoidUpdate(){lock (dispatchQueue) {if (dispatchQueue.Count>0) {dispatchQueue.Dequeue()(); } }}