Create a Button in the inspector

I’m learning editor scripting, and I can create custom windows, and wizards. But how do you create a button in the inspector? So you click on an object, and under the script title, is a button.

Here’s a C# version:

[CustomEditor(typeof(DecalMeshHelper))]
class DecalMeshHelperEditor : Editor {
  public override void OnInspectorGUI() {
    if(GUILayout.Button("Test"))
      Debug.Log("It's alive: " + target.name);
  }
}

It’s very important to use public override, otherwise it won’t work!

Its very easy:

@CustomEditor(YourScript)
// ^ This is the script we are making a custom editor for.
public class YourScriptEditor extends Editor {
   
    override function OnInspectorGUI () {
    //Called whenever the inspector is drawn for this object.
        DrawDefaultInspector();
        //This draws the default screen.  You don't need this if you want
        //to start from scratch, but I use this when I'm just adding a button or
        //some small addition and don't feel like recreating the whole inspector.

        if(GUILayout.Button("Your ButtonText")) [
            //add everthing the button would do.
        }
   }
}

The key part is to create a custom inspector, and override OnInspectorGUI. From there you have much more freedom in what you want to do. DrawDefaultInspector() will draw the inspector exactly like Unity would so it is useful if you are adding functionality to the end or a small addition because you can draw the default inspector then attach more functionality to the end like the above script does.

Of course, if you wanted, you could create a custom inspector from scratch, recreating every inspector field and such, but if you just need a button, that would probably be more work than you want to do.

I know this is extremely old, but for anyone looking here years later, there’s an easier makeshift way to add buttons!

[ExecuteInEditMode]
public class myClass : Monobehaviour
{

    public bool buttonDisplayName; //"run" or "generate" for example
    public bool buttonDisplayName2; //supports multiple buttons

    Update()
    {
        if (buttonDisplayName)
            ButtonFunction1 ();
        else if (buttonDisplayName2)
            ButtonFunction2 ();
        buttonDisplayName = false;
        buttonDisplayName2 = false;
    }

    void ButtonFunction1 ()
    {
        //DoStuff
    }

    void ButtonFunction2 ()
    {
        //DoStuff
    }
}

In the editor, update is called whenever things are changed. When you press one of our “buttons”, the Boolean value changes to true, and triggers Update() immediately. This runs the button function a single time, and at the end of update, turns the Boolean back to false. This allows you to not have to make a separate editor script, and gives the exact functionality, but just a tinier button.

Also, in C#, be sure to use the UnityEditor namespace so that we can actually use the “Editor” class. Otherwise, you’ll probably get errors.

So at the top, be sure to put the “using UnityEditor;”

Reference to documentation:

But… I am missing something here! How can you build the final project, if in order to create the build you have to save the files (using UnityEditor) in the Editor folder, and then… you cant add components from the Editor folder?

Here is my script to even type & test the method parameters directly from the inspector