Unity 协程的执行逻辑

〇、

  1. 同步等待
  2. 异步协程
  3. 同步协程
  4. 并行协程

一、

同步等待是指,当调用了 yield return yieldInstruction 时,会根据不同的指令的不同,暂时挂起( suspend )协程,等待条件满足后,继续执行协程。

yield return new WaitForSeconds(0.1f);

同步等待的示意图如下:

Unity 协程的执行逻辑_第1张图片

二、

void Start()
{
    StartCoroutine(TestCoroutine());
}

IEnumerator TestCoroutine()
{
    print(this + "On TestCoroutine.");
    yield return new WaitForSeconds(0.1f);
    print("yield return 0.1 and start intextcoroutine On TestCoroutine.");
    StartCoroutine(InTestCoroutine());
    yield return new WaitForSeconds(0.1f);
    print("yield return 0.1 On TestCoroutine. And finish this coroutine.");
}

IEnumerator InTestCoroutine()
{
    print(this + "On InTestCoroutine.");
    yield return new WaitForSeconds(0.1f);
    print("yield return 0.1 On InTestCoroutine.");
    yield return new WaitForSeconds(0.1f);
    print("yield return 0.1 On InTestCoroutine.");
    yield return new WaitForSeconds(0.1f);
    print("yield return 0.1 On InTestCoroutine. And finish this coroutine.");
}

在协程中开启新协程,两个协程并行执行。即异步( Asynchronous )执行。新的协程并未阻塞旧协程,而是各自执行到结束。

Unity 协程的执行逻辑_第2张图片

异步协程的示意图如下:

Unity 协程的执行逻辑_第3张图片

三、

    IEnumerator TestCoroutine()
    {
        print(this + "On TestCoroutine.");
        yield return new WaitForSeconds(0.1f);
        print("yield return 0.1 and start intextcoroutine On TestCoroutine.");
        yield return StartCoroutine(InTestCoroutine());
        yield return new WaitForSeconds(0.1f);
        print("yield return 0.1 On TestCoroutine.");
        yield return new WaitForSeconds(0.1f);
        print("yield return 0.1 On TestCoroutine. And finish this coroutine.");
    }

修改 TextCoroutine 中的代码,将启动新协程的命令修改为 yield return StartCoroutine(),此时,在执行程序后,旧的协程会在新协程执行结束后执行。

Unity 协程的执行逻辑_第4张图片

同步协程的示意图如下:

Unity 协程的执行逻辑_第5张图片

四、

IEnumerator TestCoroutine()
{
    print(this + "On TestCoroutine.");
    yield return new WaitForSeconds(0.1f);
    print("yield return 0.1 and start intextcoroutine On TestCoroutine.");
    Coroutine a = StartCoroutine(InTestCoroutine(0.4f));
    var b = StartCoroutine(InTestCoroutine(0.8f));
    var c = StartCoroutine(InTestCoroutine(1.2f));
    yield return new WaitForSeconds(0.1f);
    print("yield return 0.1 On TestCoroutine.");
    yield return a;
    yield return b;
    yield return c;
    print("yield return 0.1 On TestCoroutine. And finish this coroutine.");
}

IEnumerator InTestCoroutine(float ft)
{
    print(this + "On InTestCoroutine(" + ft + ").");
    yield return new WaitForSeconds(0.1f);
    print("yield return 0.1 On InTestCoroutine(" + ft + ").");
    yield return new WaitForSeconds(0.1f);
    print("yield return 0.1 On InTestCoroutine(" + ft + ").");
    yield return new WaitForSeconds(ft);
    print("yield return " + ft + " On InTestCoroutine(" + ft + "). And finish this coroutine.");
}

将开启的协程保存在 UnityEngine 定义的 Coroutine 对象中,可以据此查询协程的执行状态。这里启动的每个协程对于旧的协程都是异步执行的,而这些新协程在同一刻开始并行执行,到了最后,让旧的协程同步等待新协程执行完毕后,才结束自己的执行。

Unity 协程的执行逻辑_第6张图片

并行协程的示意图如下:

Unity 协程的执行逻辑_第7张图片

你可能感兴趣的:(笔记)