sceneName | 要加载的场景的名称或路径。 |
sceneBuildIndex | Build Settings 中要加载场景的索引。 |
mode | 允许您指定是否以累加方式加载场景。有关选项的更多信息,请参阅 LoadSceneMode。 |
按照 Build Settings 中的名称或索引加载场景。
**注意:**在大多数情况下,为了避免在加载时出现暂停或性能中断现象,
您应该使用此命令的异步版,即:
LoadSceneAsync。
使用 SceneManager.LoadScene 时,不会立即加载场景,而是在下一帧加载。这种半异步的行为可能会导致帧卡顿,并可能令人困惑,因为加载无法立即完成。
由于加载被设置为在下一个渲染帧中完成,调用 SceneManager.LoadScene 会强制完成之前的所有 AsyncOperations,即使 AsyncOperation.allowSceneActivation 设置为 false 也一样。要避免这种情况,请改用 LoadSceneAsync。
提供的 sceneName
可以只是场景名称(不包含 .unity
扩展名),也可以是 BuildSettings 窗口中显示的路径(仍然不包含 .unity
扩展名)。如果仅提供场景名称,此方法将加载场景列表中匹配的第一个场景。如果有多个名称相同但路径不同的场景,应该使用完整路径。
请注意,除非您从 AssetBundle 加载场景,否则 sceneName
不区分大小写。
有关在编辑器中打开场景的信息,请参阅 EditorSceneManager.OpenScene。
SceneA
能够以累加方式多次加载 SceneA
。每个加载的场景都使用常规名称。如果 SceneA
加载 SceneB
十次,每个 SceneB
将具有相同的名称。无法找到特定的已添加场景。
If a single mode scene is loaded, Unity calls Resources.UnloadUnusedAssets automatically.
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); } }
sceneName | 要加载的场景的名称或路径。 |
sceneBuildIndex | Build 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)); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.