Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.

    Read our full announcement for more information and let us know if you have any questions.

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


    Nov 12, 2013

    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


    Dec 2, 2015
    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


    Apr 10, 2012
    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


    Jan 17, 2013
    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.