启动一个协同程序。
可以使用 yield 语句在任意执行点暂停协同程序的执行。
yield 返回值指定何时恢复该协同程序。
当在多个帧上对行为建模时,协同程序是您的理想选择。
协同程序几乎没有性能开销。
StartCoroutine 函数始终立即返回,但您可以 yield 结果。
这将等待该协同程序执行完毕。无法保证协同程序按其启动顺序结束,
即使它们在同一帧中完成也是如此。
使用 JavaScript 时,没有必要使用 StartCoroutine,编译器会为您执行该操作。
编写 C# 代码时,必须手动调用 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);
}
}
}
另一个示例:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
IEnumerator Start() {
print("Starting " + Time.time);
yield return StartCoroutine(WaitAndPrint(2.0F));
print("Done " + Time.time);
}
IEnumerator WaitAndPrint(float waitTime) {
yield return new WaitForSeconds(waitTime);
print("WaitAndPrint " + Time.time);
}
}
启动一个名为 methodName
的协同程序。
在大多数情况下,您应该使用上面的 StartCoroutine 变体。 但是,使用字符串方法名称的 StartCoroutine 让您能够使用具有特定方法名称的 StopCoroutine。 缺点是字符串版本在启动协同程序时的运行时开销更高,并且您只能传递一个参数。
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 return null;
}
}
}
coroutine | 所创建的协同程序的名称。 |
启动一个名为 coroutine
的协同程序。
Create a Coroutine. Any coroutine name can be used. A StartCoroutine function terminates immediately, however, the Coroutine it creates runs as expected. A created coroutine can start another coroutine. These two coroutines can operate together in many ways. This includes both coroutine running in parallel. Alternatively one coroutine can stop the other while it continues itself. The script example below shows one coroutine pausing at it starts another one. Once this second coroutine finishes it restarts the first one.
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");
}
}