using UnityEngine;
using UnityEditor;
publicclass MenuItemsExample
{
// Add a new menu item under an existing menu
[MenuItem("Window/New Option")]
privatestaticvoidNewMenuOption()
{
}
// Add a menu item with multiple levels of nesting
[MenuItem("Tools/SubMenu/Option")]
privatestaticvoidNewNestedOption()
{
}
}
// Add a new menu item with hotkey CTRL-SHIFT-A
[MenuItem("Tools/New Option %#a")]
privatestaticvoidNewMenuOption()
{
}
// Add a new menu item with hotkey CTRL-G
[MenuItem("Tools/Item %g")]
privatestaticvoidNewNestedOption()
{
}
// Add a new menu item with hotkey G
[MenuItem("Tools/Item2 _g")]
privatestaticvoidNewOptionWithHotkey()
{
}
// Add a new menu item that is accessed by right-clicking on an asset in the project view
[MenuItem("Assets/Load Additive Scene")]
privatestaticvoidLoadAdditiveScene()
{
var selected = Selection.activeObject;
EditorApplication.OpenSceneAdditive(AssetDatabase.GetAssetPath(selected));
}
// Adding a new menu item under Assets/Create
[MenuItem("Assets/Create/Add Configuration")]
privatestaticvoidAddConfig()
{
// Create and add a new ScriptableObject for storing configuration
}
// Add a new menu item that is accessed by right-clicking inside the RigidBody component
[MenuItem("CONTEXT/Rigidbody/New Option")]
privatestaticvoidNewOpenForRigidBody()
{
}
[MenuItem("Assets/ProcessTexture")]
privatestaticvoidDoSomethingWithTexture()
{
}
// Note that we pass the same path, and also pass "true" to the second argument.
[MenuItem("Assets/ProcessTexture", true)]
privatestaticboolNewMenuOptionValidation()
{
// This returns true when the selected object is a Texture2D (the menu item will be disabled otherwise).return Selection.activeObject.GetType() == typeof(Texture2D);
}
[MenuItem("CONTEXT/RigidBody/New Option")]
privatestaticvoidNewMenuOption(MenuCommand menuCommand)
{
// The RigidBody component can be extracted from the menu command using the context field.var rigid = menuCommand.context as RigidBody;
}
using UnityEngine;
using UnityEditor;
using System.Collections;
// This example shows how to create a context menu inside a custom EditorWindow.// context-click the green area to show the menupublicclass GenericMenuExample : EditorWindow
{
[MenuItem("Example/Open Window")]
staticvoid Init()
{
var window = GetWindow<GenericMenuExample>();
window.position = new Rect(50, 50, 250, 60);
window.Show();
}
void Callback(object obj)
{
Debug.Log("Selected: " + obj);
}
void OnGUI()
{
Event currentEvent = Event.current;
Rect contextRect = new Rect(10, 10, 100, 100);
EditorGUI.DrawRect(contextRect, Color.green);
if (currentEvent.type == EventType.ContextClick)
{
Vector2 mousePos = currentEvent.mousePosition;
if (contextRect.Contains(mousePos))
{
// Now create the menu, add items and show it
GenericMenu menu = new GenericMenu();
menu.AddItem(new GUIContent("MenuItem1"), false, Callback, "item 1");
menu.AddItem(new GUIContent("MenuItem2"), false, Callback, "item 2");
menu.AddSeparator("");
menu.AddItem(new GUIContent("SubMenu/MenuItem3"), false, Callback, "item 3");
menu.ShowAsContext();
currentEvent.Use();
}
}
}
}