Boost your Unity editor with custom commands!

Let’s discover how to improve our Unity editor by adding custom commands, shortcuts and more 🙂

This article is also available on Medium.

Unity is a really great game engine that allows you to quickly start creating games while taking care of a lot of the heavy-lifting under the hood.

In particular, when you work in Unity, you are offered a whole set of tools, editors, windows, gizmos and Inspectors to help you with the making of your games. But, sometimes, those built-in utilities are not enough, and you would like to complement them with your own toolbox.

Well – another incredible thing with Unity is that it is highly customisable and that you have lots of ways to extend its features to suit your specific workflow 🙂

So, today, I want to talk a bit about running C# scripts in edit mode and making the best out of the [MenuItem] attribute

Ready? Let’s dive in!

What is the [MenuItem] attribute?

In short, this [MenuItem] attribute is a way of telling Unity that the function you decorate should also be associated with a menu entry somewhere in the Unity editor interface. To specify the exact position of this menu (i.e. which panel it will appear in), you need to set the “path” of the menu item by passing it to the attribute – each slash / character will create a new level:

As you can see, you also need to import the UnityEditor package to have access to this attribute, and you need to use it on a static function.

Note: the class you define the function in, and the name of the function in itself, aren’t important, though – just make sure it doesn’t conflict with another 🙂

Just by doing this, I’ve just created a new menu at the top of my Unity editor (in the menu bar), with a “Run a command” entry, and as soon as I click it, I directly get my debug:

In other words, with just a few lines of code, I can now run C# from my editor in a breeze! 🙂

Of course, you can use a path with more levels to get sub-menus:

Adding shortcuts

To make it even easier to run my command, I can even boost the [MenuItem] attribute path with some extra characters to link it to a keyboard shortcut. As explained in the docs, all I have to do is use keys or special characters (for the Ctrl, Cmd, Shift, Alt… modifiers) to build my shortcut, and add this series of letters at the end of the “path”:

Now, I can check my shortcut in the menu, and I can use it to run my command without even going to the “Custom Commands” menu!

Using a validator for your command

If you want, you can also define a second static function that returns a boolean and defines a specific condition that tells Unity whether your command should be enabled or not; basically, you just need to re-use the same command path, and pass in true as the second parameter of the [MenuItem] attribute to say this is a validator function:

Here, for example, my custom command from before will still be associated to the “Run a command” menu item, but this item will not be enabled if an object is currently selected (because now I want it to print the name of the selected object, so I need to have one!):

Using special paths to place the command in specific menus

Something that sometimes repels beginners at first is that the Unity interface has quite a lot of panels and windows available. The default layout already presents you with several areas that are all important to understand if you wish to properly interact with your project contents: you have the scene hierarchy, the scene/game views, the Inspector for the currently selected assets, the project browser…

But for devs who are more familiar with Unity, this layout quickly becomes home, and so, as you start to add your own commands, you might be wondering if you can get access to all of these areas, and go beyond just “creating new menus at the top”?

Don’t worry – the answer is yes! 😉

Basically, there are some special keywords you can use in your command’s path to have the associated menu appear in different parts of the editor interface:

  • the “CONTEXT” keyword allows you to add your menu item to the contextual menu of a component (and your command will therefore be available for any object that has this component on):
  • the “Assets” keyword allows you to add your menu item in the project browser window, when you create new assets:
  • the “GameObject” keyword allows you to add your menu item in the Game Object creation menu in the Hierarchy panel – this is particularly useful if you want to quickly instantiate an object with some predefined options, for example:

In that last case, you see that you need to give some additional parameters to the [MenuItem] attribute to insure that the new menu entry does appear in the scene hierarchy (the “10” at the end, see the docs for more info) and that you can use a MenuCommand-typed input to optionally do some contextual configs, like here the auto-reparenting.

Important note: from what I’ve tested, it looks like you can’t have spaces in the first part of your object creation command’s path (i.e. the category of the object, here “CustomObjects”).

Creating un-doable actions!

But this last example is incomplete: for this tool to be really intuitive, you’d also want for the command to be easily undoable. This way, if you run your C# logic but you feel like it didn’t work as intended, or the generated object was not prepared properly, you can easily go back to your previous state, just as you would with a “normal” built-in Unity tool 🙂

Luckily, those menu-based commands can easily be fitted in the global undo/redo system of Unity, thanks to the Undo.RegisterCreatedObjectUndo() function:

And, now, if you re-run the command, you’ll see that you can use Ctrl + Z as usual to undo your latest actions – yay! 😉


I find it super that Unity, in addition to having most common utilities available out-of-the-box, also lets us play around with the editor and input our own helpers in it so easily!

By the way, if you want to boost your Unity toolbox even more, and speed your workflow like crazy, then I really recommend you watch this great talk by Yilmaz Kiymaz, “Become a Speed Demon Being Faster at Everything you Do in Unity”, from Unite Berlin 2018 😉

But what about you: do you create your own tooling a lot in Unity? Do you have any tips and tricks on how to improve a Unity workflow? Feel free to share your thoughts in the comments, and as always, thanks a lot for reading!

Leave a Reply

Your email address will not be published.