問題: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無此問題