- Home /
How to find the first child serializedProperty inside a PropertyDrawer
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
Answer by Adam-Mechtley · Jan 19, 2017 at 01:02 PM
You probably want SerializedProperty.NextVisible()
property.NextVisible(true);
Ah I see I use it with Copy() var tempProp = property.Copy(); property.NextVisible (true); int index = property.enumValueIndex; label.text = property.enumDisplayNames [index]; property = tempProp;
Your answer
Follow this Question
Related Questions
How can I correctly draw properties instead of fields in a custom property drawer? 0 Answers
Draw custom Property Field in Custom Editor 1 Answer
Display Custom Inspectors for each class in a List<> 1 Answer
How to find property of a serializeObject that has the same name with a field of another property? 1 Answer
Getting PropertyAttribute from PropertyDrawer.OnGUI 0 Answers