Game Development Reference
In-Depth Information
First, we need a property type or attribute that we want to control. This could be a set of
parameters (such as the
Range
property, which has a beginning and an end), a validation
string, or even an enumeration.
Note
The property type or attribute you want to control has to live in your project folder and not
in the special
Editor
folder. The Unity documents are not clear enough on this.
So, create a new folder named
Properties
in
Assets\Scripts\Classes
. Then,
create a new C# script named
PopupAttribute
in the
Properties
folder and re-
place its contents with the following code:
using UnityEngine;
public class PopUpAttribute: PropertyAttribute
{
public string[] value;
public PopUpAttribute(params string[] input)
{
value = input;
}
}
Note that your
property
class must be derived from the
PropertyAttribute
class,
and it must have a constructor with the same number of parameters required for your at-
tribute (for example, the
Range
attribute has two
int
values).
Tip
In a strange (I suspect reflection) circumstance, you can either call your class by its name
or suffix it with the word
Attribute
(as shown in the preceding code); both will be re-
cognized by the name alone.
For example,
PopUpAttribute
can be recognized as
PopUp
or
PopUpAttribute
.
With the property in place, we can now add our custom property drawer code. Unlike the
property we just created, this does have to live in the special
Editor
folder.