Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

CustomPropertyDrawer and Inheritance / Polymorphism

Discussion in 'Immediate Mode GUI (IMGUI)' started by yumupdate, Jul 21, 2013.

  1. yumupdate

    yumupdate

    Joined:
    Nov 20, 2012
    Posts:
    29
    This is what I have (essentially):

    Code (csharp):
    1. public class Weapon
    2. { // some public fields and methods
    3. }
    4.  
    5. public class Axe : Weapon
    6. { // more public fields and methods
    7. }
    8.  
    9. public class Inventory : MonoBehavior
    10. {
    11.      Weapon weapon1;
    12. }
    I have a custom property drawer for every one of the classes inheriting Weapon. The inspector only seems to want to use a CustomPropertyDrawer for "Weapon", no matter which Weapon-inherited object I have assigned to the Inventory->Weapon object. Is there an easy way to fix that?

    Thanks!
     
    jpthek9 likes this.
  2. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    I believe you must use a subclass in the declaration because a default inspector isn't so smart.

    You could also make a custom inspector with your own logic (you can dynamically decide which type of object you are expecting to be dragged in).

    Code (csharp):
    1. private Axe _slot;
    2.  
    3. void OnGUI() {
    4.     _slot = EditorGUILayout.ObjectField("Axe: ", _slot, typeof(Axe));
    5. }
     
  3. yumupdate

    yumupdate

    Joined:
    Nov 20, 2012
    Posts:
    29
    I have the CustomPropertyDrawers setup like so, but it still doesn't work. Or were you suggesting something else?

    Code (csharp):
    1. [CustomPropertyDrawer (typeof(Weapon))]
    2. public class WeaponDrawer : PropertyDrawer {
    3. // OnGUI override
    4. }
    5.  
    6. [CustomPropertyDrawer (typeof(Axe))]
    7. public class AxeDrawer : WeaponDrawer{
    8. base.OnGUI(yadda, yadda, yadda);
    9. // OnGUI override for Axe
    10. }
     
  4. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    Never used the CustomPropertyDrawer attribute, sorry.
     
  5. yumupdate

    yumupdate

    Joined:
    Nov 20, 2012
    Posts:
    29
    No worries. I got tired of messing with it. I'm just constructing a larger CustomEditor and having each inherited object keep up with it's own OnGUI function to call during the CustomEditor manipulations. Works amazingly well :) I'm intrigued to know what I was doing wrong, but if anyone ever has a similar requirement, my recommendation is to go with the CustomEditor, no matter how much larger of a beast it will be ... everything just went so much more smoothly for me in comparison.
     
  6. BigRoy

    BigRoy

    Joined:
    Jan 2, 2014
    Posts:
    12
    Hey yumupdate, can you share code of how you ended up doing this? :)
     
  7. Matheuz

    Matheuz

    Joined:
    Aug 27, 2014
    Posts:
    8
    Any updates on this problem? PropertyDrawers and inheritance don't work well...
     
  8. codestage

    codestage

    Joined:
    Jul 27, 2012
    Posts:
    1,492
    jpthek9 and Arycama like this.
  9. Chazmundo

    Chazmundo

    Joined:
    Oct 4, 2016
    Posts:
    3
    Hi, I know I'm a little late to the party, but inspired by this answer and my own frustration:

    I've created a generic and easy to use solution for Polymorphic PropertyDrawers in Unity.

    I've posted the full details here (ready to Copy & Paste into your own project): https://forum.unity.com/threads/custompropertydrawer-for-polymorphic-class.824667/#post-6702670

    P.S. I am posting here as it's one of the top search results when looking for "unity polymorphic property drawer" so hopefully it'll help more people
     
    codestage likes this.
unityunity