Search Unity

A new exciting editor extension feature is coming for Unity 4 – Property Drawers!

What started out as a NinjaCamp project between Rune and I has now been included in Unity. Property Drawers significantly reduce the amount of work you have to do in order to customize how your scripts look in the Inspector. You no longer have to write an entire Custom Editor (although that is still supported as well). Instead, you can just apply appropriate attributes to variables in your scripts to tell the editor how you want those properties to be drawn.

This allows you to enforce sensible settings on your properties, create «intelligent» properties that gather data from a data source to show a set of valid options, make your scripts more developer friendly or just have a nice tidy up.

Let’s take a look at a script before and after applying some PropertyAttributes to spice things up.

Changing the appearance and behavior of the field controls is as easy as adding attributes to your fields as in the script below.

Here’s how it works. When the Inspector renders the component for your script, it will check for each property if there is a ProperyDrawer defined for the type of the property or if the property has an attribute that inherits from PropertyAttribute. If there is, then an associated PropertyDrawer will handle rendering of that single property. You are able to define both attributes and drawers to customize rendering and data exchange for a given property.

Writing your own PropertyAttribute and PropertyDrawer

Let’s take a closer look at RegexAttribute and its PropertyDrawer to show how it fits together. First off, we should define an attribute that derives from PropertyAttribute. This attribute can store any kind of data you wish and it is intended to later be used by the PropertyDrawer.

The new RegexAttribute can be applied to a field in a script as shown below.

So far we have just created an attribute which holds data. However this alone will not cause anything to render. We need to define a drawer for the attribute, so let’s implement a RegexDrawer. It has to derive from PropertyDrawer and it must have a CustomPropertyDrawer attribute which tells Unity which kinds of attributed fields it can draw controls for. In our case we want it to draw the regex controls for any strings with the RegexAttribute attached to it, so we specify that here.

Writing a PropertyDrawer for a custom Serializable Class

Sometimes you have a custom serializable class that is used in multiple different scripts, and you want to use some specific GUI for that class everywhere it’s used. Consider this ScaledCurve class:

Normally every instance of this class would be shown in the Inspector with a foldout, but you can create a PropertyDrawer to show the class with custom GUI. This GUI is then used everywhere the class is used. The Property Drawer used here places all the controls in one line. Here’s the image again so you can compare the look of the Range and Falloff properties without and with the custom PropertyDrawer:

To make a PropertyDrawer for a class, pass the type of that class to the CustomPropertyDrawer attribute of the PropertyDrawer:

Wrapping Up

Finally a few words about how to structure these classes. Your scripts remain in the asset folder as before. Your new attributes should also be placed in the asset folder just like any scripts, however a recommendation is to keep your asset folder organized, so consider creating an Attributes folder. Your new property drawers should be placed in an editor folder, inside your assets folder. Check out this image, hopefully it should make it pretty clear.

If you have open beta access to Unity 4, you can learn more about PropertyDrawers in your local copy of the Unity 4 Script Reference – just search for PropertyDrawer. Once Unity 4 is released, the information will be in the online version of the Script Reference as well.

That’s all! We’re excited to see what kinds of cool extensions you will create with Unity 4!

31 replies on “Property Drawers in Unity 4”

Yes!!! I’m totally OSD with custom editors, and always create tons of them. This feature is so smart and useful: love it! :)

Amazing feature! Really helpful.
Some change to implement an attribute to assign comments to our variables?
Something like:

[VariableCommentary(«Controls the speed of the character in meters per second»)]
public float speed = 2;

If not, this is possible to be written by our own with a custom attribute?
This would be just awesome!

Please report bugs using the bug reporter tool if you want them to actually get fixed. We don’t track bug reports in blog comments. :)

Well I seem to have found a workaround by using a CurveField directly instead of a PropertyField. The problem seems to be with PropertyField.

Another small bug… When the animationCurveValue is changed on a property (while in PropertyDrawer.OnGUI), the small image of the curve doesn’t change in the inspector. The correct curve does appear up in the curve editor, and the small image appears correctly after selecting a different object then back again (i.e. forcing the inspector to refresh).

[…] details and source code examples on Unity’s blog post: Related posts:Brace yourself, Unity 4 is coming…Unity3D online learning section incoming!Unite […]

Nice feature. Quick question, but can C#’s dictionaries appear as an editable variable on the inspector for Unity 4? Or will we have to spin our own?

> CustomPropertyDrawers seem to support multiple object editing by default. Is there any way to tell them not to?

No. Multi-object editing support is decided on a per-component basis, not a per-property basis. The best you can do for a property is detect if there’s multiple objects (property.serializedObject.isEditingMultipleObjects) and if so display a label saying that multi-object editing is not supported for the property.

> How can I get the height of the property drawers for the items, so I can calculate the overall height of the array?

Hmm, we’ll expose a function to get the height of a property. It’ll be in one of the next betas.

Another thing I’m stuck on… I have a custom property drawer which is displaying an array of items. The items in the array also have custom property drawers. How can I get the height of the property drawers for the items, so I can calculate the overall height of the array?
I almost succeeded in doing this via reflection but got stumped when I found the type is not exposed on the CustomPropertyDrawer attribute so I have no way of finding what types a PropertyDrawer handles.

> I’m betting this will also be clever and for prefab instances correctly bold your property, and support Undo in the correct way?

Yes, if you follow the guidelines on how to work with SerializedProperties. The PropertyDrawers are no different from creating custom editors in that regard.

> Do you think you could also support buttons?

The PropertyDrawers can display any GUI you want for your properties (script variables). It can’t show GUI that’s not associated with any properties. If you need to do that, write a custom editor.

Awesome! This is a really great feature! I’m betting this will also be clever and for prefab instances correctly bold your property, and support Undo in the correct way?

Do you think you could also support buttons? For instance, you can use [ContextItem()] to add a function, but for the level designer its rather hidden away and requires 2 clicks. A button would be more easily accessible.

> What happens if attribute and property are mismatched ? In this case, what is the best practice ?

Make your PropertyDrawer check the type of the SerializedProperty and make it show an error label if it’s not the right type.

@This looks really great but it would be wonderfull if inspector properties were created from C# properties instead of public fields. This would really clean the design of the scripts….

You can serialize private variables using [SerializeField].

Properties can unfortunately not be serialized since we would be C# code from other threads. It is very easy to introduce hard to find crashes and bugs this way.

This looks really great but it would be wonderfull if inspector properties were created from C# properties instead of public fields. This would really clean the design of the scripts….

I would have to join the consensus that this is one of the most fundamental changes coming with Unity4. The scope of larger projects and not being a programmer myself this is a true blessing. Great work and thankyou!

[…] های سایت آخرین اخبار یونیتی بلاگ یونیتی Property Drawers in Unity 4Made in Brighton!Hang out with Unity #1Learn Unity coming soon.Unite 12 Day 3 Summary آمار […]

One of the best features of unity4. You know, mechanim and dx11 are great but those are not useful for all projects. these usability features like custom inspectors in this new ways and prefab property exposing and component managment features are really great additions to any project.

Fantastic guys..I love the ninjacamp projects. I wish more come into unity. Like the CodeEditor for example :-P
Again great work and usefull.

> Wow that’s great and unexpected. One question, will this “just work” with the new prefab exposed property sheet?

Yes. It’s magic.

Wow that’s great and unexpected. One question, will this «just work» with the new prefab exposed property sheet?

Comments are closed.