协程——Coroutine

作者:追风剑情 发布于:2016-1-26 14:31 分类:Unity3d

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!

 

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号