Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. Unity 2017.2 is now released.
    Dismiss Notice
  3. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  4. If you're a macOS 10.13 High Sierra user take be sure to read this before updating Unity.
    Dismiss Notice
  5. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  7. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Custom Inspector: Draw standard editor for serialized class

Discussion in 'Extensions & OnGUI' 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.
     
    CaseyLee and IgorAherne like this.
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    7,314
    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.
     
  4. CaseyLee

    CaseyLee

    Joined:
    Jan 17, 2013
    Posts:
    33
    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.