using UnityEngine; using System.Collections; public class YieldTest : MonoBehaviour { public void Awake() { print("Begin Awake"); //StartCoroutine("FunCoroutine1"); StartCoroutine(FunCoroutine1()); print("End Awake"); } public IEnumerator FunCoroutine1() { print(Time.time); yield return new WaitForSeconds(5); //等待5秒 print("WaitForSeconds: " + Time.time); yield return new WaitForFixedUpdate(); //等待FixedUpdate()执行完成 print("WaitForFixedUpdate: "+Time.time); yield return new WaitForEndOfFrame();//等待当前帧执行完成 print("WaitForEndOfFrame: " + Time.time); yield return null; print(Time.time); } }
运行效果
注意:如果协程还未运行完,对象的active被设置成false,会导致报错:Coroutine couldn't be started because the the game object 'xxx' is inactive!