scenePath | 场景的路径。这应该是相对于项目文件夹的;例如,“Assets/MyScenes/MyScene.unity”。 |
mode | 允许您选择如何打开指定的场景以及是否保留层次结构中的现有场景。有关选项的更多信息,请参见OpenSceneMode。 |
Scene 对已打开场景的引用。
在编辑器中打开一个场景。
在编辑器中使用此函数在层次结构中打开场景,例如制作自定义编辑器脚本、工具或菜单项。请勿将其用于在运行时加载场景。要加载运行时的场景,请参见SceneManager.LoadScene。
如果出于任何原因导致此功能运行失败(例如,由于文件路径不正确),它将抛出 ArgumentException。
//Create a new folder (Right click in the Assets folder, Create>Folder) and name it “Editor” if one doesn’t already exist //Put this script in the folder
//This script creates a new menu (Examples) and item (Open Scene). If you choose this item in the Editor, the EditorSceneManager opens the Scene at the given directory (In this case, the “Scene2” Scene is located in the Assets folder). This allows you to open Scenes while still working with the Editor.
using UnityEngine; using UnityEditor; using UnityEditor.SceneManagement;
public class Example : MonoBehaviour { // Create a new drop-down menu in Editor named "Examples" and a new option called "Open Scene" [MenuItem("Examples/Open Scene")] static void OpenScene() { //Open the Scene in the Editor (do not enter Play Mode) EditorSceneManager.OpenScene("Assets/Scene2.unity"); } }