Unity 3d 里有关时间延迟

来源:互联网 发布:英语单词读音软件初中 编辑:程序博客网 时间:2024/04/28 20:34

 

 

IEnumerator WaitAndPrint(float waitTime)
    {
        // pause execution for waitTime seconds
        yield return new WaitForSeconds(waitTime);
        print("----------------")
    }

在其它方法里调用:
StartCoroutine(WaitAndPrint(3.0F));
好像在Update里不好用。或者说不能使用。求解ing。。。。。。。



暂停Time.timeScale = 0;
FixedUpdate功能将不会被调用时, 时间刻度设置为零。

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Coroutines & Yield ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

 

在编写游戏代码的时候,常常需要处理一系列事件。如:
Private Update()
{
if(state == 0)
{
   state = 1;
   return;
}
if(state == 1)
{
   state = 2;
   return;
}
}
更方便的是使用yield语句。yield语句是一个特殊类型的返回,他确保在下次调用时该函数继续从该yield语句之后执行。
while(true)
{
// 做步骤1
yield; // 等待一帧
// 做步骤2
yield; // 等待一帧
}
你也可以传递特定的值给yield语句来延迟Update函数的执行,直到一个特定的事件发生。
// 做一些事儿
yield WaitForSeconds(5.0); // 等待5秒
// 做其他事儿
可以叠加和连接coroutines。
这个例子执行Do,在调用之后立即继续。
Do();
print("this is printed immedinately");

function Do()
{
print("do now");
yield WaitForSeconds(2);
print("Do 2 seconds later");
}
这个例子将执行Doo并等待它完成,再继续执行自己。
yield StartCoroutine("Doo");
print("Also after 2 seconds");
print("This is after the doo coroutine has finished execution");
function Doo()
{
print("Doo now");
yield WaitForSeconds(2);
print("Doo 2 seconds later");
}
任何事件处理句柄都可以是一个coroutine。
注意:不能在Update或FixedUpdate内使用yield。但是你可以使用StartCoroutine来开始一个函数。