Azound
February 18, 2010, 6:48am
1
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?
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");
}
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 .
Semut
April 6, 2011, 4:48pm
3
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
Create new folder and rename it as
"Editor"
On the folder you just create right
click and Create >
JavaScript
There will be a new file and rename
it as "SemutMenu"
Right click the newly created and
renamed file to edit it (choose
edit).
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!