RectTransform custom editor ontop of Unity RectTransform Custom Editor??

Using the code below I lose all the functionality of the current Unity Custom Editor on their RectTransform. How do I keep their exact RectTransform while also adding my own stuff underneath it? I tried Editor.CreateEditor stuff and I kept getting infinite loop crashing when I tried to create Editor based on the RectTransform.

using UnityEditor;
using UnityEngine;
using System.Collections;


[CustomEditor(typeof(RectTransform))]
public class RectTransformEditor : Editor {   
   
    public override void OnInspectorGUI () {
        DrawDefaultInspector ();
    }
}

You need to inherit RectTransformEditor and call base OnInspectorGUI function. Since RectTransformEditor is internal class you need System.Reflection to get editor type, then instantiate it.

using UnityEngine;
using UnityEditor;
using System.Reflection;
using System;

[CustomEditor(typeof(RectTransform), true)]
public class RectTransformEditor : Editor
{
    private Editor _editorInstance;

    private void OnEnable()
    {
        Assembly ass = Assembly.GetAssembly(typeof(UnityEditor.Editor));
        Type rtEditor = ass.GetType("UnityEditor.RectTransformEditor");
        _editorInstance = CreateEditor(target, rtEditor);
    }

    public override void OnInspectorGUI()
    {
        _editorInstance.OnInspectorGUI();
        // ...
    }
}
6 Likes

If you also want to see ‘Anchor Indicators’ in editor scene, you need to override ‘OnSceneGUI’ method explicitly as i’ve shown below.

using UnityEngine;
using UnityEditor;
using System.Reflection;
using System;

[CustomEditor(typeof(RectTransform), true)]
public class RectTransformEditor : Editor {
    private Editor _editorInstance;

    private void OnEnable() {
        Assembly ass = Assembly.GetAssembly(typeof(UnityEditor.Editor));
        Type rtEditor = ass.GetType("UnityEditor.RectTransformEditor");
        _editorInstance = CreateEditor(target, rtEditor);
    }

    public override void OnInspectorGUI() {
        _editorInstance.OnInspectorGUI();
    }

    private void OnSceneGUI() {
        MethodInfo onSceneGUI_Method = _editorInstance.GetType().GetMethod("OnSceneGUI", BindingFlags.NonPublic | BindingFlags.Instance);
        onSceneGUI_Method.Invoke(_editorInstance, null);
    }
}
2 Likes

Hey I’ve been using this to add a few extra buttons to the rectransform component and it’s so handy, but I keep getting these errors -

SerializedObjectNotCreatableException: Object at index 0 is null

Does anyone know what might cause that?

Hey. You need to destroy created editor. It’s mentioned in documentation.
In my case I had to call it from OnDisable and OnDestroy from my custom editor script.

Ace! Thanks for that :slight_smile: