Posts tagged with: Custom Inspector. Property Drawer

Unity3D Tutorials: Custom Editor, Inspector, Property Drawers and Editor Windows

Note: This post will be regularly updated with other examples. Make sure to return later. Last edit: 31/Jan/2015

There are a few official tutorials [1][2] and videos about this subject, and others made by community. The thing is, the manual itself doesn’t cover a lot of things, it uses mostly UnityScript, and many other things lacks of examples. In this post, I’ll try to keep it always updated with my last codes.

First, lets start with the basics, and I assume you know Unity3D and C# well. The default Unity inspector have a few properties bars built-in, and sometimes it’s not enough. The not-so-new Unity 4 introduced Property Drawers. If you have an unique property in diferent objects, then that’s what you need. Let’s take a look on how default built-in properties works.

My Enemy class:

That gives me the following inspector:
Custom Inspector - Default

Now let’s add a “power attack” to our enemy. It will be a simple Serializable Class. Check the following code:

Before talking about Property Drawers, you must know that you can create a custom PropertyAttribute, just like Attributes in C#. Range and Multiline are built-in, but you can create your own, and combine with Property Drawers. As far as I can tell, PropertyAttribute’s can only deal with one property. Property Drawers can combine PropertyAttribute, and/or other properties for a better display.

So, I added a struck and an enum type. The special attack its level-type property. Lets say the enemy will attack with Melee until level 20, Range between 21 and 30, and Magic from level 31. So, let’s create our SpecialAttack Custom Property Drawer


Explaining: I store the original Rect position given by my PropertyDrawer, and I do some modifications with the axis X and width. This way, I can display the 2 properties in one line. The EditorGUI.PropertyField can guess what type of variable it is, thats why it renders Popups with Enum, TextField’s for strings, and so on. You can force it to draw an IntSlider instead of a simple textfield. We will talk about this in another topic.

Level/Type Property

Ok, what about forcing Type according to Level value? I’m just doing that to demonstrate how we can manipulate values inside a Property Drawer. If it was real case, I would probably use some Label to display the Type, because changing it would have no value because its linked to level value. Anyway, here’s an updated code:

Also I am forcing the Level values between 1 and 50. I could also do that using [Range(1,50)] inside SpecialAttack class, like this:

Then I am forcing the Type value using simple if checks. Hope it helps… use the comment section!

Did you like this? Share it: