- Home /
(Editor Scripting) Is it possible to generate MenuItems?
Hello reader!
I've been experimenting with Unity Editor scripting and I was wondering if it is possible to generate MenuItems with code. As in, not having to write:
[MenuItem("Open Scene/TestScene1")]
public static void OpenTestScene1()
{
OpenScene ("TestScene1", "Levels");
}
For every single MenuItem I want to create.
I want to be able to automatically generate a Menu Item to open any specific scene in my project, without having to navigate to them or having to add them to the script manually.
I already have this script:
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
public class EDEX_SceneMenuItem : Editor
{
[MenuItem("Open Scene/Opening", false, 10)]
public static void OpenOpening ()
{
OpenScene ("OpeningScene", "Menus");
}
[MenuItem("Open Scene/Credits")]
public static void OpenCredits ()
{
OpenScene ("Credits", "Menus");
}
[MenuItem("Open Scene/Gallery")]
public static void OpenGallery ()
{
OpenScene ("Gallery", "Menus");
}
[MenuItem("Open Scene/Menus/Character Select")]
public static void OpenCharacterSelect ()
{
OpenScene ("CharacterSelect", "Menus");
}
[MenuItem("Open Scene/Menus/Level Select")]
public static void OpenLevelSelect ()
{
OpenScene ("LevelSelect", "Menus");
}
[MenuItem("Open Scene/Menus/Main Menu")]
public static void OpenMainMenu ()
{
OpenScene ("Menu", "Menus");
}
[MenuItem("Open Scene/Menus/Custom Game")]
public static void OpenCustomGame ()
{
OpenScene ("CustomGame", "Menus");
}
[MenuItem("Open Scene/Menus/Options")]
public static void OpenOptions ()
{
OpenScene ("Options", "Menus");
}
[MenuItem("Open Scene/Menus/Title")]
public static void OpenTitle ()
{
OpenScene ("Title", "Menus");
}
static void OpenScene (string name, string subfolder)
{
if (EditorApplication.SaveCurrentSceneIfUserWantsTo())
{
EditorApplication.OpenScene("Assets/Scenes/" + subfolder + "/" + name + ".unity");
}
}
}
Thanks in advance.
if you mean a keyboard shortcut, the docs show you how... if not, what do you mean?
Answer by basklein_ · Apr 13, 2016 at 11:29 AM
Nevermind, I just made a script to write the above script for me. It works by detecting all scenes in the project and then writing a C# editor script with the correct functions.
Your answer
Follow this Question
Related Questions
How to create an endless track? 1 Answer
Get the currently open scene name or file name 8 Answers
Editor Scripting Question 1 Answer
MenuItem from foreach a list? 1 Answer
Editor Script does not save changes 1 Answer