Sei sulla pagina 1di 11

(/) Unity (/unity) Services (/services)

Made with Unity (/showcase) Learn (/learn)

Tutorials (/learn/tutorials)

 Interface & Essentials (/learn/tutorials/topics/interface- Interface


essentials) &
Essentials
 Unity Editor Extensions – Menu Items

 Using

Unity Editor the Unity


Interface

Extensions –  Essential
Unity

Menu Items Concepts


 Extending
Checked with version: 4.5 - the Unity
Editor
Difficulty: Intermediate
01. Building
a
The Unity editor allows adding custom menus that look and
Custom
behave like the built-in menus. This can be very useful for adding Inspector
commonly used functionality that is frequently needed to be (/learn/tutorials/topics/interfa
accessible directly from the editor UI. In this lesson I’ll show how essentials/building-
custom-
new menu items in the Unity editor are created and try to provide
inspector?
real-world example usages to every described topic. playlist=17090)
02. Adding
Buttons
Adding Menu Items to a
Custom
In order to add a new menu to the top-level toolbar, you should
Inspector
create an editor script (a script file that is placed anywhere in the (/learn/tutorials/topics/interfa
project under a folder named Editor). Menu items are created in essentials/adding-
script code as static methods that are marked with the MenuItem buttons-
custom-
attribute.
inspector?
playlist=17090)
03. The
DrawDefaultInspector
For example, it’s common to add a new “Tools” menu (or a top- Function
(/learn/tutorials/topics/interfa
level menu with your company’s name) to provide options that are
essentials/drawdefaultinspec
commonly used by your team/company. function?
Here’s an example of adding a new Tools menu with an option playlist=17090)
under it (clears all PlayerPrefs data): 04. Unity
Editor
C#   Extensions
– Menu

Items
1 using UnityEngine;
2 using UnityEditor;
3
4 public class MenuItems
5 {
 Live
6 [MenuItem("Tools/Clear PlayerPrefs")] Sessions
7 private static void NewMenuOption()
8 {
on Unity
9 PlayerPrefs.DeleteAll(); Interface
10 }
11 }
and
 Essentials

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;

public class MenuItemsExample


{
// Add a new menu item under an existing menu

[MenuItem("Window/New Option")]
private static void NewMenuOption()
{
}

// Add a menu item with multiple levels of nesting 

This results in the following menu items:


Hotkeys
To allow power users and
keyboard junkies to work faster, new menu items can be assigned
with hotkeys – shortcut key combinations that will automatically
launch them.
These are the supported keys (can also be combined together):

• % – CTRL on Windows / CMD on OSX

• # – Shift

• & – Alt

• LEFT/RIGHT/UP/DOWN – Arrow keys

• F1…F2 – F keys

• HOME, END, PGUP, PGDN

Character keys not part of a key-sequence are added by adding an


underscore prefix to them (e.g: _g for shortcut key “G”).
Hotkey character combinations are added to the end of the menu
item path, preceded by a space), as shown in the following
examples:

C#  

1 // Add a new menu item with hotkey CTRL-SHIFT-A


2
3 [MenuItem("Tools/New Option %#a")]
4 private static void NewMenuOption()
5 {
6 }
7
8 // Add a new menu item with hotkey CTRL-G
9
10 [MenuItem("Tools/Item %g")]
11 private static void NewNestedOption()
12 {
13 } 

Menu items with hotkeys will display the key-combination that is


used to launch them. For example, the code above results in this
menu:
Note: There’s no
validation for overlapping
hotkeys ! defining multiple
menu items with the same hotkey results in only 1 option being
called by hitting the key combination.

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):

• Assets – items will be available under the “Assets” menu, as


well using right-click inside the project view.

• Assets/Create – items will be listed when clicking on the


“Create” button in the project view (useful when adding new
types that can be added to the project)

• 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 }

 

When right-clicking on anything that is not a texture in the project


view, the menu item option will be disabled (greyed out):

Controlling Order with Priority


Priority is a number that can be assigned to a menu item (passed
to the MenuItem attribute) that controls the ordering of menu
items under the root menu.
Menu items are also
automatically grouped
according to their
assigned priority in
increments of 50:
C#  

1 [MenuItem("NewMenu/Option1", false, 1)]


