I would like to find the first child property inside a a PropertyDrawer. In my current code I get value of the first enum to use as a label, but I need explicitly find the child property by name
how to find first property in a property drawer
[CustomPropertyDrawer (typeof (TempleIdleGenerator.RaceSlotItemFreq))]
public class RaceEnum : PropertyDrawer {
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
// use the default property height, which takes into account the expanded state
return EditorGUI.GetPropertyHeight(property);
}
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
EditorGUI.BeginProperty (position, label, property);
// Draw label
var labelProperty = property.FindPropertyRelative ("race");
int index = labelProperty.enumValueIndex;
label.text = labelProperty.enumDisplayNames [index];
EditorGUI.PropertyField(position, property, label, true);
EditorGUI.EndProperty ();
}
}
[System.Serializable]
public class RaceSlotItemFreq{
public Race race;
public List<SlotItemFreq> slotItemFreq;
}
I would like to replace the phrase
property.FindPropertyRelative (“race”)
with something that finds the first child.
Final code here
[CustomPropertyDrawer (typeof (TempleIdleGenerator.RaceSlotItemFreq))]
public class LabelEnum : PropertyDrawer {
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
// use the default property height, which takes into account the expanded state
return EditorGUI.GetPropertyHeight(property);
}
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
EditorGUI.BeginProperty (position, label, property);
// Draw label
var tempProp = property.Copy();
property.NextVisible (true);
int index = property.enumValueIndex;
label.text = property.enumDisplayNames [index];
property = tempProp;
EditorGUI.PropertyField(position, property, label, true);
EditorGUI.EndProperty ();
}
}
Cheers,
Grant