-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExposeObjectAttribute.cs
More file actions
136 lines (103 loc) · 4.6 KB
/
ExposeObjectAttribute.cs
File metadata and controls
136 lines (103 loc) · 4.6 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Diagnostics;
using UnityEngine;
using Debug = UnityEngine.Debug;
using Object = UnityEngine.Object;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Jackey.Utilities.Attributes {
/// <summary>
/// Expose the properties of the assigned object directly in the inspector
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
[Conditional("UNITY_EDITOR")]
public sealed class ExposeObjectAttribute : PropertyAttribute { }
#if UNITY_EDITOR
namespace PropertyDrawers {
[CustomPropertyDrawer(typeof(ExposeObjectAttribute))]
public class ExposeObjectPropertyDrawer : PropertyDrawer {
private readonly Color FOLDOUT_GUIDE_COLOR = new(0.39f, 0.4f, 0.39f);
private SerializedObject m_exposedSerializedObject;
private float m_propertyHeight;
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
// Object Field
float totalHeight = EditorGUIUtility.singleLineHeight;
if (!property.isExpanded || property.objectReferenceValue == null)
return totalHeight;
// HelpBox
if (property.hasMultipleDifferentValues)
totalHeight += 2f * (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing);
// Object Properties
if (!property.hasMultipleDifferentValues && m_exposedSerializedObject != null) {
SerializedProperty iterator = m_exposedSerializedObject.GetIterator();
for (bool enterChildren = true; iterator.NextVisible(enterChildren); enterChildren = false) {
float propertyHeight = EditorGUI.GetPropertyHeight(iterator, true);
if (propertyHeight > 0f)
totalHeight += propertyHeight + EditorGUIUtility.standardVerticalSpacing;
}
// Extra Padding
totalHeight += 2f * EditorGUIUtility.standardVerticalSpacing;
}
m_propertyHeight = totalHeight;
return totalHeight;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
position.height = EditorGUIUtility.singleLineHeight;
label = EditorGUI.BeginProperty(position, label, property);
EditorGUI.PropertyField(position, property, label, true);
EditorGUI.EndProperty();
if (property.propertyType != SerializedPropertyType.ObjectReference) {
Debug.LogWarning("The ExposeObject attribute only supports application on UnityEngine.Object derived types");
return;
}
Object propertyValue = property.objectReferenceValue;
if (propertyValue == null)
return;
const float FOLDOUT_X_OFFSET = 2f;
position.xMin -= FOLDOUT_X_OFFSET;
property.isExpanded = EditorGUI.Foldout(position, property.isExpanded, GUIContent.none, true);
position.xMin += FOLDOUT_X_OFFSET;
if (property.isExpanded) {
if (m_exposedSerializedObject == null || m_exposedSerializedObject.targetObject != propertyValue)
m_exposedSerializedObject = new SerializedObject(propertyValue);
const float GUIDE_X_OFFSET = FOLDOUT_X_OFFSET + 7f;
Rect indentedRect = EditorGUI.IndentedRect(position);
Rect foldGuideRect = indentedRect;
foldGuideRect.x -= GUIDE_X_OFFSET;
foldGuideRect.y += EditorGUIUtility.singleLineHeight;
foldGuideRect.width = 1f;
foldGuideRect.height = m_propertyHeight - EditorGUIUtility.singleLineHeight;
EditorGUI.DrawRect(foldGuideRect, FOLDOUT_GUIDE_COLOR);
EditorGUI.indentLevel++;
EditorGUI.BeginDisabledGroup(!GUI.enabled);
Rect helpRect = indentedRect;
helpRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
helpRect.height *= 2f;
if (property.hasMultipleDifferentValues)
EditorGUI.HelpBox(helpRect, "Unable to view/edit properties whilst editing multiple objects with different values", MessageType.Info);
else
DrawExposedProperties(position);
EditorGUI.EndDisabledGroup();
EditorGUI.indentLevel--;
}
}
private void DrawExposedProperties(Rect position) {
m_exposedSerializedObject.Update();
SerializedProperty iterator = m_exposedSerializedObject.GetIterator();
for (bool enterChildren = true; iterator.NextVisible(enterChildren); enterChildren = false) {
if (iterator.name == "m_Script")
EditorGUI.BeginDisabledGroup(true);
position.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
position.height = EditorGUI.GetPropertyHeight(iterator, true);
EditorGUI.PropertyField(position, iterator, true);
position.y += position.height - EditorGUIUtility.singleLineHeight;
if (iterator.name == "m_Script")
EditorGUI.EndDisabledGroup();
}
m_exposedSerializedObject.ApplyModifiedProperties();
}
}
}
#endif
}