Game Development Reference
In-Depth Information
So, create a new folder named PropertyDrawers in Assets\Scripts\Editor
and create a new script named PopUpCustomPropertyDrawer , replacing its con-
tents with the following code:
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(PopUpAttribute))]
public class PopUpCustomPropertyDrawer : PropertyDrawer {
PopUpAttribute popUpAttribute {
get { return ((PopUpAttribute)attribute); } }
}
The preceding code gives us the basic framework for our custom property drawer (the
public property I've added isn't mandatory, but provides quick and easy access to the
underlying property type we are enabling). Next, we need to add the OnGUI function that
will draw our custom property UI using the following code:
public override void OnGUI(Rect position,
SerializedProperty prop, GUIContent label)
{
if (prop.propertyType != SerializedPropertyType.String)
{
throw new UnityException("property " + prop + " must be
string to use with PopUpAttribute ");
}
var popupRect = EditorGUI.PrefixLabel(position,
GUIUtility.GetControlID(FocusType.Passive), label);
var currentItem = prop.stringValue;
var currentIndex = popUpAttribute.value.Length - 1;
for (; currentIndex >= 0; currentIndex--)
{
if (popUpAttribute.value[currentIndex] == currentItem)
break;
}
Search WWH ::




Custom Search