What is it you’re trying to do? Create an editor script where you have a menu of your scenes? Or is this a script in game where a player can select a level?
Since the MenuItems are based on attributes, you’d have to do some code generation to make that work.
It’s much easier and faster to make a custom editor window that lists all of your scenes, and has a button to open it. Then you can dock it behind the inspector or whatnot and bring it up when you need it.
How could I go about doing this I am new to the whole editor scripting and want to crete this tool for a project I am currently working on to make finding scenes easier.
Is there any chance that you can point me in the right direction for this?
I can private message if you would like.
To start out with custom editor windows, here’s a basic snippet that creates a window with a button that opens a scene named “Main_Menu”:
//EditorWindow is the class that defines custom windows
public class SelectLevel : EditorWindow {
[MenuItem("Custom/SelectLevel")]
public static void ShowWindow() {
//GetWindow is a static method that gives you the window of the type. If it's not open yet, it's also opened.
GetWindow<SelectLevel>();
}
//This method draws the window
private void OnGUI() {
//Name of the scene you want to open
string mainMenu = "Main_Menu";
//GUILayout.Button creates a button, and returns true if you click it
if (GUILayout.Button("Open main menu")) {
//This creates a "do you want to save changes to your scene?" popup if you've got unsaved changes.
//It returns true if you click "yes" or "no", and false if you click "cancel"
if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo()) {
//Opens the scene!
EditorSceneManager.OpenScene(mainMenu);
}
}
}
}
EditorBuildSettings.scenes gives you all the scenes you have added to the build settings. Each of those has a .path that you can use to get the path to the scene. Then you’ll have to do some string manipulation to remove the Assets/Scenes/Whatever in front of the path and the .unity at the end of the path, and then you have all of the scenes that exists in your game. So you can use that to create the menu with all of the scenes.