2 private static void NewMenuOption()
3 {
4 }
5
6 [MenuItem("NewMenu/Option2", false, 2)]
7 private static void NewMenuOption2()
8 {
9 }
10
11 [MenuItem("NewMenu/Option3", false, 3)]
12 private static void NewMenuOption3()
13 { 

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

UnityEditor.CreateBuildInWindows) that is responsible for creating


some of the menus in the editor.

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# 

1 public class NameBehaviour : MonoBehaviour


2 {
3 public string Name;
4
5 [ContextMenu("Reset Name")]
6 private static void ResetName()
7 {
8 Name = string.Empty;
9 }
10 }

ContextMenuItem
This attribute is added to fields of a component (MonoBehaviour)
class, to allow adding context menus at a finer resolution. While
the ContextMenu attribute shown above adds context menus at
the component level, marking fields with this attribute will add a
right-click menu to individual public fields.
Since this attribute is added to a field and not a method, it accepts
2 arguments: the display name of the menu item and a name of a
method (instance method) to be invoked when the menu item is
selected.
Example – Adding a method to randomly initialize a component’s
field to some state:

C# 

1 public class NameBehaviour : MonoBehaviour


2 {
3 [ContextMenuItem("Randomize Name", "Randomize")]
4 public string Name;
5
6 private void Randomize()
7 {
8 Name = "Some Random Name";
9 }
10 }

This code results in this context menu when right-clicking on the


Name field of this component:

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.

Lior Tal (http://www.tallior.com/)


Community author
A software engineer, game developer, gamer and geek,
trying to be active in the Unity community and to share my
knowledge, as well as learn from others. For the past few
years I've been busy doing software dev and game
development for mobile platforms. I am interested in all
topics related to software engineering and game dev,
including: tools, methodologies, automated testing and
game engines. On my website, i blog about my experiences
in game development and software engineering (which are
mostly related to Unity).

(/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

I agree to the Unity Privacy Policy


(https://unity3d.com/asset-
Web
(Script Reference) Player (https://unity3d.com/community)
relations)
(https://unity3d.com/company/legal/privacy-
store) (https://unity3d.com/webplayer)
Documentation Blog
• Context Menu
Resellers Item
Press (https://unity3d.com/learn/documentation)
policy) and the processing and use of my
(http://blogs.unity3d.com/)information
(http://docs.unity3d.com/ScriptReference/ContextMenuItemAttribute.html)
(https://store.unity.com/resellers)
material Unity QA Events
(Script Reference)
(https://unity3d.com/public-
(https://unity3d.com/unity/qa)
(https://unity3d.com/events)
Language
Education relations/downloads)
FAQ Careers
中文 Français Deutsch日本語
Students Whitepapers (https://unity3d.com/unity/faq)
(https://careers.unity.com)
(/cn/learn/tutorials/topics/interface-
(/fr/learn/tutorials/topics/interface-
(/de/learn/tutorials/topics/interface
(/jp/learn/tutorials/topics/int
(https://unity3d.com/whitepapers)
Services
(https://store.unity.com/education#student) Press
essentials/unity-
essentials/unity-
essentials/unity-
essentials/unity-
Educators Status (https://unity3d.com/public-
editor- editor- editor- editor-
(http://status.cloud.unity3d.com/)
(https://store.unity.com/education#educator) relations)
extensions-
extensions-
extensions-
extensions-
Certification Contact
menu- menu- menu- menu-
(https://certification.unity.com)
(https://unity3d.com/contact)
items) items) items) items)
Connect Partners
한국어 Português Русский Español
(https://connect.unity.com/)
(https://unity3d.com/partners)
(/kr/learn/tutorials/topics/interface-
(/pt/learn/tutorials/topics/interface-
(/ru/learn/tutorials/topics/interface-
(/es/learn/tutorials/topics/in
Affiliates
essentials/unity-
essentials/unity-
essentials/unity-
essentials/unity-
(https://unity3d.com/affiliates)
editor- editor- editor- editor-
extensions-
extensions-
extensions-
extensions-
menu- menu- menu- menu-
items) items) items) items)
Partners
(http://www.cloudmoolah.com/)
(http://www.facebook.com/)
(https://unity3d.com/partners/google/daydream)
(https://unity3d.com/partners/intel)
(https://unity3d.com/partners/microsoft)

(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/)

© 2017 Unity Technologies

Legal (https://unity3d.com/legal)

Privacy Policy (https://unity3d.com/legal/privacy-

policy)

Cookies (https://unity3d.com/legal/cookie-

policy#cookies)

Potrebbero piacerti anche