鸟语天空
协程——Coroutine
post by:追风剑情 2016-1-26 14:31
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);
    }
}

运行效果

run.png

注意:如果协程还未运行完,对象的active被设置成false,会导致报错:Coroutine couldn't be started because the the game object 'xxx' is inactive!

 

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容