统一,在LoadScene之后,有没有通用的方法来等待所有monobehaviour#start完成

chi*_*nce 1 c# unity-game-engine

以下是我的 unitytest 代码:

        LoadScene("Scene/Level-2");
        yield return new WaitUntil(() => { return GameObject.FindObjectOfType<Arrow>(); });
        var arrow = GameObject.FindObjectOfType<Arrow>();
Run Code Online (Sandbox Code Playgroud)

我加载一个场景并等待某个对象加载

我希望找到一种方法来省略yield return new WaitUntil,那么有没有办法等待所有 MonoBehaviour#Start 完成然后运行代码?

Jav*_*ich 5

Awake()Start()元件被实例化后,方法被称为第一框架上。所以,如果你加载一个场景并等待下一帧,所有的开始方法都会被调用。

这应该对你有用。

private IEnumerator LoadScene()
{
    // Start loading the scene
    AsyncOperation asyncLoadLevel = SceneManager.LoadSceneAsync("myLevel", LoadSceneMode.Single);
    // Wait until the level finish loading
    while (!asyncLoadLevel.isDone)
        yield return null;
    // Wait a frame so every Awake and Start method is called
    yield return new WaitForEndOfFrame();
}
Run Code Online (Sandbox Code Playgroud)