forked from Deadcows/MyBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DisplayInspectorAttribute.cs
180 lines (151 loc) · 5.98 KB
/
DisplayInspectorAttribute.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System.Collections.Generic;
using UnityEngine;
namespace MyBox
{
/// <summary>
/// Use to display inspector of property object
/// </summary>
public class DisplayInspectorAttribute : PropertyAttribute
{
public readonly bool DisplayScript;
public DisplayInspectorAttribute(bool displayScriptField = true)
{
DisplayScript = displayScriptField;
}
}
}
#if UNITY_EDITOR
namespace MyBox.Internal
{
using EditorTools;
using UnityEditor;
[CustomPropertyDrawer(typeof(DisplayInspectorAttribute))]
public class DisplayInspectorAttributeDrawer : PropertyDrawer
{
private ButtonMethodHandler _buttonMethods;
private EditorPrefsBool _foldout;
private readonly Dictionary<Object, SerializedObject> _targets = new Dictionary<Object, SerializedObject>();
private SerializedObject GetTargetSO(Object targetObject)
{
SerializedObject target;
if (_targets.ContainsKey(targetObject)) target = _targets[targetObject];
else
{
_targets.Add(targetObject, new SerializedObject(targetObject));
target = _targets[targetObject];
}
target.Update();
return target;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
bool notValidType = property.propertyType != SerializedPropertyType.ObjectReference;
if (notValidType)
{
EditorGUI.LabelField(position, label.text, "Use [DisplayInspector] with MB or SO");
return;
}
position.height = EditorGUIUtility.singleLineHeight;
bool displayScript = ((DisplayInspectorAttribute)attribute).DisplayScript;
if (displayScript || property.objectReferenceValue == null)
{
// Draw foldout only if Script line drawn and there is content to hide (ref assigned)
if (property.objectReferenceValue != null)
{
// Workaround to make label clickable, accurately aligned and property field click is not triggering foldout
var foldRect = new Rect(position);
foldRect.width = EditorGUIUtility.labelWidth;
_foldout.Value = EditorGUI.Foldout(foldRect, _foldout.Value, new GUIContent(""), true, StyleFramework.FoldoutHeader);
EditorGUI.PropertyField(position, property, label);
if (GUI.changed) property.serializedObject.ApplyModifiedProperties();
if (!_foldout.Value) return;
}
else
{
EditorGUI.PropertyField(position, property, label);
if (GUI.changed) property.serializedObject.ApplyModifiedProperties();
}
}
if (property.objectReferenceValue == null) return;
if (_buttonMethods == null) _buttonMethods = new ButtonMethodHandler(property.objectReferenceValue);
if (displayScript) position.y += position.height + 4;
var startY = position.y - 2;
float startX = position.x;
var target = GetTargetSO(property.objectReferenceValue);
var propertyObject = target.GetIterator();
propertyObject.Next(true);
propertyObject.NextVisible(true);
var xPos = position.x + 10;
var width = position.width - 10;
bool expandedReorderable = false;
while (propertyObject.NextVisible(propertyObject.isExpanded && !expandedReorderable))
{
#if UNITY_2020_2_OR_NEWER
expandedReorderable = propertyObject.isExpanded && propertyObject.isArray &&
!propertyObject.IsAttributeDefined<NonReorderableAttribute>();
#endif
position.x = xPos + 10 * propertyObject.depth;
position.width = width - 10 * propertyObject.depth;
position.height = EditorGUI.GetPropertyHeight(propertyObject, expandedReorderable);
EditorGUI.PropertyField(position, propertyObject, expandedReorderable);
position.y += position.height + 4;
}
if (!_buttonMethods.TargetMethods.IsNullOrEmpty())
{
foreach (var method in _buttonMethods.TargetMethods)
{
position.height = EditorGUIUtility.singleLineHeight;
if (GUI.Button(position, method.Name)) _buttonMethods.Invoke(method.Method);
position.y += position.height;
}
}
var bgRect = position;
bgRect.y = startY;
bgRect.x = startX - 12;
bgRect.width = 11;
bgRect.height = position.y - startY;
if (_buttonMethods.Amount > 0) bgRect.height += 5;
DrawColouredRect(bgRect, new Color(.6f, .6f, .8f, .5f));
target.ApplyModifiedProperties();
property.serializedObject.ApplyModifiedProperties();
}
private bool IsFolded(SerializedProperty property)
{
_foldout ??= new EditorPrefsBool("DisplayInspectorFoldout" +
property.GetParent().GetType().Name +
property.propertyPath);
return _foldout.Value;
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
bool notValidType = property.propertyType != SerializedPropertyType.ObjectReference;
bool displayScript = ((DisplayInspectorAttribute)attribute).DisplayScript;
if (notValidType || property.objectReferenceValue == null || (displayScript && !IsFolded(property))) return base.GetPropertyHeight(property, label);
if (_buttonMethods == null) _buttonMethods = new ButtonMethodHandler(property.objectReferenceValue);
float height = displayScript ? EditorGUI.GetPropertyHeight(property) + 4 : 0;
var target = GetTargetSO(property.objectReferenceValue);
var propertyObject = target.GetIterator();
propertyObject.Next(true);
propertyObject.NextVisible(true);
bool expandedReorderable = false;
while (propertyObject.NextVisible(propertyObject.isExpanded && !expandedReorderable))
{
#if UNITY_2020_2_OR_NEWER
expandedReorderable = propertyObject.isExpanded && propertyObject.isArray &&
!propertyObject.IsAttributeDefined<NonReorderableAttribute>();
#endif
height += EditorGUI.GetPropertyHeight(propertyObject, expandedReorderable) + 4;
}
if (_buttonMethods.Amount > 0) height += 4 + _buttonMethods.Amount * EditorGUIUtility.singleLineHeight;
return height;
}
private void DrawColouredRect(Rect rect, Color color)
{
var defaultBackgroundColor = GUI.backgroundColor;
GUI.backgroundColor = color;
GUI.Box(rect, "");
GUI.backgroundColor = defaultBackgroundColor;
}
}
}
#endif