Hey, U3D coroutine
During Game Development, we often encounter this situation. when specific conditions are met, some behaviors are triggered and a series of code is executed. You can use coroutine when you encounter such problems that require wake-up operations.
Features:
Conditional callback code.
Example:
Private void BeginCoroutine ()
{
Debug. Log ("coroutine before ");
StartCoroutine (DoSomethings ());
Debug. Log ("coroutine after ");
}
Private IEnumerator DoSomethings ()
{
Debug. Log ("doing ");
Yield return 0;
Debug. Log ("done ");
}
Running result:
Coroutine before
Doing
Coroutine after
Done
The example is very simple. From the running result, we can see that the DoSomethings function immediately executes the coroutine after code segment after running doing.
Brief Analysis of implementation process
1. Execute the function through StartCoroutine to start the coroutine
2. embed the yield block in the function. The block must be of the IEnumerator iterator return type.
Below, we often see several obfuscation points:
1. coroutine is executed on the main thread and is not blocked.
2. Once again, yield can only appear in the iterator Block