usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEngine.SceneManagement;publicclasssceneTest:MonoBehaviour{// Start is called before the first frame updatevoidStart(){//场景类 场景管理类//SceneManager.LoadScene(1);//获取当前场景Scene scene = SceneManager.GetActiveScene();//场景名称
Debug.Log(scene.name);//场景是否已经加载
Debug.Log(scene.isLoaded);//场景路径
Debug.Log(scene.path);//场景索引
Debug.Log(scene.buildIndex);GameObject[] gos = scene.GetRootGameObjects();
Debug.Log(gos.Length);//场景管理类//创建新场景Scene newScene = SceneManager.CreateScene("newScene");//已加载场景个数
Debug.Log(SceneManager.sceneCount);//卸载场景
SceneManager.UnloadSceneAsync(newScene);//加载场景
SceneManager.LoadScene("MyScene", LoadSceneMode.Additive);}// Update is called once per framevoidUpdate(){}}
异步加载场景
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEngine.SceneManagement;publicclassAsyncSceneTest:MonoBehaviour{// Start is called before the first frame updateAsyncOperation operation;voidStart(){StartCoroutine(loadScene());}//协程方法用来异步加载场景IEnumeratorloadScene(){
operation = SceneManager.LoadSceneAsync(1);//加载完场景不要自动跳转
operation.allowSceneActivation =false;yieldreturn operation;}float timer =0;// Update is called once per framevoidUpdate(){//输出加载进度 0-0.9
Debug.Log(operation.progress);
timer += Time.deltaTime;if(timer >5){
operation.allowSceneActivation =true;}}}