I have a simple custom attribute which just sets names to array elements so it doesnt appear as element 0,element 1,etc instead they shows names that is given in a string array.
public class LabeledArrayAttribute : PropertyAttribute {
public readonly string[] names;
public LabeledArrayAttribute(string[] names) { this.names = names; }
public LabeledArrayAttribute(Type enumType) { names = Enum.GetNames(enumType); }
}
public class ColorManager : MonoBehaviour
{
public enum FollowColor {
Accent,
Background,
Border,
Button,
Disabled,
Error,
Foreground,
Header,
Highlight,
Hover,
Info,
Link,
Primary,
Secondary,
Selected,
Success,
Tab,
Tetriary,
Text,
Warning
}
[LabeledArray(typeof(FollowColor))]
public Color[] Colors = new Color[20];
}
[CustomPropertyDrawer(typeof(LabeledArrayAttribute))]
public class LabeledArrayDrawer : PropertyDrawer {
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
return EditorGUI.GetPropertyHeight(property, label, true);
}
public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label) {
try {
EditorGUI.BeginProperty(rect, label, property);
var path = property.propertyPath;
int pos = int.Parse(path.Split('[').LastOrDefault().TrimEnd(']'));
//Debug.Log($"Pos: {pos}, Name: {((LabeledArrayAttribute) attribute).names[pos]} , Path: {path}");
EditorGUI.PropertyField(rect, property, new GUIContent(((LabeledArrayAttribute) attribute).names[pos]), true);
EditorGUI.EndProperty();
} catch {
EditorGUI.PropertyField(rect, property, label, true);
}
}
}
now this looks perfect in the inspector but however the functionality is bugged. if i select the first element and change the color the changes are shown in the 2nd element and so on for elements upto length of enum but from 21st element they work fine.
in this image there is only one element so the color change is not visible
in this image there are 2 elements and even though the first one is selected changes are visible in second element
the 21st element and onwards work fine as intended