forked from Deadcows/MyBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AttributeBaseDrawer.cs
62 lines (51 loc) · 1.49 KB
/
AttributeBaseDrawer.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
62
#if UNITY_EDITOR
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace MyBox.Internal
{
[CustomPropertyDrawer(typeof(AttributeBase), true)]
public class AttributeBaseDrawer : PropertyDrawer
{
private AttributeBase[] _cachedAttributes;
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
CacheAttributes();
for (var i = _cachedAttributes.Length - 1; i >= 0; i--)
{
var overriden = _cachedAttributes[i].OverrideHeight();
if (overriden != null) return overriden.Value;
}
return base.GetPropertyHeight(property, label);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
CacheAttributes();
bool drawn = false;
for (var i = _cachedAttributes.Length - 1; i >= 0; i--)
{
var ab = _cachedAttributes[i];
ab.ValidateProperty(property);
ab.OnBeforeGUI(position, property, label);
// Draw the things with higher priority first. If drawn once - skip drawing
if (!drawn)
{
if (ab.OnGUI(position, property, label)) drawn = true;
}
ab.OnAfterGUI(position, property, label);
}
if (!drawn) EditorGUI.PropertyField(position, property, label);
}
private void CacheAttributes()
{
if (_cachedAttributes.IsNullOrEmpty())
{
_cachedAttributes = fieldInfo
.GetCustomAttributes(typeof(AttributeBase), false)
.OrderBy(s => ((PropertyAttribute) s).order)
.Select(a => a as AttributeBase).ToArray();
}
}
}
}
#endif