Game Development Reference
In-Depth Information
66 //----------------------------------------------
67 //This function returns how high (in pixels) the field should be
68 //This is to make controls not overlap
69 public override float GetPropertyHeight (SerializedProperty
property, GUIContent label)
70 {
71 return InspectorHeight;
72 }
73 //----------------------------------------------
74 }
75 //----------------------------------------------
The following are the comments for code sample 8-10:
Line 08 : Notice that the CustomPropertyDrawer attribute is now associated
with a regular class as opposed to an attribute. In this case, the rendering of
a specific class is being customized for the Object Inspector as opposed to
various properties of different types, which can share a common attribute.
Lines 12-18 : Some public members are declared, primarily to calculate the
height (in pixels) of a single row in the Object Inspector. By default, the
Object Inspector allocates one row (or line) for our custom rendering and
all drawing is supposed to fit within that space. If the total height of our
rendering exceeds the height of one line, all additional controls and data will
overlap and mix with controls and widgets beneath. To address this problem,
the GetPropertyHeight (at line 69) function can be used to return a pixel
height allocated for our custom drawing.
Lines 26-27 : These lines are especially important. They use reflection to
retrieve a type-correct object reference to the ClassWithProperties instance
currently being drawn for this call to OnGUI . Specifically, a reference to
targetObject is retrieved (the object selected), and then an instance to
ClassWithProperties is retrieved from that. The result is that this code
gives us direct and immediate access to the ClassWithProperties object.
Lines 37-58 : Each public property on the object is cycled in sequence, and for
valid or supported data types, an inspector property is drawn that allows
both read/write access to the property, provided the property itself supports
both methods.
 
Search WWH ::




Custom Search