I've come across some articles about using coroutine to run functions on main thread, such as Thread Ninja:
IEnumerator StartExamples() { //Jump to main thread yield return Ninja.JumpToUnity; Destroy(someGameObject); //OK //Jump back to background thread yield return Ninja.JumpBack; } It didn't work on my code since I wanted to jump to main thread from a socket listener.
SocketIOClient.Client socket; IEnumerator StartExamples() { socket.On("connect", (fn) => { WriteLine("\r\nConnected event...\r\n"); //Jump to main thread yield return Ninja.JumpToUnity; //ERROR: couldn't do a yield return Destroy(someGameObject); //Jump back to background thread yield return Ninja.JumpBack; //ERROR: couldn't do a yield return }); } So do we have any workaround for this? I wish I could jump to main thread like this:
Dispatcher.BeginInvoke(() => { //Unity API Destroy(gameObject); });