CustomPropertyDrawer for Generic Type

0 votes
asked Aug 15, 2015 in Programming by ismix Senior (1,150 points)

I need to be able to create a custom property drawer for a custom generic type.

[CustomPropertyDrawer(typeof(CustomType<float>))]

But with the generic style it doesn't work, there's no compile nor a runtime error.

How can I achieve this? I been racking my head for hours....

1 Answer

+2 votes
answered Aug 15, 2015 by Bugstacker Senior (4,800 points)
selected Aug 15, 2015 by ismix
 
Best answer

Unfortunately the Unity Property Draw System does not directly support generic types.

But however you can make a dirty hack and extend a generic class in a non-generic subclass. To get the drawer working for all sub classes you should also have non-generic base class.

    [Serializable]
    public class CustomBaseType
    {
    }


    [Serializable]
    public class CustomGenericType<T> : CustomBaseType
    {

        public T Value;
    }

    [Serializable]
    public class CustomFloatType : CustomGenericType<float>
    {
    }


    [Serializable]
    public class CustomIntegerType: CustomGenericType<int>
    {
    }

    [CustomPropertyDrawer(typeof(CustomBaseType), true)]
    public class CustomDrawer : PropertyDrawer
    {

        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {

             // Your logic goes here

        }

    }

This way you can draw custom properties for generic types. It is not a proper OO design, but it works.

 

commented Aug 15, 2015 by ismix Senior (1,150 points)
Thank you very much! This way it works!!
Welcome to Bugstacker Q&A, where you can ask questions concerning Game Development, Programming and Design and receive answers from other members of the community.

Help for

...