Skip to content

Commit c6cd04b

Browse files
committed
Drop inline array editor
Add 1 level auto reassign value type support Remove number indicator in method selector Code tidy up
1 parent 24aad58 commit c6cd04b

File tree

4 files changed

+53
-54
lines changed

4 files changed

+53
-54
lines changed

UInspectorPlus/ComponentMethodDrawer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ string GetMethodNameFormatted(ComponentMethod m, int i) {
256256
method = m.method;
257257
name = Helper.GetMemberName(method as MemberInfo).Replace('_', ' ');
258258
}
259-
var result = string.Format("{0:000} {1} ({2})", i + 1, name, Helper.JoinStringList(null, method.GetParameters().Select(x => x.ParameterType.Name), ", "));
259+
var result = string.Format("{0} ({1})", name, Helper.JoinStringList(null, method.GetParameters().Select(x => x.ParameterType.Name), ", "));
260260
return result;
261261
}
262262

UInspectorPlus/InspectorChildWindow.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@
44
namespace UInspectorPlus {
55
class InspectorChildWindow: EditorWindow {
66
InspectorDrawer drawer;
7+
MethodPropertyDrawer parent;
78
Vector2 scrollPos;
89
bool updateProps;
10+
bool isReadOnly;
911

10-
public static void Open(object target, bool showProps, bool showPrivate, bool showObsolete, bool showMethods, bool updateProps) {
11-
CreateInstance<InspectorChildWindow>().InternalOpen(target, showProps, showPrivate, showObsolete, showMethods, updateProps);
12+
public static void Open(object target, bool showProps, bool showPrivate, bool showObsolete, bool showMethods, bool updateProps, MethodPropertyDrawer parent) {
13+
CreateInstance<InspectorChildWindow>().InternalOpen(target, showProps, showPrivate, showObsolete, showMethods, updateProps, parent);
1214
}
1315

14-
void InternalOpen(object target, bool showProps, bool showPrivate, bool showObsolete, bool showMethods, bool updateProps) {
16+
void InternalOpen(object target, bool showProps, bool showPrivate, bool showObsolete, bool showMethods, bool updateProps, MethodPropertyDrawer parent) {
1517
titleContent = new GUIContent(string.Format("{0} - Inspector+", target));
1618
drawer = new InspectorDrawer(target, true, showProps, showPrivate, showObsolete, showMethods);
1719
drawer.OnRequireRedraw += Repaint;
20+
this.parent = parent;
1821
this.updateProps = updateProps;
1922
ShowUtility();
2023
UpdateValues();
24+
isReadOnly = parent != null && parent.IsReadOnly && parent.requiredType != null && parent.requiredType.IsValueType;
2125
}
2226

2327
void OnGUI() {
@@ -40,7 +44,17 @@ void OnGUI() {
4044
GUILayout.EndHorizontal();
4145
scrollPos = GUILayout.BeginScrollView(scrollPos);
4246
EditorGUILayout.Space();
43-
drawer.Draw(false);
47+
drawer.Draw(false, isReadOnly);
48+
if (drawer.changed) {
49+
drawer.changed = false;
50+
if (parent != null && !parent.IsReadOnly &&
51+
((parent.requiredType != null && parent.requiredType.IsValueType) || parent.Value != drawer.target))
52+
if(!Helper.AssignValue(parent.Info, parent.Target, drawer.target)) {
53+
object reverted;
54+
if (Helper.FetchValue(parent.Info, parent.Target, out reverted))
55+
drawer.target = reverted;
56+
}
57+
}
4458
GUILayout.FlexibleSpace();
4559
GUILayout.EndScrollView();
4660
if (drawer.target == null) Close();

UInspectorPlus/InspectorDrawer.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class InspectorDrawer {
1212
public List<IReflectorDrawer> drawer;
1313
public bool shown;
1414
public bool isInternalType;
15+
public bool changed;
1516
public string searchText;
1617
public event Action OnRequireRedraw;
1718
Type targetType;
@@ -31,8 +32,7 @@ public InspectorDrawer(object target, bool shown, bool showProps, bool showPriva
3132
if (!showObsolete && Attribute.IsDefined(field, typeof(ObsoleteAttribute)))
3233
continue;
3334
drawer.Add(new MethodPropertyDrawer(field, target, showPrivateFields, showObsolete) {
34-
AllowReferenceMode = false,
35-
Info = field
35+
AllowReferenceMode = false
3636
});
3737
} catch (Exception ex) {
3838
Debug.LogException(ex);
@@ -53,7 +53,6 @@ public InspectorDrawer(object target, bool shown, bool showProps, bool showPriva
5353
continue;
5454
drawer.Add(new MethodPropertyDrawer(prop, target, showPrivateFields, showObsolete, prop.CanRead && EditorApplication.isPlaying) {
5555
AllowReferenceMode = false,
56-
Info = prop,
5756
Updatable = isInternalType || Helper.GetState(prop, false),
5857
ShowUpdatable = !isInternalType
5958
});
@@ -115,6 +114,8 @@ public void Draw(bool drawHeader = true, bool readOnly = false) {
115114
} else
116115
propDrawer.GetException = value as Exception;
117116
}
117+
} else {
118+
changed = true;
118119
}
119120
}
120121
}

UInspectorPlus/MethodPropertyDrawer.cs

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class MethodPropertyDrawer: IReflectorDrawer {
1616
public Type requiredType;
1717
readonly List<PropertyType> castableTypes;
1818
PropertyType currentType;
19+
object target;
1920
object rawValue;
2021
bool referenceMode;
2122
int grabValueMode;
@@ -36,17 +37,13 @@ class MethodPropertyDrawer: IReflectorDrawer {
3637
bool masked;
3738
bool showUpdatable;
3839
bool updatable;
39-
bool arrayShown;
4040
bool isInfoReadonly;
4141
bool isStatic;
4242
bool isPrivate;
4343
public event Action OnRequireRedraw;
4444

4545
Exception getException;
4646

47-
readonly List<MethodPropertyDrawer> arrayContentDrawer;
48-
ReorderableList arrayHandler;
49-
5047
public UnityObject Component {
5148
get { return component; }
5249
set {
@@ -117,7 +114,7 @@ public bool ReferenceMode {
117114
InitFieldTypes();
118115
} else {
119116
rawValue = GetReferencedValue();
120-
SetArray();
117+
// SetArray();
121118
}
122119
selectedFieldIndex = -1;
123120
selectedField = null;
@@ -160,11 +157,13 @@ public object Value {
160157
get {
161158
if (referenceMode) {
162159
rawValue = GetReferencedValue();
160+
/*
163161
} else if (currentType == PropertyType.Array) {
164162
var array = Array.CreateInstance(requiredType.GetElementType(), arrayContentDrawer.Count);
165163
for (int i = 0; i < arrayContentDrawer.Count; i++)
166164
array.SetValue(arrayContentDrawer[i].Value, i);
167165
rawValue = array;
166+
*/
168167
}
169168
var convertedValue = rawValue;
170169
if (rawValue != null && requiredType != typeof(object) && requiredType.IsInstanceOfType(rawValue)) {
@@ -180,7 +179,7 @@ public object Value {
180179
set {
181180
rawValue = value;
182181
changed = false;
183-
SetArray();
182+
// SetArray();
184183
}
185184
}
186185

@@ -193,33 +192,45 @@ public Exception GetException {
193192
}
194193
}
195194

195+
public bool IsReadOnly {
196+
get { return isInfoReadonly; }
197+
}
198+
199+
public object Target {
200+
get { return target; }
201+
}
202+
196203
private MethodPropertyDrawer(bool allowPrivate, bool allowObsolete) {
197204
this.castableTypes = new List<PropertyType>();
198205
this.fields = new List<ComponentFields>();
199206
this.selectedFieldIndex = -1;
200-
this.arrayContentDrawer = new List<MethodPropertyDrawer>();
201-
this.arrayHandler = new ReorderableList(arrayContentDrawer, typeof(MethodPropertyDrawer));
202207
this.privateFields = allowPrivate;
203208
this.obsolete = allowObsolete;
204209
}
205210

206211
public MethodPropertyDrawer(FieldInfo field, object target, bool allowPrivate, bool allowObsolete)
207212
: this(allowPrivate, allowObsolete) {
213+
this.memberInfo = field;
214+
this.isInfoReadonly = Helper.IsReadOnly(field);
208215
this.requiredType = field.FieldType;
209216
this.name = Helper.GetMemberName(field, true);
210217
this.nameContent = new GUIContent(name, Helper.GetMemberName(field));
211218
this.rawValue = field.GetValue(target);
219+
this.target = target;
212220
this.isStatic = field.IsStatic;
213221
this.isPrivate = field.IsPrivate;
214222
InitType();
215223
}
216224

217225
public MethodPropertyDrawer(PropertyInfo property, object target, bool allowPrivate, bool allowObsolete, bool initValue)
218226
: this(allowPrivate, allowObsolete) {
227+
this.memberInfo = property;
228+
this.isInfoReadonly = Helper.IsReadOnly(property);
219229
this.requiredType = property.PropertyType;
220230
this.name = Helper.GetMemberName(property, true);
221231
this.nameContent = new GUIContent(name, Helper.GetMemberName(property));
222232
if(initValue) this.rawValue = property.GetValue(target, null);
233+
this.target = target;
223234
var getMethod = property.GetGetMethod();
224235
this.isStatic = getMethod != null ? getMethod.IsStatic : false;
225236
this.isPrivate = getMethod != null ? getMethod.IsPrivate : false;
@@ -231,7 +242,8 @@ public MethodPropertyDrawer(ParameterInfo parameter, bool allowPrivate, bool all
231242
this.requiredType = parameter.ParameterType;
232243
this.name = parameter.Name;
233244
this.nameContent = new GUIContent(this.name, this.name);
234-
if (parameter.IsOptional) this.rawValue = parameter.DefaultValue;
245+
if (parameter.IsOptional)
246+
this.rawValue = parameter.DefaultValue;
235247
InitType();
236248
}
237249

@@ -244,27 +256,11 @@ public MethodPropertyDrawer(Type type, string name, object defaultValue, bool al
244256
InitType();
245257
}
246258

247-
void ListAddItem(object value = null) {
248-
var drawer = new MethodPropertyDrawer(requiredType.GetElementType(), "", value, privateFields, obsolete);
249-
drawer.OnRequireRedraw += RequireRedraw;
250-
arrayContentDrawer.Add(drawer);
251-
}
252-
253259
void InitType() {
254260
Helper.InitPropertyTypeMapper();
255-
if (Helper.IsInterface(requiredType, typeof(IList<>))) {
261+
if(requiredType.IsArray) {
256262
castableTypes.Add(PropertyType.Array);
257263
currentType = PropertyType.Array;
258-
arrayHandler.headerHeight = EditorGUIUtility.singleLineHeight;
259-
arrayHandler.elementHeight = EditorGUIUtility.singleLineHeight + 2;
260-
arrayHandler.drawHeaderCallback = r => EditorGUI.LabelField(r, name);
261-
arrayHandler.drawElementCallback = (r, i, c, d) => arrayContentDrawer[i].Draw(false, Helper.ScaleRect(r, offsetHeight: -2));
262-
arrayHandler.onAddCallback = l => ListAddItem();
263-
arrayContentDrawer.Clear();
264-
var enumerable = rawValue as IEnumerable;
265-
if (enumerable != null)
266-
foreach (object item in enumerable)
267-
ListAddItem(item);
268264
return;
269265
}
270266
if (requiredType.IsByRef)
@@ -303,7 +299,13 @@ public void Draw() {
303299

304300
public void Draw(bool readOnly, Rect? rect = null) {
305301
readOnly |= isInfoReadonly;
306-
var referenceModeBtn = (!allowReferenceMode && (currentType == PropertyType.Unknown || currentType == PropertyType.Object || currentType == PropertyType.Array)) || allowReferenceMode || (allowReferenceMode && optionalPrivateFields) || castableTypes.Count > 1;
302+
var referenceModeBtn = (!allowReferenceMode && (
303+
currentType == PropertyType.Unknown ||
304+
currentType == PropertyType.Object ||
305+
currentType == PropertyType.Array)
306+
) ||
307+
allowReferenceMode ||
308+
castableTypes.Count > 1;
307309
if (!rect.HasValue)
308310
EditorGUI.indentLevel--;
309311
EditorGUILayout.BeginHorizontal();
@@ -384,14 +386,6 @@ void InitFieldTypes() {
384386
selectedFieldIndex = -1;
385387
}
386388

387-
void SetArray() {
388-
arrayContentDrawer.Clear();
389-
if (requiredType.IsArray && rawValue != null) {
390-
foreach (object item in (Array)rawValue)
391-
arrayContentDrawer.Add(new MethodPropertyDrawer(requiredType.GetElementType(), "", item, privateFields, obsolete));
392-
}
393-
}
394-
395389
object GetReferencedValue() {
396390
object val = null;
397391
return Helper.FetchValue(selectedField as MemberInfo ?? selectedProperty, component, out val) ? val : null;
@@ -576,16 +570,6 @@ void DrawDirectField(bool readOnly, Rect? rect) {
576570
else
577571
value = Helper.ObjectField(nameContent, value as UnityObject, requiredType, true, readOnly);
578572
break;
579-
case PropertyType.Array:
580-
if (rect.HasValue) {
581-
arrayHandler.DoList(rect.Value);
582-
break;
583-
}
584-
EditorGUILayout.BeginVertical();
585-
if (arrayShown = EditorGUILayout.Foldout(arrayShown, nameContent))
586-
arrayHandler.DoLayoutList();
587-
EditorGUILayout.EndVertical();
588-
break;
589573
case PropertyType.String:
590574
if (rect.HasValue)
591575
value = Helper.StringField(rect.Value, nameContent, (string)value, readOnly);
@@ -626,7 +610,7 @@ void DrawUnknownField(bool readOnly, object target, Rect? position = null) {
626610
else
627611
clicked = GUI.Button(position.Value, "…", EditorStyles.miniButton);
628612
if (clicked)
629-
InspectorChildWindow.Open(target, true, privateFields, obsolete, true, false);
613+
InspectorChildWindow.Open(target, true, privateFields, obsolete, true, false, this);
630614
}
631615

632616
void ShowMenu(Rect position) {

0 commit comments

Comments
 (0)