SceneManager.LoadScene
public static void LoadScene(int sceneBuildIndex,SceneManagement.LoadSceneMode mode = LoadSceneMode.Single);
public static void LoadScene(string sceneName,SceneManagement.LoadSceneMode mode = LoadSceneMode.Single);
Description:
Loads the scene by its name or index in Build Settings.
例子1:
using UnityEngine; using UnityEngine.SceneManagement;//使用场景管理器 public class ExampleClass : MonoBehaviour{ void Start() { //使用LoadScene加载场景 //第二个参数AddSceneMode.Additive表示当前场景不销毁,并加载需要的场景 SceneManager.LoadScene("OtherSceneName", LoadSceneMode.Additive); } }
例子2:
using UnityEngine; using UnityEngine.SceneManagement; public class LoadScene : MonoBehaviour{ private AssetBundle myLoadedAssetBundle; private string[] scenePaths; // Use this for initialization void Start() { //获取所有场景 myLoadedAssetBundle = AssetBundle.LoadFromFile("Assets/AssetBundles/scenes"); scenePaths = myLoadedAssetBundle.GetAllScenePaths(); } void OnGUI() { if (GUI.Button(new Rect(10, 10, 100, 30), "Change scene")) { Debug.Log("scene2 loading: " + scenePaths[0]); //销毁当前场景,加载要加载的场景 SceneManager.LoadScene(scenePaths[0], LoadSceneMode.Single); } } }