public static void LoadScene (int sceneBuildIndex, SceneManagement.LoadSceneMode mode= LoadSceneMode.Single);
public static void LoadScene (string sceneName, SceneManagement.LoadSceneMode mode= LoadSceneMode.Single);

参数

sceneName要加载的场景的名称或路径。
sceneBuildIndexBuild Settings 中要加载场景的索引。
mode允许您指定是否以累加方式加载场景。有关选项的更多信息,请参阅 LoadSceneMode

描述

按照 Build Settings 中的名称或索引加载场景。

注意:在大多数情况下,为了避免在加载时出现暂停或性能中断现象, 您应该使用此命令的异步版,即: LoadSceneAsync

使用 SceneManager.LoadScene 时,不会立即加载场景,而是在下一帧加载。这种半异步的行为可能会导致帧卡顿,并可能令人困惑,因为加载无法立即完成。

由于加载被设置为在下一个渲染帧中完成,调用 SceneManager.LoadScene 会强制完成之前的所有 AsyncOperations,即使 AsyncOperation.allowSceneActivation 设置为 false 也一样。要避免这种情况,请改用 LoadSceneAsync

提供的 sceneName 可以只是场景名称(不包含 .unity 扩展名),也可以是 BuildSettings 窗口中显示的路径(仍然不包含 .unity 扩展名)。如果仅提供场景名称,此方法将加载场景列表中匹配的第一个场景。如果有多个名称相同但路径不同的场景,应该使用完整路径。

请注意,除非您从 AssetBundle 加载场景,否则 sceneName 不区分大小写。

有关在编辑器中打开场景的信息,请参阅 EditorSceneManager.OpenSceneSceneA 能够以累加方式多次加载 SceneA。每个加载的场景都使用常规名称。如果 SceneA 加载 SceneB 十次,每个 SceneB 将具有相同的名称。无法找到特定的已添加场景。

using UnityEngine;
using UnityEngine.SceneManagement;

public class ExampleClass : MonoBehaviour { void Start() { // Only specifying the sceneName or sceneBuildIndex will load the Scene with the Single mode SceneManager.LoadScene("OtherSceneName", LoadSceneMode.Additive); } }
// Load an assetbundle which contains Scenes.
// When the user clicks a button the first Scene in the assetbundle is
// loaded and replaces the current Scene.

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); } } }

以下两个脚本示例说明 LoadScene 可以如何从 Build Settings 加载场景。LoadSceneA 使用要加载的场景的名称。LoadSceneB 使用要加载的场景的编号。这些脚本将协作。

LoadSceneA 文件。

// SceneA.
// SceneA is given the sceneName which will
// load SceneB from the Build Settings

using UnityEngine; using UnityEngine.SceneManagement;

public class LoadScenesA : MonoBehaviour { void Start() { Debug.Log("LoadSceneA"); }

public void LoadA(string scenename) { Debug.Log("sceneName to load: " + scenename); SceneManager.LoadScene(scenename); } }

LoadSceneB 文件。

// SceneB.
// SceneB is given the sceneBuildIndex of 0 which will
// load SceneA from the Build Settings

using UnityEngine; using UnityEngine.SceneManagement;

public class LoadScenesB : MonoBehaviour { void Start() { Debug.Log("LoadSceneB"); }

public void LoadB(int sceneANumber) { Debug.Log("sceneBuildIndex to load: " + sceneANumber); SceneManager.LoadScene(sceneANumber); } }

public static SceneManagement.Scene LoadScene (int sceneBuildIndex, SceneManagement.LoadSceneParameters parameters);
public static SceneManagement.Scene LoadScene (string sceneName, SceneManagement.LoadSceneParameters parameters);

参数

sceneName要加载的场景的名称或路径。
sceneBuildIndexBuild Settings 中要加载场景的索引。
parameters用于加载场景的各种参数。

返回

Scene 正在加载的场景的句柄。

描述

按照 Build Settings 中的名称或索引加载场景。

有关使用两个场景(名为 Scene1 和 /Scene2/)的示例。ExampleScript1.cs 用于 /scene1/,ExampleScript2.cs 用于 /scene2/。

using UnityEngine;
using UnityEngine.SceneManagement;

// This is scene1. It loads 3 copies of scene2. // Each copy has the same name.

public class ExampleScript1 : MonoBehaviour { private Scene scene;

private void Start() { var parameters = new LoadSceneParameters(LoadSceneMode.Additive);

scene = SceneManager.LoadScene("scene2", parameters); Debug.Log("Load 1 of scene2: " + scene.name); scene = SceneManager.LoadScene("scene2", parameters); Debug.Log("Load 2 of scene2: " + scene.name); scene = SceneManager.LoadScene("scene2", parameters); Debug.Log("Load 3 of scene2: " + scene.name); } }

Scene2:

using UnityEngine;

// create a randomly placed cube

public class ExampleScript2 : MonoBehaviour { private void Start() { GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.position = new Vector3(Random.Range(-5.0f, 5.0f), 0.0f, Random.Range(-5.0f, 5.0f)); } }