以下是我的 unitytest 代码:
LoadScene("Scene/Level-2");
yield return new WaitUntil(() => { return GameObject.FindObjectOfType<Arrow>(); });
var arrow = GameObject.FindObjectOfType<Arrow>();
我加载了一个场景并等待加载了某个对象
我希望找到一种方法来省略yield return new WaitUntil
,那么有没有办法等待所有 MonoBehavior#Start 完成然后运行代码?
在元素实例化后的第一帧上调用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();
}