Search Unity

Custom Inspector: Draw standard editor for serialized class

Discussion in 'Immediate Mode GUI (IMGUI)' started by sz-Bit-Barons, Jan 5, 2016.

  1. sz-Bit-Barons

    sz-Bit-Barons

    Joined:
    Nov 12, 2013
    Posts:
    150
    Hi,

    I have a custom Inspector for a MonoBehaviour.
    That MonoBehaviour holds a List<IAction>. All classes in my project which implement IAction are declared [Serializable].

    I want to draw each Item in the list as though it where drawn with the default inspector. How do I do that?

    EDIT: I just realized that I don't have a chance to do it: derived objects cannot be serialized by unity (damn it! Thats why I normally always use Full Inspector). Seems like I have to create one big class which contains all possible actions.

    However the question is still valid: how to draw the default inspector for a [Serializable] class instance inside of a custom inspector?
     
    Last edited: Jan 5, 2016
  2. AndyGainey

    AndyGainey

    Joined:
    Dec 2, 2015
    Posts:
    216
    I'm only learning myself, but I have recently been doing something similar, and found the static method Editor.CreateEditor(). I call that once for each object in the list, and then save each editor in a parallel list. Then I just have to go through the list of editors and call editor.OnInspectorGUI().

    The inability to serialize interface members also bit me, so I just gave up and started using abstract base classes instead, which is of course more limited given that .NET doesn't support multiple inheritance. You will probably also need to delve into the world of deriving these classes from ScriptableObject, because without doing so, each element of the list will be serialized as exactly the list element's type, not as the differing derived type that each element actually is. (Not to mention that without ScriptableObject, each reference to an object will get serialized as a separate copy, even if they were originally multiple references to the exact same object.) And going down the ScriptableObject rabbit whole will likely lead you to further fun with AssetDatabase.CreateAsset(), AssetDatabase.AddObjectToAsset(), and others.
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,702
    As @AndyGainey writes, you can serialize derived objects if they ultimately derive from ScriptableObject. I posted an example here. In that example, an Item has a list of Attributes, which are derived from ScriptableObjects.

    Use EditorGUI.PropertyField (or EditorGUILayout). Here's a simple example.
     
    danielrusnac likes this.
  4. CaseyLee

    CaseyLee

    Joined:
    Jan 17, 2013
    Posts:
    41
    I used Andy's method. Tony's assumes the original inspector's target has a reference to the instances you need drawn. But what if the instances are not serialized properties of the object you are drawing... for my case i need to get all children of the object that are a certain type, which carry different sub-types - even if the script on the parent had no direct reference to those scripts - i needed to draw them all as a list on the parent.