Unity Double Layered Coroutines: A Complete Guide
To use double layered coroutines in Unity, you need to follow these steps:
-
Create two coroutines: one as the 'inner coroutine' and the other as the 'outer coroutine'.
-
In the outer coroutine, start the inner coroutine using the StartCoroutine() method.
-
In the inner coroutine, add a yield statement that pauses the coroutine execution for a certain amount of time.
-
Once the yield statement is completed, return the coroutine execution back to the outer coroutine by using the yield return statement.
-
In the outer coroutine, add another yield statement that pauses the coroutine execution for a certain amount of time.
-
Repeat steps 3-5 as many times as you need.
Here is an example of a double layered coroutine in Unity:
IEnumerator InnerCoroutine()
{
yield return new WaitForSeconds(1f);
yield return null;
}
IEnumerator OuterCoroutine()
{
while(true)
{
yield return StartCoroutine(InnerCoroutine());
yield return new WaitForSeconds(2f);
}
}
To start the outer coroutine, call the StartCoroutine() method:
StartCoroutine(OuterCoroutine());
This code will run the InnerCoroutine() every 2 seconds and pause for 1 second within the InnerCoroutine().
原文地址: https://www.cveoy.top/t/topic/lNoZ 著作权归作者所有。请勿转载和采集!