Coroutine

85 阅读1分钟

Coroutine 运作机制

medium.com/feis-studio…

forum.unity.com/threads/why…

yield return后面的条件,全部都是继承自YieldInstruction这个类,包括AsyncOpreation,WaitForSeconds,WaitForFixedUpdate,coroutine等等Unity内置的条件。用户也可以自己定义条件,这需要继承自CustomYieldInstruction,或者IEnumerator。 这里需要着重说一下条件Coroutine,每次我们调用StartCoroutine去开启一个协程,它都会返回一个Coroutine类型的对象,用于实时的跟踪协程的处理状态,因为继承自YieldInstruction,所以可以作为yield return的条件。

nested coroutine:

forum.unity.com/threads/cor…

www.alanzucconi.com/2017/02/15/…

对内嵌协程而言,有两种形式调用:

  1. yield return StartCoroutine(*** ()) 或者 Coroutine coroutine = StartCoroutine();yield return coroutine;
  2. yield return *** () ;

这两种语法都可以在一个协程中开启一个新的协程,可以看成是等价的。

CustomYieldInstruction:

medium.com/feis-studio…

docs.unity3d.com/ScriptRefer…