在 C# 中等待 Unity 3D 游戏引擎吗?

问题描述 投票:0回答:2

Unity 3D 游戏引擎中是否有使用 C# 的命令让代码在发生事情之前等待?这是一个例子:

void Start () {
    //(wait code here)
    Connect();
}
c# unity-game-engine wait
2个回答
4
投票

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

http://docs.unity3d.com/ScriptReference/WaitForSeconds.html


0
投票

使用类似的启动函数来等待几秒

using System.Collections;  //using this libarary mandatory
private IEnumerator Start()
        {
            yield return new WaitForSeconds(5);
            //Then do this
        }
© www.soinside.com 2019 - 2024. All rights reserved.