-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInlineAttribute.cs
More file actions
89 lines (69 loc) · 2.64 KB
/
InlineAttribute.cs
File metadata and controls
89 lines (69 loc) · 2.64 KB
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEditor;
using UnityEngine;
namespace Jackey.Utilities.Attributes {
/// <summary>
/// Draw all serialized fields of an object on one line
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
[Conditional("UNITY_EDITOR")]
public class InlineAttribute : PropertyAttribute {
public bool PrefixLabel { get; set; } = true;
public bool FieldLabels { get; set; }
}
#if UNITY_EDITOR
namespace PropertyDrawers {
[CustomPropertyDrawer(typeof(InlineAttribute))]
public class InlinePropertyDrawer : PropertyDrawer {
private bool m_labelsInitialized;
private GUIContent[] m_subLabels;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
InlineAttribute attr = (InlineAttribute)attribute;
EditorGUI.BeginProperty(position, label, property);
if (!property.hasVisibleChildren) {
EditorGUI.PropertyField(position, property, attr.PrefixLabel ? label : GUIContent.none, true);
EditorGUI.EndProperty();
return;
}
if (!m_labelsInitialized)
InitializeLabels(property);
if (attr.PrefixLabel)
position = EditorGUI.PrefixLabel(position, label);
property.NextVisible(true);
EditorGUI.MultiPropertyField(position, m_subLabels, property, GUIContent.none);
EditorGUI.EndProperty();
}
private void InitializeLabels(SerializedProperty property) {
InlineAttribute attr = (InlineAttribute)attribute;
SerializedProperty propertyIterator = property.Copy();
int propertyDepth = propertyIterator.depth + 1;
List<GUIContent> subLabels = new();
for (bool enterChildren = true; propertyIterator.NextVisible(enterChildren); enterChildren = false) {
if (propertyIterator.depth != propertyDepth) break;
if (attr.FieldLabels)
subLabels.Add(new GUIContent(ObjectNames.NicifyVariableName(propertyIterator.name)));
else
subLabels.Add(GUIContent.none);
}
m_subLabels = subLabels.ToArray();
m_labelsInitialized = true;
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
float maxHeight = EditorGUIUtility.singleLineHeight;
if (property.hasVisibleChildren) {
int basePropertyDepth = property.depth;
for (bool enterChildren = true; property.NextVisible(enterChildren) && property.depth > basePropertyDepth; enterChildren = false) {
maxHeight = Mathf.Max(maxHeight, EditorGUI.GetPropertyHeight(property, true));
}
}
else {
maxHeight = EditorGUI.GetPropertyHeight(property, true);
}
return maxHeight + EditorGUIUtility.standardVerticalSpacing;
}
}
}
#endif
}