场景切换前必须要将场景拖动到Build中
同步加载场景
using System.Collections;
using System.Collections.Generic;
//using UnityEditor.SearchService;
using UnityEngine;
// 场景管理 需要导入该类
using UnityEngine.SceneManagement;public class c3 : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){// 获取当前场景Scene scene1 = SceneManager.GetActiveScene();Debug.Log(scene1.name);// 场景是否加载完成Debug.Log(scene1.isLoaded);// 场景路径Debug.Log(scene1.path);// 场景中最外层的游戏物体GameObject[] gameObjects = scene1.GetRootGameObjects();Debug.Log(gameObjects.Length); // 看场景中的数量// 切换场景// 根据序号//SceneManager.LoadScene(1);// 根据场景名//SceneManager.LoadScene("Scene2");// 替换场景//SceneManager.LoadScene("Scene2", LoadSceneMode.Single);// 融合场景//SceneManager.LoadScene("Scene2", LoadSceneMode.Additive);// 创建新场景}// Update is called once per framevoid Update(){}
}
异步加载场景
using System.Collections;
using System.Collections.Generic;
//using UnityEditor.SearchService;
using UnityEngine;
// 场景管理 需要导入该类
using UnityEngine.SceneManagement;public class c3 : MonoBehaviour
{// 声明 AsyncOperation类型的 返回值AsyncOperation operation;void Start(){// 调用协程StartCoroutine(loadScene());}// 计时器 5秒后再跳转float t1 = 0;// 协程IEnumerator loadScene(){operation = SceneManager.LoadSceneAsync(1);// 加载后 可以不要自动跳转operation.allowSceneActivation = false;yield return operation;}void Update(){// 获取加载进度 0-0.9// Debug.Log(operation.progress);t1 += Time.deltaTime;// 如果到5秒,则跳转场景if (t1 >=5){operation.allowSceneActivation = true;}}
}