Skip to content

NamedArrayAttribute update #6

Description

@MikeyBally

Hi,

I've been enjoying using [NamedArray] and I made a couple of updates (via AI) to fix a couple of issues, so I'm including it here...

  1. It didn't work with multi-select.
  2. It didn't work with array of structs.

Hope this helps. Thanks!

    public class NamedArrayAttribute : PropertyAttribute
    {
        public Type TargetEnum;
        public NamedArrayAttribute(Type TargetEnum)
        {
            this.TargetEnum = TargetEnum;
        }
    }

    [CustomPropertyDrawer(typeof(NamedArrayAttribute))]
    public class NamedArrayDrawer : PropertyDrawer
    {
        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            // FIX 1: Pass 'true' instead of 'property.isExpanded'. 
            // Unity will internally calculate the correct foldout height for structs, 
            // preventing overlap bugs during multi-object selection.
            return EditorGUI.GetPropertyHeight(property, label, true);
        }

        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            // Replace label with enum name if possible.
            try
            {
                var config = attribute as NamedArrayAttribute;
            
                // Extract the index 
                string path = property.propertyPath;
                int startIndex = path.LastIndexOf('[');
                int endIndex = path.LastIndexOf(']');

                if (startIndex != -1 && endIndex != -1)
                {
                    string indexString = path.Substring(startIndex + 1, endIndex - startIndex - 1);
                    int pos = int.Parse(indexString);

                    // Get enum names
                    var enum_names = System.Enum.GetNames(config.TargetEnum);

                    // Ensure we are within the enum's bounds
                    if (pos < enum_names.Length)
                    {
                        var enum_label = enum_names[pos];
                        enum_label = ObjectNames.NicifyVariableName(enum_label);
                        
                        // FIX 2: Modify the existing label's text instead of making a new GUIContent.
                        // This preserves tooltips, prefab override bolding, and multi-object editing states.
                        label.text = enum_label;
                    }
                }
            }
            catch
            {
                // Fallback to default if something goes wrong
            }
            
            // FIX 3: Pass 'true' instead of 'property.isExpanded'.
            EditorGUI.PropertyField(position, property, label, true);
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions