22. Drawers

  04. Features No Comments

The classes that are responsible for drawing and handling the user-interaction logic of elements inside Power Inspector are called drawers.

You can think of drawers as Power Inspector’s unified and more powerful alternative to Unity’s custom editors and property drawers. and decorator drawers.

Full Backwards Compatibility

Power Inspector is fully backwards compatible with your existing collection of custom editors, property drawers and decorator drawers.

The drawers in Power Inspector can act as wrappers for all of the above, using them to draw the GUI in the inspector, and then augmenting them with additional Power Inspector-specific features such as Copy-Paste and Reset.

Nestable

The Drawers in Power Inspector are nestable in nature. Most drawers, outside the ones for simple built-in types, actually have multiple member drawers that handle most of the grunt work for them.
For example, the drawer for a GameObject can contain multiple member drawers to handle its components, all of which can contain multiple member drawers for handling their class members.

The fact that drawers are easily nestable makes it possible to build new drawers like you were working with lego blocks, utilizing the whole existing library of drawers to build a new drawer out of multiple members drawers.

No More Black Boxes

All drawers contain a wealth of information about themselves, like their label, type and value, their position inside the inspector view, and what member drawers they have.

The fact that the contents of custom editors are a total black box can be a very limiting factor, and makes advanced inspector features like content filtering very difficult to pull off.
But drawers unlock a whole new world of possibilities, since it is now for the first time possible to get detailed information about everything contained inside the inspector view.

Interface For Issuing Commands

All drawers contained inside the inspector view implement an interface called IDrawer. Through this interface, it is possible to issue any drawers various commands such as SetValue, Reset, OpenContextMenu and CopyToClipboard.

This again unlocks new possibilities for your editor tools, as it is now possible to communicate with contents of the inspector view on completely new level. Complex actions like “unfold everything inside the inspector” can now be done with just a few lines of code.

Easily Extendable

When you create a new custom editor, you usually have to start completely from scratch, manually fetching serialized properties for all the fields you want to expose, then manually drawing each one of them. To make even simple changes to the default editor, you can end up having to write a lot of boilerplate code.

With drawers I wanted to change this.

With drawers you can easily generate the default list of member drawers for any data you have with just one line of code. Once you have this, you can then modify it however you like. Maybe remove a couple of drawers from the beginning, sort the rest of them alphabetically by label, add a couple of new buttons in the middle, and replace that int drawer with a range drawer.

A big benefit of working in this way, modifying instead of starting from scratch, is that your custom drawers can adapt much better when you make changes to your target classes. This means that if you add a new field to your MonoBehaviour later on, you’re much less likely to need to have to go edit its custom drawer to have that field show up, than if you were working with custom editors.

Change Only What You Need

Another thing about drawers that makes them more easily extendable, is that their functionality has been divided under several different methods. While in custom editors pretty much all of your logic is stuffed inside a single OnInspectorGUI method, with drawers the logic has been split under many different methods. This means that you can only override the specific aspects of a drawer that you want to modify.

For example, you can override the BuildContextMenu method if you just want to add a couple of new items to the context menu. You can override the OnMouseover method if you just want to display a tooltip while the mouse cursor is positioned over the drawer. You can override the OnDoubleClick method if you just want to ping an some asset when the drawer is double-clicked.

 

See also: