Creating a Context Menu in Custom Unity Editors

Опубликовано: 04 Октябрь 2024
на канале: World of Zero
7,151
74

I've been experimenting with creating custom Inspectors and Editor Windows in Unity lately and one of the features I've found is the `GenericMenu` class. This allows you to create Drop Down and Context Menu's in Unity.

To draw a Context Menu with two options (Foo and Bar) which will print themselves to the Unity Console when clicked the code might look something like this:

```csharp
// Create a new Generic Menu object
var menu = new GenericMenu();

// Create a reusable function for the menu options
var function = new GenericMenu.MenuFunction2((name) ⇒ {
Debug.Log($"{name}");
});

// Create two new menu options to print foo and bar
menu.AddItem(new GUIContent("Print Foo"), false, function, "Foo");
menu.AddItem(new GUIContent("Print Bar"), false, function, "Bar");

// Display the context menu
menu.ShowAsContext();
```

The documentation for `GenericMenu` is also on Unity's scripting references with additional examples: https://docs.unity3d.com/ScriptRefere...

Join the World of Zero Discord Server:   / discord