Unity 3D – Multithreading

In Unity3D, you will sometimes need to synchronize asynchronous tasks, that is, waiting for response from an API, a Web service.

Unity uses the concept of Coroutine to satisfy this goal.

Here is an example of Coroutine :

IEnumerator MyCoroutine  ()
    // Before Asynchronous Task
    yield return new WaitForSeconds(1.0f);
    yield return lock;
    // After Asynchronous Task

A common problem is how to synchronize those Coroutines ?

And better how to communicate a result back to client classes ?

This simple pattern should be followed :

– Client calls the class API wrapper function with a callback parameter (to update the View)
– Wrapper function ends while it’s Coroutine(callback) waits for the result
– Coroutine send back the result to client callback function

Leave a Reply

Your email address will not be published. Required fields are marked *