Draw parent in a custom property drawer

Hello

I’m working on creating a custom inspector for a series of classes in Unity, and I’ve run into a challenge.

I want to create a custom property drawer for a class that inherits from a base class. My problem is that while I can access the base class variables to draw them, I have to repeat this process in every child class’s property drawer. This approach quickly becomes unmaintainable, especially if I need to change or add variables in the base class.

Is there a way to create a base property drawer that automatically draws the inherited properties of the base class without having to modify each child class?

Thank you!

Two options I can think of.

Firstly, if you have specific code for drawing the the base class stuff, just encapsulate that into a method/visual element and simply call/add that in your derived drawer. Using UI Toolkit you could have a base visual element, and can derive from that element in your derived type drawers.

Secondly, by looping through each of the child serialized properties and drawing them. Some googling gets me this example: SerializedObject: How-to

Ideally you are drawing things via correctly bound serialized properties so the undo and dirtying is handled automatically.

2 Likes