Game Development Reference
In-Depth Information
int selectedIndex = EditorGUI.Popup(popupRect,
currentIndex, popUpAttribute.value);
prop.stringValue = selectedIndex < 0 ? "" :
popUpAttribute.value[selectedIndex];
}
Walking through the preceding script is quite simple; it is described as follows:
• The class is decorated with a CustomPropertyDrawer attribute and the type
of class it is targeted at.
• As stated, the class is derived from the PropertyDrawer class.
• A helper property ( popUpAttribute ) gets the correct type of class from the
attribute property of the PropertyDrawer base class (optional).
• We override the OnGUI function for the property drawers.
• We then check whether the target property (the variable you will attach this to) is
of the correct type (in this case, a string). It returns UnityException if it is
not correct.
• A Rect variable is defined for where we want to draw the output from our prop-
erty drawer (a requirement to use the EditorGUI.Popup control).
• We get the current value for the property we are attached to and compare it with
the possible values for the item. We do this only because we have a list of options
and need to know which the current one is. For other types, this may not be
needed.
• We draw a pop-up control using the EditorGUI.Popup control.
• Lastly, we set the property we are attached to with the value the user has selected.
Note
We could have used an enum object instead of an array to give us a more programmatic
approach, in which case the preceding steps would be very similar. However, this ap-
proach allows us to set the scope of the selection for each property.
With the property and our custom property drawer in place, we can decorate the variables
in our class to achieve the result I pictured earlier, as follows:
public string Name;
[Range(10, 100)]
public int Age;
[PopUp("Imperial", "Independant", "Evil")]
public string Faction;
Search WWH ::




Custom Search