To use double layered coroutines in Unity, you need to follow these steps:

  1. Create two coroutines: one as the 'inner coroutine' and the other as the 'outer coroutine'.

  2. In the outer coroutine, start the inner coroutine using the StartCoroutine() method.

  3. In the inner coroutine, add a yield statement that pauses the coroutine execution for a certain amount of time.

  4. Once the yield statement is completed, return the coroutine execution back to the outer coroutine by using the yield return statement.

  5. In the outer coroutine, add another yield statement that pauses the coroutine execution for a certain amount of time.

  6. 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 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录