Overview: Coroutines & Yield 协同程序 & 中断

When writing game code, one often ends up needing to script a sequence of events. This could result in code like the following.

写游戏代码,往往最终需要代码为连续的事件.结果会像这样:

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	private int state = 0;
	void Update() {
		if (state == 0) {
			state = 1;
			return;
		}
		if (state == 1) {
			state = 2;
			return;
		}
	}
}
private var state = 0;

function Update() {
	if (state == 0) {
		// do step 0  //做步骤0
		state = 1;
		return;
	}
	if (state == 1) {
		// do step 1 // 做步骤1
		state = 2;
		return;
	}
	// ...
}

It is often more convenient to use the yield statement. The yield statement is a special kind of return, that ensures that the function will continue from the line after the yield statement next time it is called.

往往使用中断语句更为方便.中断语句是一个特殊的返回类型,它确保函数从中断语句的下一行继续执行.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	IEnumerator Awake() {
		while (true) {
			yield return null;
			yield return null;
		}
	}
}
while(true) {
	// do step 0	// 做步骤0
	yield; 
	// wait for one frame 
	// do step 1
	// 等待一帧
	// 做步骤1
	yield; 
	// wait for one frame
	// 等待一帧
	// ...
}

You can also pass special values to the yield statement to delay the execution of the Update function until a certain event has occurred.

你也可以传递时间值到中断语句,Update函数会在中断时间结束后执行下一语句.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	IEnumerator Awake() {
		yield return new WaitForSeconds(5.0F);
	}
}
// do something
yield WaitForSeconds (5.0); // wait for 5 seconds等待5秒
// do something 
more...

You can both stack and chain coroutines.

你能叠加和连接协同程序.

This example will execute Do but continue after calling do immediately.

这个例子将执行Do,但是do函数之后的print指令会立刻执行.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	IEnumerator Do() {
		print("Do now");
		yield return new WaitForSeconds(2);
		print("Do 2 seconds later");
	}
	void Awake() {
		Do();
		print("This is printed immediately");
	}
}
Do ();
print ("This is printed immediately");

function Do (){
	print("Do now");
	yield WaitForSeconds (2);
	print("Do 2 seconds later");
}

This example will execute Do and wait until it is finished before continuing its own execution

这个例子将执行Do,并等待,直到Do完成再执行其他语句.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	IEnumerator Do() {
		print("Do now");
		yield return new WaitForSeconds(2);
		print("Do 2 seconds later");
	}
	IEnumerator Awake() {
		yield return StartCoroutine("Do");
		print("Also after 2 seconds");
		print("This is after the Do coroutine has finished execution");
	}
}
// chain the coroutine
// 连接协同程序
yield StartCoroutine("Do");
print("Also after 2 seconds");

print ("This is after the Do coroutine has finished execution");

function Do (){
	print("Do now");

	yield WaitForSeconds (2);

	print("Do 2 seconds later");
}

Any event handler can also be a coroutine.
任何事件处理程序都可以是协同程序

Note that you can't use yield from within Update or FixedUpdate, but you can use StartCoroutine to start a function that can.

注意你不能在Update或FixedUpdate函数内使用中断,但是你能使用 StartCoroutine 开始一个函数.

查看 YieldInstruction, WaitForSeconds, WaitForFixedUpdate, Coroutine and MonoBehaviour.StartCoroutine 可以获得更多使用中断的信息.

最后修改:2011年3月23日 Wednesday 9:23

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。