1. 程式人生 > >SceneManager.LoadScene & SceneManager.SetActiveScene

SceneManager.LoadScene & SceneManager.SetActiveScene

問題:SceneManager.LoadScene之後呼叫SceneManager.SetActiveScene報錯,提示場景未載入。(ArgumentException: SceneManager.SetActiveScene failed; scene 'XXX' is not loaded and therefore cannot be set active)

原因:SceneManager.LoadScene

Unity5.x

這個函式呼叫後馬上載入場景

Unity2017.x

這個函式呼叫後第二幀結束的時候完成載入

2017/2018的文件說明:“When using

SceneManager.LoadScene, the loading does not happen immediately, it completes in the next frame. This semi-asynchronous behavior can cause frame stuttering and can be confusing because load does not complete immediately.”

解決辦法:

在SceneManager.LoadScene呼叫之後新增2個 yield return new WaitForEndOfFrame(); 就OK了。

PS:Unity5.x無此問題