Hey all,

So I ran into a problem this week where Unity did not allow for a simple way of executing UI functions from different threads. This is quite annoying, especially if you’re working with certain multithreaded networking libraries that dispatch event handlers to different threads.

I made a quick fix for it that is easy to use and noob-friendly. It uses coroutines that are queued and executed on the main thread through a game object that is added to the scene.

You can use it like this:

public IEnumerator ThisWillBeExecutedOnTheMainThread() {
Debug.Log (“This is executed from the main thread”);
yield return null;
}

You can now just call this from anywhere:
UnityMainThreadDispatcher.Instance().Enqueue(ThisWillBeExecutedOnTheMainThread());

Simply head over to https://github.com/PimDeWitte/UnityMainThreadDispatcher and start using it if you’d like.

 

 

 

 

Advertisements