How to acces type of generic parameter in PropertyDrawer?
Hello! I have a generic class GenericClass<T>
for which I have a PropertyDrawer. I know that class by itself won't serialize, but I'm currently deriving from it, and thus, can see the child classes in the inspecot (yay), but that's not what I'm here to ask about.
I'm very new to PropertyDrawers, but I know you can create a drawer for a generic class by using the following syntax:
[System.Serializable]
public class GenericClass<T>
{
}
[CustomPropertyDrawer(typeof(GenericClass<>), true)]
public class GenericClassDrawer : PropertyDrawer
{
}
My question is: Can I access the generic type itself from the drawer component? For example, if I have a GenericClass<float>
, is there any way for the property drawer to know it's dealing with a float?
Thanks in advance!
Answer by AlexDavies · Jun 30, 2020 at 05:03 PM
There is probably a way to do it via reflection (maybe by traversing the root object like in this post and then using GetGenericTypeDefinition()
), but AFAIK it's impossible via the current SerializedProperty implementation. TBH the serialization system could do to be rewritten. I've heard Odin does it better, but it's still unacceptable that better serialization isn't available out of the box.
Your answer
Follow this Question
Related Questions
Can I make a custom property drawer for Unity types? 0 Answers
PropertyDrawer for Array 2 Answers
Custom inspector popup for creating a item list 0 Answers
OnPreviewGUI for Canvas and UI Elements 0 Answers