Search Unity

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

Discussion in 'Immediate Mode GUI (IMGUI)' started by peterpi, May 11, 2016.

  1. peterpi

    peterpi

    Joined:
    May 18, 2013
    Posts:
    20
    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

    Code (csharp):
    1. class SomeData : ScriptableObject
    2. {
    3.     public string m_text;
    4.     // Potentially many other fields here.
    5. }
    6.  
    7. class MyBehaviour : MonoBehaviour
    8. {
    9.     public SomeData m_data;
    10. }
    11.  
    12. class MyBehaviourEditor : Editor
    13. {
    14.     public override void OnInspectorGUI()
    15.     {
    16.         DrawDefaultInspector();
    17.    
    18.         MyBehaviour mb = target as MyBehaviour;
    19.         Editor e = ...; //  (Get an editor for mb.m_data);
    20.         e.OnInspectorGUI();
    21.     }
    22. }
     
    Last edited: May 11, 2016
  2. peterpi

    peterpi

    Joined:
    May 18, 2013
    Posts:
    20
    Tony_Max and TrickyHandz like this.
  3. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    279
    I know this is an old thread but i'm interested to know what is your approach. Why you call OnInspectorGUI inside OnInspectorGUI ?
     
  4. peterpi

    peterpi

    Joined:
    May 18, 2013
    Posts:
    20
    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.