This page looks best with JavaScript enabled

Using ContextMenu and ContextMenuItem in Unity 3D

 ·  ☕ 2 min read

There are a lot of options available to you when you want to test your project in Unity3D and validate that your code actually does what you think it does. Sometimes you just need a quick way to perform some action or access some area during development. One of the ways to introduce this into the editor is through modifications to Unity’s inspector. The ContextMenu and ContextMenuItem attributes provide this option by allowing you to create menu’s in the Unity inspector that can execute functions on your MonoBehaviors.

Thanks to Jason Storey for recommending the use of ContextMenu’s. You can find more of their content as a guest in videos at Infallible Code: https://www.youtube.com/user/charlesamat

Code Examples

Some example uses of Unity 3D’s ContextMenu and ContextMenuItem Attributes:

ContextMenu

A ContextMenu Attribute is attached to functions in your MonoBehavior. When that menu item is clicked it will invoke the function the attribute is assigned to:

1
2
3
4
[ContextMenu("Context Menu Example")]
void HelloContextMenu() {
    Debug.Log("Context Menu Clicked!");
}

ContextMenuItem

A ContextMenuItem Attribute is attached to a field and requires you to provide the name of a function that will be invoked when the menu item is clicked.

1
2
3
4
5
6
[ContextMenuItem("Context Menu Item Example", nameof(InvokeContextMenuItem))]
public float someVariable;

void InvokeContextMenuItem() {
    Debug.Log("Context Menu Item Clicked!");
}

nameof is a C# function that returns a string representation for the name of the class, function, field, property etc that is referenced. Using this allows things like refactoring’s to function better and provides compiler errors if you spell a function name incorrectly (nameof(FunctionName) will throw an error if FunctionName is not defined).

Note: The functions invoked by both ContextMenu and ContextMenuItem should not have any arguments.


Unity’s documentation on the ContextMenu Attribute: https://docs.unity3d.com/ScriptReference/ContextMenu.html
Unity’s documentation on the ContextMenuItem Attribute: https://docs.unity3d.com/ScriptReference/ContextMenuItemAttribute.html
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u


Sam Wronski
WRITTEN BY
Sam Wronski
Maker of things, currently helping build cloud things @ Google. World of Zero is a personal project disconnected from my professional work. Lets make something awesome together!