Version: 2020.2
public Coroutine StartCoroutine (IEnumerator routine);

描述

启动协程。

可以使用 yield 语句,随时暂停协程的执行。使用 yield 语句时,协程会暂停执行,并在下一帧自动恢复。请参阅协程文档以了解更多详细信息。

协程非常适合用于在若干帧中对行为建模。StartCoroutine 方法在第一个 yield 返回时返回,不过可以生成结果,这会等到协程完成执行。即使多个协程在同一帧中完成,也不能保证它们按照与启动相同的顺序结束。

生成的任何类型(包括 null)都会导致执行在后面的帧返回,除非协程已停止或完成。

注意:可以使用 MonoBehaviour.StopCoroutine 和 MonoBehaviour.StopAllCoroutines 停止协程。销毁 MonoBehaviour 时,或是如果 MonoBehaviour 所附加到的 GameObject 已禁用,也会停止协程。 禁用 MonoBehaviour 时,不会停止协程。

另请参阅:CoroutineYieldInstruction

StartCoroutine 的示例:

using UnityEngine;
using System.Collections;

// In this example we show how to invoke a coroutine and // continue executing the function in parallel.

public class ExampleClass : MonoBehaviour { // In this example we show how to invoke a coroutine and // continue executing the function in parallel.

private IEnumerator coroutine;

void Start() { // - After 0 seconds, prints "Starting 0.0" // - After 0 seconds, prints "Before WaitAndPrint Finishes 0.0" // - After 2 seconds, prints "WaitAndPrint 2.0" print("Starting " + Time.time);

// Start function WaitAndPrint as a coroutine.

coroutine = WaitAndPrint(2.0f); StartCoroutine(coroutine);

print("Before WaitAndPrint Finishes " + Time.time); }

// every 2 seconds perform the print() private IEnumerator WaitAndPrint(float waitTime) { while (true) { yield return new WaitForSeconds(waitTime); print("WaitAndPrint " + Time.time); } } }

另一个示例:

// In this example we show how to invoke a coroutine and wait until it
// is completed

using UnityEngine; using System.Collections;

public class ExampleClass : MonoBehaviour { IEnumerator Start() { // - After 0 seconds, prints "Starting 0.0" // - After 2 seconds, prints "WaitAndPrint 2.0" // - After 2 seconds, prints "Done 2.0" print("Starting " + Time.time);

// Start function WaitAndPrint as a coroutine. And wait until it is completed. // the same as yield return WaitAndPrint(2.0f); yield return StartCoroutine(WaitAndPrint(2.0f)); print("Done " + Time.time); }

// suspend execution for waitTime seconds IEnumerator WaitAndPrint(float waitTime) { yield return new WaitForSeconds(waitTime); print("WaitAndPrint " + Time.time); } }

public Coroutine StartCoroutine (string methodName, object value= null);

描述

启动一个名为 methodName 的协同程序。

在大多数情况下,要使用上面的 StartCoroutine 变体。 但是,使用字符串方法名称的 StartCoroutine 让你能够使用具有特定方法名称的 StopCoroutine。 缺点是字符串版本在启动协程时的运行时开销更高,并且只能传递一个参数。

// In this example we show how to invoke a coroutine using a string name and stop it.

using UnityEngine; using System.Collections;

public class ExampleClass : MonoBehaviour { IEnumerator Start() { StartCoroutine("DoSomething", 2.0f); yield return new WaitForSeconds(1); StopCoroutine("DoSomething"); }

IEnumerator DoSomething(float someParameter) { while (true) { print("DoSomething Loop");

// Yield execution of this coroutine and return to the main loop until next frame yield return null; } } }

已创建的协程可以启动另一个协程。这两个协程能按多种方式共同运行。这包括并行运行两个协程。或者,一个协程可以让另一协程停止,而自己继续运行。以下脚本示例说明一个协程在启动另一协程时暂停。第二个协程结束后,将重新启动第一个协程。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ExampleClass : MonoBehaviour { void Start() { StartCoroutine(coroutineA()); }

IEnumerator coroutineA() { // wait for 1 second Debug.Log("coroutineA created"); yield return new WaitForSeconds(1.0f); yield return StartCoroutine(coroutineB()); Debug.Log("coroutineA running again"); }

IEnumerator coroutineB() { Debug.Log("coroutineB created"); yield return new WaitForSeconds(2.5f); Debug.Log("coroutineB enables coroutineA to run"); } }