Add a button in the component inspector

Hi,

I have a MonoBehaviour script and I would like to add a button to it in the inspector tab when it is added to an editor-only GameObject. Like the terrain script that has a customized inspector GUI.

All that I have found is to add a button in the GUILayer of the camera by calling GUI.Button(). I think I could create an EditorWindow but I would like to attach this window to a GameObject like I can attach a script to a GameObject

Is it possible and how can I do it ?
Thanks.

You have to create a custom inspector for your class.

Just create a new script in an editor folder and derive your class from Editor. Add the CustomEditor attribute with your inspected class. OnInspectorGUI is the “event” that you need. Keep in mind that the default inspector will no longer show your public variables. You have to implement the whole inspector for your class yourself or call DrawDefaultInspector at the end.

You might want to take a look at how to extend the editor

See an answer “Create a Button in the inspector” here for JavaScript and C# examples: