Unity Editor Extensions - Menu Items
Unity Editor Extensions - Menu Items
Unity Editor Extensions - Menu Items
Tutorials (/learn/tutorials)
Using
Extensions – Essential
Unity
This creates a new editor menu called Tools, and places a menu
item called Clear PlayerPrefs under it:
It’s also possible to create
new menu items under an
existing menu (e.g: under
the Window menu), and also to create multiple levels of menus for
better structuring and organization (recommended):
C#
using UnityEngine;
using UnityEditor;
[MenuItem("Window/New Option")]
private static void NewMenuOption()
{
}
• # – Shift
• & – Alt
• F1…F2 – F keys
C#
Special Paths
As seen, the path passed to the MenuItem attribute controls under
which top level menu the new item will be placed.
Unity has a few “special” paths that act as context menus (menus
that are accessible using right-click):
• 1
CONTEXT/ComponentName – items will be available by right-
clicking inside the inspector of the given component.
2
3
Here
4 are some examples of how to use these special paths:
5
6 C#
7
8
9
1 // Add a new menu item that is accessed by right-clicking on an asset in the project view
10
2
11
3 [MenuItem("Assets/Load Additive Scene")]
12
4 private static void LoadAdditiveScene()
13
5 {
14
6 var selected = Selection.activeObject;
15
7 EditorApplication.OpenSceneAdditive(AssetDatabase.GetAssetPath(selected));
16
8 }
17
9
18
10 // Adding a new menu item under Assets/Create
19
11
12 [MenuItem("Assets/Create/Add Configuration")]
13 private static void AddConfig()
The results for this code segment is these new menu options:
Assets (project view) right-click menu
New option available from the Asset’s CREATE button
New context menu option for the RigidBody component
Validation
Some menu items only make sense in a given context, and should
not be available otherwise. Enabling/disabling menu items
according to their usage
context is done by adding
validation methods.
Validation methods are
static methods, marked
with the MenuItem
attribute, passing true to
the validation argument.
The validation method
should have the same
menu path as the menu it
is validating, and should
return a boolean value to
determine whether the
menu item is active or
not.
For example, Validation
methods can be used to add a right-click menu to Texture assets
only under the project view:
C#
1 [MenuItem("Assets/ProcessTexture")]
2 private static void DoSomethingWithTexture()
3 {
4 }
5
6 // Note that we pass the same path, and also pass "true" to the second argument.
7 [MenuItem("Assets/ProcessTexture", true)]
8 private static bool NewMenuOptionValidation()
9 {
10 // This returns true when the selected object is a Texture2D (the menu item will be disabled otherwise).
11 return Selection.activeObject.GetType() == typeof(Texture2D);
12 }
The code example results in the menu that has 2 groups of items,
according to the assigned priority:
If it is required to add and
organize menu items
under existing Unity
menus, a bit of “guess
work” is needed, as most
of the built-in menu items
use priorities. Another
option is to use a tool
such as Reflector and
look at the source code
for internal Unity code
(such as
Related Classes
Below is a listing of a few extra classes that are related to adding
new menu items.
MenuCommand
When adding a new menu item to an inspector (using
“CONTEXT/Component”, as described above), sometimes it is
necessary to get a reference to the actual component (e.g: to
modify its data).
This can be done by adding a MenuCommand argument to the
static method that defines the new menu item:
C#
1 [MenuItem("CONTEXT/RigidBody/New Option")]
2 private static void NewMenuOption(MenuCommand menuCommand)
3 {
4 // The RigidBody component can be extracted from the menu command using the context field.
5 var rigid = menuCommand.context as RigidBody;
6 }
As seen in the code example, when invoking the menu item, the
component that serves as its context can be accessed using the
context field.
ContextMenu
This attribute allows defining context menu items. This works
exactly the same as defining a method with the MenuItem attribute
with a path that starts with “CONTEXT/…”.
The difference is that with this attribute, you define the default
context menu for a given component, whereas with the MenuItem
approach, you “extend” other components’ menus (for example –
the default components that are part of the engine).
Example – a component that exposes a context menu option to
clear its data:
C#
C#
Wrap Up
As shown in this article,
extending the Unity editor with custom menus can be pretty
straightforward.
Building commonly used functionality and having it available from
the editor is recommended for teams of all sizes and can be a
huge time saver.
(/learn/tutorials/topics/interface-
(/learn/tutorials/topics/interface-
Tweet Like 1 essentials/editor-
essentials/drawdefaultinspector-
function? basics?
(/)
Related documentation
• Menu Item
(http://docs.unity3d.com/ScriptReference/MenuItem.html)
Purchase Download Resources About Unity Get Unity news
(Script Reference)
Subscription Unity Learn Company
•
Asset
Context Menu
(https://store.unity.com/)
Store
(https://unity3d.com/get-
unity)
(https://unity3d.com/learn)
Community
facts
(http://docs.unity3d.com/ScriptReference/ContextMenu.html)
Enter your email Sign
(https://unity3d.com/public-
here...
up
(http://www.nintendo.com/)
(http://www.oculus.com/)
(https://unity3d.com/partners/qualcomm)
(https://unity3d.com/partners/samsung)
(http://www.sony.com)
(https://developer.vuforia.com)
(http://www.mi.com/)
Legal (https://unity3d.com/legal)
policy)
Cookies (https://unity3d.com/legal/cookie-
policy#cookies)