Search Unity

Custom Inspector : List with several type of Class

Discussion in 'Immediate Mode GUI (IMGUI)' started by xXApOXx, Jul 18, 2015.

  1. xXApOXx

    xXApOXx

    Joined:
    Feb 9, 2015
    Posts:
    76
    Hi,

    I'm looking for some help with the custom inspector.

    I have those classes : SpellEffect / SpellEffectSpeed / SpellEffectLife / SpellEffectLifeOverTime

    SpellEffect is the parent of the 3 others and it's [System.Serializable]. Then I have a List<SpellEffect> that can contains all of those 3 children.

    And I can't have a custom inspector that allow me to add from the editor one of them. The only thing that I can, it's add a SpellEffect.

    Thx for any solutions !
     
  2. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    This is because you have a list of Spell Effects. Polymorphism is not supported with serialization.
     
  3. AhrenM

    AhrenM

    Joined:
    Aug 30, 2014
    Posts:
    74
    From a practical perspective BMayne is correct. Technically serialization of polymorphic objects is supported, if the classes derive from GameObject (ScriptableObject?), but I don't think that's going to help your in this case. Without serious work an inspector is certainly going to choke on a polymorphic list, so your problem remains.

    A couple of options would be to compress your classes down to a single SpellData and maybe include an enum flag that says how to interpret the data, or possibly re-work your classes in Components and use a GameObject as your list container. Both of these sound messy.

    Are the contents of your Spell* classes directly affecting game engine objects or are they largely data classes? If the latter, the maybe the GO/Inspector approach isn't the best way to go??
     
  4. xXApOXx

    xXApOXx

    Joined:
    Feb 9, 2015
    Posts:
    76
    For now I can use an enum to differenciate my Spells but I will probably upgrade each of them later and I wanted a system that don't make me rewrite the code that manage the spell effects.

    So I was thinking of a simple editor tool with button for each kind of spell effect I have and will have later. When I click on one those buttons, I can params the spell effect and add it to the List.

    And for the inspector, I just need to see what there are in the List, no matter if I can't add directly a spell effect on it.
     
  5. xXApOXx

    xXApOXx

    Joined:
    Feb 9, 2015
    Posts:
    76
    I'm a little confused. I have this code in my custom inspector :

    Code (csharp):
    1.  
    2. foreach (SpellEffect se in mySpellBehavior.effects)
    3. {
    4.       Debug.Log(se.GetType());
    5. }
    6.  
    7. type = (SpellEffectType)EditorGUILayout.EnumPopup("Effect", type);
    8. if (GUILayout.Button("Add New Effect"))
    9. {
    10.       switch (type)
    11.       {
    12.                 case SpellEffectType.SpellEffectLife:
    13.                     mySpellBehavior.effects.Add(new SpellEffectLife(0));
    14.                     break;
    15.                 case SpellEffectType.SpellEffectLifeOverTime:
    16.                     mySpellBehavior.effects.Add(new SpellEffectLifeOverTime(0, 0.0f));
    17.                     break;
    18.      }
    19. }
    20.  
    It works fine, the log(se.GetType()) display the right type. But once I launch the game and I back to edit, the log(se.GetType()) doesn't work anymore, it log SpellEffect only...
     
  6. xXApOXx

    xXApOXx

    Joined:
    Feb 9, 2015
    Posts:
    76
    mayntos likes this.