Unity 3D 游戏引擎中是否有使用 C# 的命令让代码在发生事情之前等待?这是一个例子:
void Start () {
//(wait code here)
Connect();
}
Unity3D 游戏引擎教程中推荐的方法是使用
WaitForSeconds
等待给定的浮点值(例如 0.5f 半秒)。
但是根据这条规则,你需要将类设置为
IEnumerator
所以:
public flat StartWait;
IEnumerator SpawnWaves ()
{
//your code
yield return new WaitForSeconds (StartWait);
}
这会导致类/对象等待,但不会冻结整个游戏逻辑。也可以看看 http://unity3d.com/learn/tutorials/projects/space-shooter/spawning-waves
使用类似的启动函数来等待几秒
using System.Collections; //using this libarary mandatory
private IEnumerator Start()
{
yield return new WaitForSeconds(5);
//Then do this
}