One of the most important things in all the softwares is menus.

Menus can give you a lot of options when you need them.

Unity has 7 top menus as default, but can you add a custom menu to it?

The answer is YES you can add custom menu in unity.

Follow these steps to add a custom menu to your unity project.

1. Create a C# Script and call it whatever you want

In this case we are going to call our script CustomMenu.

After that Add UnityEditor library in the top of the code.

Remove the MonoBehaviour interface.

Remove the Update and Start function.

And make your code like this :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class CustomMenu
{

}

Before adding a custom menu you need a function for your menu for when it’s clicked.

So let’s add one

But what this function should do?

Let’s place a Debug.Log for now.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class CustomMenu
{
    public static void CostomMenu1()
    {
        Debug.Log("Custom Menu 1 Clicked");
    }
}

So now, How we can add that custom menu?

With this line of code:

[MenuItem("MenuName/ChilldMenuName")]

If you want to know more about MenuItem read unity’s official documentation about MenuItem

Add it on top of your functions.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class CustomMenu
{
    [MenuItem("CustomMenu/Menu 1")]
    public static void CostomMenu1()
    {
        Debug.Log("Custom Menu 1 Clicked");
    }
}


Now save the code and go back to unity. After the code has been compiled, our Custom Menu should be added like this:

Let’s test our custom menu and see what is going to happen.

Now we add our custom menu and tested it, but one tip is left.

Add this to your code so when you build your project you don’t encounter lots of errors.

#if UNITY_EDITOR
...
#endif

Your code now must be like this.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

#if UNITY_EDITOR
public class CustomMenu
{
    [MenuItem("CustomMenu/Menu 1")]
    public static void CostomMenu1()
    {
        Debug.Log("Custom Menu 1 Clicked");
    }
}
#endif

Now you can replace that Debug.Log with your own code.

Check out these Articles on unity for more tips

If you are learning Blender these Articles should help you become faster in Blender

Leave a Reply

Your email address will not be published. Required fields are marked *