In a custom editor, instantiate and use an Editor for a member variable.

I have a MonoBehaviour that has a ScriptableObject as a member variable. By default, the inspector shows an object field for that member variable, where I can drag in other instances. So far, so good.

In addition to this field, I’d like to place, inline in the inspector at that point, all the fields that would normally be shown if I had that ScriptableObject selected instead. Altering those values should alter the underlying ScriptableObject.

I’m familiar with writing custom editors generally. If I write a custom editor for my MonoBehaviour, is there a way in its OnInspectorGUI, that can I can say to Unity “Give me an instance of the editor for this other object”, and then “call its OnInspectorGUI function”.

Edited to add: Something like the following

class SomeData : ScriptableObject
{
    public string m_text;
    // Potentially many other fields here.
}

class MyBehaviour : MonoBehaviour
{
    public SomeData m_data;
}

class MyBehaviourEditor : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();
   
        MyBehaviour mb = target as MyBehaviour;
        Editor e = ...; //  (Get an editor for mb.m_data);
        e.OnInspectorGUI();
    }
}

To answer my own question, use Editor.CreateEditor: Unity - Scripting API: Editor.CreateEditor

 Editor e = Editor.CreateEditor (mb.m_data);
 e.OnInspectorGUI();
2 Likes

I know this is an old thread but i’m interested to know what is your approach. Why you call OnInspectorGUI inside OnInspectorGUI ?

Within MyBehaviourEditor.OnInspectorGUI, I’m calling the OnInspectorGUI method for whatever editor is used for mb.m_data.

Normally, without the code above, if I clicked on a MyBehaviour, the inspector would show just a single field for m_data, where I could drag in an object of type SomeData. With the code I wrote, it would also show all the fields of m_data, such as m_data.m_text.