/
InlineDataInInspectorAttribute.cs
94 lines (75 loc) · 2.77 KB
/
InlineDataInInspectorAttribute.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using UnityEngine;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace GUIAttributes
{
public class InlineDataInInspectorAttribute : MultiPropertyAttribute
{
#if UNITY_EDITOR
#region Properties and Fields
private const float TOP_SPACING = 3;
private const float TITLE_SPACING = 5;
private const float PROPERTY_SPACING = 2;
private const float BOTTOM_SPACING = 5;
private GUIStyle _boldLabel;
private GUIStyle BoldLabel
{
get
{
if (_boldLabel == null)
{
_boldLabel = new GUIStyle(GUI.skin.label) { fontStyle = FontStyle.Bold };
}
return _boldLabel;
}
}
#endregion
public override float? GetPropertyHeight(SerializedProperty property, GUIContent label)
{
float propertyHeight = TOP_SPACING + BoldLabel.CalcSize(label).y + TITLE_SPACING;
foreach (SerializedProperty visibleChild in VisibleChildren(property))
{
propertyHeight += EditorGUI.GetPropertyHeight(visibleChild, true) + PROPERTY_SPACING;
}
propertyHeight += BOTTOM_SPACING;
return propertyHeight;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
Rect drawRect = new Rect(position);
drawRect.y += TOP_SPACING;
GUIStyle style = BoldLabel;
float labelHeight = style.CalcSize(label).y;
drawRect.height = labelHeight;
EditorGUI.LabelField(drawRect, label, style);
drawRect.y += labelHeight + TITLE_SPACING;
++EditorGUI.indentLevel;
foreach (SerializedProperty visibleChild in VisibleChildren(property))
{
float childHeight = EditorGUI.GetPropertyHeight(visibleChild, true);
drawRect.height = childHeight;
EditorGUI.PropertyField(drawRect, visibleChild, true);
drawRect.y += childHeight + PROPERTY_SPACING;
}
--EditorGUI.indentLevel;
}
private IEnumerable<SerializedProperty> VisibleChildren(SerializedProperty property)
{
SerializedProperty nextSiblingProperty = property.Copy();
nextSiblingProperty.NextVisible(false);
if (property.NextVisible(true))
{
do
{
if (SerializedProperty.EqualContents(property, nextSiblingProperty))
yield break;
yield return property;
}
while (property.NextVisible(false));
}
}
#endif
}
}