How to update the UI when a callback resolves?
Last updated
Was this helpful?
Last updated
Was this helpful?
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 .
Let's add a , 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.
Next, let's add method to add an Action to the Queue. Add QueueOnUpdate()
right after Update()
:
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.