- Home /
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.
Answer by Bunny83 · Jul 06, 2011 at 02:14 PM
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
Thanks, it is nearly working. The only problem is that EditorGUILayout and EditorGUI don't have a Button method. I have to use GUI.Button and the position of the bottom is relative from the top of the inspector of the GameObject, not from the top of the inspector of the component.
Don't use the GUI or EditorGUI stuff in the inspector. The Inspector window is layouted. GUILayout and EditorGUILayout are your firends in the inspector ;).
Btw. EditorGUILayout is just an extention of GUILayout that is only available in the editor.
Yes I understand more the relation between the classes. Thank you, that is perfect
Answer by Zbyl · Dec 08, 2012 at 05:29 PM
See an answer "Create a Button in the inspector" here for JavaScript and C# examples: http://answers.unity3d.com/questions/126048/create-a-button-in-the-inspector.html#answer-360940