Issue of unavailable thread context for main-thread-only unity functionalities when called from Namama Socket.ReceiveLoop() async events

The current answer by @novabyte:

You can use the synchronization context from the Unity main thread when you need to do it:

https://github.com/heroiclabs/nakama-unity/blob/master/Assets/Nakama/Snippets/NakamaManager.cs#L108

Alternatively you can use a dispatch queue to move messages from the socket thread onto the Unity main thread. Either pattern works.

update

I simply ended up using the second approach. I found a great repository on GitHub named UnityMainThreadDispatcher and all I have to do now inside my handler subscribed to the async event of Socket:

UnityMainThreadDispatcher.Instance().Enqueue(() => 
{
   //some code that needs to be run on unity's main thread 
});
1 Like