I’m using a Unity version that’s a couple years old (2021.3) so forgive me if this is already a thing.
The editor’s custom property drawer system is pretty useful and seems to work well for the most part. However, I’ve noticed what seems like a silly quirk in it: the overridable GetPropertyHeight function doesn’t take a width parameter. This makes it so a custom drawer that changes its height based on its width is basically impossible to implement.
Some examples of where this would be useful:
A drawer with a word-wrapped text box. In particular, this is useful when the length of the contents of the text box is not known at compile time, but even if the length is known, the text may still be cut off if the Inspector view became narrow enough.
A drawer with “element wrapping”, where elements are arranged in a row that wraps around when the right side of the display rect is reached. An example of this would be the U2D SpriteLibraryAsset drawer, which currently uses a box with a constant height and a scrollbar.
The constant height with a scrollbar approach is, I suppose, a workable solution, but it’s one that I find clunky. When putting several sprites in a category of a Sprite Library Asset, for instance, I have to be sure to carefully drag a sprite into the category box from the left so that it doesn’t scroll to a position other than the one I want.
I don’t see why adding another method wouldn’t work, although, to be fair, I don’t know the specific implementation details.
I tried the solution you suggested. The problem with that is, every time I show my object in the inspector, my drawer’s parent calls GetPropertyHeight on my drawer before my drawer is drawn, so the parent drawer draws at the wrong height until I force it to redraw by editing something inside it. I can’t figure out how to force that redraw from code.
The box with wrapped text is marked in red. Green is my custom drawer, and blue is its parent, which is not tall enough until I collapse and expand it again.
Simply forcing a repaint doesn’t work – the parent property drawer apparently caches the result it gets from GetPropertyHeight until something inside is edited or it is collapsed, because the repaint I forced doesn’t result in another call to GetPropertyHeight, and the height of the parent drawer is still wrong.
What I ended up doing was grabbing the serialized object (in this case, the Boss component), then checking if its sequences property is expanded. If it is, I force it to collapse, then register a delayed callback to force it to expand again. This invalidates the cached property heights and forces the actions drawer to the right height. This is really stupid and hacky, but it gets the job done.