Persistent Scene 開發 – LoadSceneAsync (Additive)

Persistent Scene 開發 – LoadSceneAsync (Additive)

 

LoadSceneAsync??

今日嘗試用Persistent Scene(MainScene)形式, 把UI放左Persistent Scene, 再加載另一個場景(Scene1)。個Fader 無問題, 但係Load完個Scene1之後仍然停在Persistent Scene個樣度, 見唔到Scene1啲野。

Persistent Scene

 

首先我以為個SceneManagement.LoadSceneAsync唔work…


	private IEnumerator LoadSceneAndSetActive(string sceneToLoad){
		Debug.Log ("[LoadSceneAndSetActive] : start SceneManager.LoadSceneAsync");
		AsyncOperation asyncOp = SceneManager.LoadSceneAsync (sceneToLoad, LoadSceneMode.Additive);
		//asyncOp.allowSceneActivation = false;

		while (!asyncOp.isDone) {
			yield return null;
		}



		//Scene   = SceneManager.GetSceneAt (SceneManager.sceneCount - 1); //Most recent scene
		Scene newSceneToLoad = SceneManager.GetSceneByName(sceneToLoad);
		if (newSceneToLoad.IsValid()) {
			Debug.Log("[LoadSceneAndSetActive] : Loading "+ newSceneToLoad.name);
			SceneManager.SetActiveScene(newSceneToLoad); //Set Active	
		}

		Scene activeScene = SceneManager.GetActiveScene ();

		Debug.Log ("[LoadSceneAndSetActive] : end :" + activeScene.name + " is active.");
	}

Google一輪, 又D人話加個asyncOp.allowSceneActivation = false;又乜乜乜, 查證後, 行左呢句
asyncOp.allowSceneActivation = false;之後, 個AsyncOperation係唔會100%Done, 係比你預Load個Scene 但係又唔Activate佢, isDone永遠係false所以之後comment左呢句。

yield return null即係"等"

行佢最後果句, debug log竟然係話Scene1係Active, 即是話個後加Scene其實load左, 仲係Active添!!! Active左又睇唔到, 即係點?

於是試下Run個陣, disable左Persistent Scene個Camera (我每個Scene都預設一個Cam), Scene1畫面即刻出現。唔通我要寫script 一Load左其他Scene就Disable佢? 咁我放係Persistent Scene 嘅UI會唔會無左, 我覺得無理由要disable左佢。

Camera問題

無意中見到個Camera 有個Depth property, 用法係當在場有幾個Camera, Depth大D果個係會衾左Value細個D。
睇返個Setting, Persistent Scene Set左個, Scene1竟然係-1!!!! 唔知邊個咁Set…定係預設。

我無需要邊個衾邊個, 只要個Scene Active就應該show, 於是我兩都Set做零, 咁就做到我要O既效果了。

Project Studies
https://www.youtube.com/watch?v=aq8Myv1fxBU

Unity Offical Document
https://docs.unity3d.com/Manual/class-Camera.html

Other Reference:
http://blog.theknightsofunity.com/using-multiple-unity-cameras-why-this-may-be-important/
https://www.quora.com/How-do-I-make-persistent-UI-so-it-stays-from-scene-to-scene-in-Unity