Member-only story
Coroutines are a new way to write code in Unity that allows you to execute tasks in parallel. They are similar to regular functions, but they can be suspended and resumed at any time.
Coroutines in Unity are based on the C# 7.1 library. They are implemented using a context-switching mechanism that allows Unity to switch between different tasks in the background.
How coroutines work?
Coroutines work by using a context-switching mechanism to suspend and resume the execution of a task at any time. When a coroutine is suspended, it is placed in a queue of suspended tasks. When a coroutine is resumed, it is removed from the queue and executed.
The context-switching mechanism is implemented using a thread pool. The thread pool contains a number of threads that are available to execute coroutines. When a coroutine is suspended, it is removed from the current thread and placed in the thread.
Advantages of using coroutines
Coroutines have the following advantages:
- Parallelism: Coroutines allow you to execute tasks in parallel, which can improve the performance of your application.
- Ease of use: Coroutines are easy to use and similar to regular functions.