I’m writing a UI theme API which allows individual components to inherit properties (i.e. color, font, and etc.) from an assigned theme, or overriding them.
To provide such a functionality, I need to allow setting any such properties to a null value which is problematic for struct types since they do not support such a notion.
In my code, I can easily use C#'s ‘?’ suffix to declare a struct type as a nullable property, like ‘Color?’. But the problem is, Unity doesn’t seem to support such types so it doesn’t show proper property drawers for nullable properties.
Of course, I can write custom drawers for them, but it’d be quite tedious job to duplicate every such types, like Color, Vector3, every enums, and etc.
Is there any way I can write some generic property drawer which can handle all such nullable struct types?
Thanks!
I thought a bit about it, and can’t think of an easy solution. the ‘?’ suffix is basically just making it Nullable. So float? is just Nullable. As Nullable has a generic parameter (duh, the type of the value you want to make nullable), visualizing this in Unity will be tough as their serialisation doesn’t this. You could go into some different ways of doing this (Serializable class using generics - Questions & Answers - Unity Discussions), but it would probably defeat the whole purpose. Maybe someone else has an elegant solution, I’d be interested 
You can think of putting the Color, Vector3 and such in a class, and write a drawer for that. Or maybe even have booleans yourself that note if the values are set or “null”. As that’s pretty close to making it actually nullable.
@marijnz Thanks for the suggestion. I ended up with writing my own version of Nullable and a matching property drawer for each structs, as you suggested.
I really hope there could be more elegant way to do it, so please let me know if anyone finds a way to achieve this without writing a value/drawer pair per each structs.
1 Like