Game Development Reference
In-Depth Information
The following are the comments for code sample 8-7:
•
Line 01
: The
CustomPropertyDrawer
attribute is used here to associate
the
PropertyDrawer
class with the
ColorRangeAttribute
attribute. The
Unity Editor uses this metadata internally to determine which types require
custom rendering in the Object Inspector. In this case, all members with
ColorRangeAttribute
will be drawn manually by the
OnGUI
function of the
PropertyDrawer
class.
•
Line 11
: The
OnGUI
function is overridden from the base class to define how
all fields with
ColorRangeAttribute
should be rendered in the Object
Inspector.
EditorGUI
is a native Unity Editor utility class for drawing GUI
elements, such as buttons, textboxes, and sliders. For more information on
•
Line 14
: The
OnGUI
function is called once, perhaps many times per second,
for each unique member to render manually in the Object Inspector. The
attribute data for
ColorRangeAttribute
is retrieved here with typecasting,
and this gives us access directly to all its members for the current object
being rendered. To access the member variables of the object itself (for read/
write access), as opposed to its attribute, the
SerializedProperty
argument
should be used, such as the
FindPropertyRelative
method. For more
information, see the online Unity documentation at
http://docs.unity3d.
•
Line 24
: From here onwards, the
FindPropertyRelative
function is called
to retrieve public member variables, such as the
SourceColor
,
DestColor
,
and
BlendedColor
in the selected object. This is where the values are
actually set by moving the slider component.
More information on the
PropertyDrawer
class can be found in the
online Unity documentation at
http://docs.unity3d.com/Manual/
The code sample 8-7 overrides the Object Inspector drawing for any
ColorBlend
instances when tagged with the
ColorRangeAttribute
attribute. This offers an
accessible and easy-to-use way of creating blended colors. Remember, you can
make your source and destination colors public, in order to be accessible from the
Inspector
tab, as shown here: