concept coroutine in category c#

appears as: coroutine, Coroutines, coroutines
Unity in Action: Multiplatform game development in C# with Unity 5

This is an excerpt from Manning's book Unity in Action: Multiplatform game development in C# with Unity 5.

Coroutines are a Unity-specific way of handling tasks that execute incrementally over time, as opposed to how most functions make the program wait until they finish.

Coroutines can work with the WWW class to wait for the request to complete. Coroutines were first introduced back in chapter 3, where we used them to pause some code for a set period of time. Recall the explanation given there: coroutines are special functions that seemingly run in the background of a program, in a repeated cycle of running partway and then returning to the rest of the program. When used along with the StartCoroutine() method, the yield keyword causes the coroutine to temporarily pause, handing back the program flow and picking up again from that point next frame.

In chapter 3 the coroutines yielded at WaitForSeconds(), an object that caused the function to pause for a specific number of seconds. Yielding a coroutine with WWW will pause the function until that network request completes. The program flow here is similar to making asynchronous Ajax calls in a web application: first you send a request, then you continue with the rest of the program, and after some time you receive a response.