Adding a button via editor script is breaking the default Inspector

Hi All!

I’d like to be able to add few buttons to the rectTransform component with an editor script so I made this quick test button -

[CustomEditor(typeof(RectTransform))]
public class RectTransformEditor : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
       
        if (GUILayout.Button("Test"))
            Debug.Log("Hi!");
       
        DrawDefaultInspector();
    }
}

But it seems to break the inspector for the original RectTransform. It goes from this - 6346848--705369--Screen Shot 2020-09-25 at 11.43.57 am.png
To this -
6346848--705372--Screen Shot 2020-09-25 at 11.42.37 am.png

Does anyone know a way to keep the underlying inspector?

Thanks!
Pete

@petey

I’m not sure if this is the only way to do this, but you might have to do some reflection magic… I think I’ve used this solution once or twice…

Ahh awesome thanks!

6347580--705495--Screen Shot 2020-09-25 at 5.59.27 pm.png
I wonder if it didn’t work quite right originally is because Unity have their own Editor script making the RectTransform look nice?

Anyway this is working great so thanks :slight_smile:

Pete.

1 Like