Game Development Reference
In-Depth Information
The following are the comments for code sample 8-6:
•
Line 01
: The
ColorRangeAttribute
class defines a metadata
structure that we can tag to other data types. Notice that it derives
from
PropertyAttribute
. This signifies, above everything else, that
ColorRangeAttribute
is an attribute and metadata structure but not a
regular class. It's not supposed to be instantiated as a standard class is.
•
Line 07
: The attribute has a constructor function that accepts eight
floating-point values defining the RGBA channels for the source and
destination colors of the Lerp. These will be used soon when attaching
the attribute to a variable.
Now, we'll write a class declaring an instance of
ColorBlend
with the
ColorRangeAttribute
attribute attached. Even now, however, the addition
of
ColorRangeAttribute
will do nothing per se because no
Editor
class has
yet been written to handle it. We can see this in the following code:
public class ColorAdjuster : MonoBehaviour
{
[ColorRangeAttribute(1f,0f,0f,0f, 0f,1f,0f,1f)]
public ColorBlend MyColorBlend;
}
Creating an
Editor
class for rendering
ColorBlend
in the Object Inspector with a
slider control involves handling the
ColorRangeAttribute
class. Specifically, Unity
offers us the extension
PropertyDrawer
base class from which we can derive new
classes to override the Object Inspector rendering for any specific attribute we add
to our variables. In short, the
PropertyDrawer
class lets us customize inspector
drawing for any and all variables tagged with a common attribute. Therefore, inside
the
Editor
folder of your project, create a new
ColorRangeDrawer
class, as shown in
the following code sample 8-7:
01 using UnityEngine;
02 using UnityEditor; //Be sure to include UnityEditor for all
extension classes
03 using System.Collections;
04 //------------------------------------------------------------
05 //CustomPropertyDrawer attribute for overriding drawing of all
ColorRangeAttribute members
06 [CustomPropertyDrawer(typeof(ColorRangeAttribute))]