-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathNamedArrayAttribute.cs
61 lines (57 loc) · 1.87 KB
/
NamedArrayAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
///
/// NamedArrayAttribute, originally made by idbrii, found at:
/// http://answers.unity.com/answers/1472176/view.html
///
/// Replaces array element labels with enum names in the inspector.
///
/// Example:
///
/// [NamedArray(typeof(LiquidType))]
/// public Color[] liquidColors;
///
using UnityEngine;
using System;
#if UNITY_EDITOR
using UnityEditor;
#endif
// Defines an attribute that makes the array use enum values as labels.
// Use like this:
// [NamedArray(typeof(eDirection))] public GameObject[] m_Directions;
public class NamedArrayAttribute : PropertyAttribute
{
public Type TargetEnum;
public NamedArrayAttribute(Type TargetEnum)
{
this.TargetEnum = TargetEnum;
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(NamedArrayAttribute))]
public class NamedArrayDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
// Properly configure height for expanded contents.
return EditorGUI.GetPropertyHeight(property, label, property.isExpanded);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// Replace label with enum name if possible.
try
{
var config = attribute as NamedArrayAttribute;
var enum_names = System.Enum.GetNames(config.TargetEnum);
int pos = int.Parse(property.propertyPath.Split('[', ']')[1]);
var enum_label = enum_names.GetValue(pos) as string;
// Make names nicer to read (but won't exactly match enum definition).
enum_label = ObjectNames.NicifyVariableName(enum_label.ToLower());
label = new GUIContent(enum_label);
}
catch
{
// keep default label
}
EditorGUI.PropertyField(position, property, label, property.isExpanded);
}
}
#endif