Search Unity

Property Drawer on Extended Class

Discussion in 'Immediate Mode GUI (IMGUI)' started by popMark, Jul 24, 2015.

  1. popMark

    popMark

    Joined:
    Apr 14, 2013
    Posts:
    114
    I'm having trouble getting a property drawer to draw an extended class, the setup is something like this;

    Code (CSharp):
    1.  
    2. [System.Serializable]
    3. public class Animal {}
    4.  
    5. [System.Serializable]
    6. public class Dog : Animal {
    7.     public float woofVolume;
    8. }
    The class that uses the Dog/Animal class;

    Code (CSharp):
    1. public class Kennel : MonoBehaviour {
    2.     public Animal animal = new Dog();
    3. }
    And the property drawer;

    Code (CSharp):
    1. [CustomPropertyDrawer(typeof(Dog))]
    2. public  class DogDrawer : PropertyDrawer {
    3.     public override void OnGUI (Rect position,
    4.                                 SerializedProperty property,
    5.                                 GUIContent label)
    6.     {
    7.         EditorGUI.BeginProperty (position, label, property);
    8.         GUI.Button(position, "Woof");
    9.         EditorGUI.EndProperty ();
    10.     }
    11. }
    So basically I want the inspector to display the property drawer for Dog when the animal property of kennel is a Dog, if I had a Cat and the appropriate Drawer for a Cat I would want the inspector to use the PropertyDrawer for Cat. Is this achievable with the property drawer system? Currently I can only get it to draw if I make property drawer use typeof(Animal)
     
  2. popMark

    popMark

    Joined:
    Apr 14, 2013
    Posts:
    114
    For my case I can work around this with a switch but its not the dynamic scalable solution i wanted, still would like to know if theres a solution
     
  3. MattRix

    MattRix

    Joined:
    Aug 23, 2011
    Posts:
    121
    Does your dog's woofVolume actually get serialized and deserialized properly? I was under the impression that using a subclass in a serializeable field like that will only deserialize as the super class.
     
  4. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    You are missing an argument

    Code (CSharp):
    1.   [CustomPropertyDrawer(typeof(Animal), useForChildren: true)]
    cheers,