How to tell if a SerializedProperty is part of an array in Unity?

Viewed 17

I have a struct Stat

[Serializable]
public struct Stat {
    public string key;
    public string value;
}

I want it to display in the editor as a single line property with the label when it is a single non-array field, but when it is a part of an array I want to hide the label, how can I do this?

1 Answers

This can be done in a PropertyDrawer as follows,

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label ) {
    if(!property.propertyPath.EndsWidth(']')) {// This tells if the property is a member of an array
        position = EditorGUI.PrefixLabel(position,label);
    // Remainder of Property Drawer
}
Related