- Home /
How do I add a drop-down menu to an EditorWindow?
I want to add a drop-down menu to my custom EditorWindow. Something like either the Unity File menu, or even better, the "Create" menu in the Project editor. I've looked through all the GUI and EditorGUI controls, and none of them look like a drop-down menu to me. Perhaps it's a "Popup" with a very customized style?
Does anybody know?
Answer by Statement · Feb 18, 2010 at 09:02 AM
See EditorGUI.Popup on scripting reference for using popups in editor scripts.
Alteratively EditorUtility.DisplayPopupMenu might do what you want but I have never used it. It seems to be customizable through EditorGUIUtility.LookLikeControls.
even though there's an answer with more upvotes below, here are the fixed links in case anyone else wants to have a look without having to google them themselves:
EditorUtility.DisplayPopup$$anonymous$$enu
EditorGUIUtility.LookLikeControls (Obsolete)
Answer by numberkruncher · Jun 08, 2012 at 04:27 PM
I found that a mixed of the above answers was required to get the editor-style menu strip at the top of an EditorWindow
:
void OnGUI() {
GUILayout.BeginHorizontal(EditorStyles.toolbar);
DrawToolStrip();
GUILayout.EndHorizontal();
}
void DrawToolStrip() {
if (GUILayout.Button("Create...", EditorStyles.toolbarButton)) {
OnMenu_Create();
EditorGUIUtility.ExitGUI();
}
GUILayout.FlexibleSpace();
if (GUILayout.Button("Tools", EditorStyles.toolbarDropDown)) {
GenericMenu toolsMenu = new GenericMenu();
if (Selection.activeGameObject != null)
toolsMenu.AddItem(new GUIContent("Optimize Selected"), false, OnTools_OptimizeSelected);
else
toolsMenu.AddDisabledItem(new GUIContent("Optimize Selected"));
toolsMenu.AddSeparator("");
toolsMenu.AddItem(new GUIContent("Help..."), false, OnTools_Help);
// Offset menu from right of editor window
toolsMenu.DropDown(new Rect(Screen.width - 216 - 40, 0, 0, 16));
EditorGUIUtility.ExitGUI();
}
}
void OnMenu_Create() {
// Do something!
}
void OnTools_OptimizeSelected() {
// Do something!
}
void OnTools_Help() {
Help.BrowseURL("http://example.com/product/help");
}
Answer by Semut · Apr 06, 2011 at 04:48 PM
Ok, I refer this a newbie question but as a developer state, and not as newbie question in general. I'm also still considered as noobs!
Here I will create a menu that will automatically added into your Unity.
Unity JavaScript - JS
Withing your editor, clear all then copy and paste below code..
import System.IO; class SemutMenu extends EditorWindow {
@MenuItem ("Semut/Menu One")
static function ShowWindow () {
EditorWindow.GetWindow (SemutMenu);
}
}
Now after you SAVE IT, you will have new menu that appear on the Semut > Menu One
That will be all!