Skip to content

Commit fa1e8c1

Browse files
committed
Add context menu for each Unity objects in built-in inspector.
Fix property/field setters on copy value mode.
1 parent 3f2f66e commit fa1e8c1

File tree

4 files changed

+89
-23
lines changed

4 files changed

+89
-23
lines changed

Editor/UInspectorPlus/Helpers.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -404,11 +404,9 @@ private static int CountLines(this string str) {
404404

405405
internal static bool AssignValue(MemberInfo info, object target, object value, params object[] index) {
406406
try {
407-
var fieldInfo = info as FieldInfo;
408-
var propertyInfo = info as PropertyInfo;
409-
if (fieldInfo != null && !fieldInfo.IsInitOnly && !fieldInfo.IsLiteral)
407+
if (info is FieldInfo fieldInfo && !fieldInfo.IsInitOnly && !fieldInfo.IsLiteral)
410408
fieldInfo.SetValue(target, value);
411-
else if (propertyInfo != null && propertyInfo.CanWrite)
409+
else if (info is PropertyInfo propertyInfo && propertyInfo.CanWrite)
412410
propertyInfo.SetValue(target, value, index);
413411
else
414412
return false;
@@ -419,23 +417,19 @@ internal static bool AssignValue(MemberInfo info, object target, object value, p
419417
}
420418

421419
internal static bool IsReadOnly(this MemberInfo info) {
422-
var fieldInfo = info as FieldInfo;
423-
if (fieldInfo != null)
420+
if (info is FieldInfo fieldInfo)
424421
return fieldInfo.IsInitOnly || fieldInfo.IsLiteral;
425-
var propertyInfo = info as PropertyInfo;
426-
if (propertyInfo != null)
422+
if (info is PropertyInfo propertyInfo)
427423
return !propertyInfo.CanWrite;
428424
return false;
429425
}
430426

431427
internal static bool FetchValue(this MemberInfo info, object target, out object value, params object[] index) {
432428
value = null;
433429
try {
434-
var fieldInfo = info as FieldInfo;
435-
var propertyInfo = info as PropertyInfo;
436-
if (fieldInfo != null)
430+
if (info is FieldInfo fieldInfo)
437431
value = fieldInfo.GetValue(target);
438-
else if (propertyInfo != null && propertyInfo.CanRead)
432+
else if (info is PropertyInfo propertyInfo && propertyInfo.CanRead)
439433
value = propertyInfo.GetValue(target, index);
440434
else
441435
return false;
@@ -547,6 +541,16 @@ public static IEnumerable<Type> LooseGetTypes(this Assembly assembly) {
547541
[MenuItem("Window/JLChnToZ/Inspector+")]
548542
public static void ShowInspectorPlus() => EditorWindow.GetWindow(typeof(InspectorPlus));
549543

544+
[MenuItem("CONTEXT/Object/Open Inspector+...", false, int.MaxValue)]
545+
public static void ShowInspectorPlus(MenuCommand command) => InspectorChildWindow.Open(
546+
command.context,
547+
EditorPrefs.GetBool("inspectorplus_props", true),
548+
EditorPrefs.GetBool("inspectorplus_private", true),
549+
EditorPrefs.GetBool("inspectorplus_obsolete", false),
550+
EditorPrefs.GetBool("inspectorplus_methods", true),
551+
true, null
552+
);
553+
550554
public static void PrintExceptionsWithInner(Exception ex) {
551555
do {
552556
Debug.LogException(ex);

Editor/UInspectorPlus/InspectorChildWindow.cs

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using UnityEngine;
22
using UnityEditor;
33
using System;
4+
using UnityObject = UnityEngine.Object;
45

56
namespace JLChnToZ.EditorExtensions.UInspectorPlus {
67
internal class InspectorChildWindow: EditorWindow {
@@ -14,6 +15,7 @@ internal class InspectorChildWindow: EditorWindow {
1415
private bool showPrivate;
1516
private bool showObsolete;
1617
private bool showMethods;
18+
private Rect menuButtonRect;
1719

1820
public static void Open(object target, bool showProps, bool showPrivate, bool showObsolete, bool showMethods, bool updateProps, MethodPropertyDrawer parent) =>
1921
CreateInstance<InspectorChildWindow>().InternalOpen(target, target.GetType(), showProps, showPrivate, showObsolete, showMethods, updateProps, parent);
@@ -25,14 +27,14 @@ private void InternalOpen(object target, Type targetType, bool showProps, bool s
2527
titleContent = new GUIContent($"{target ?? targetType} - Inspector+");
2628
if (target == null && targetType.ContainsGenericParameters) {
2729
resolver = new TypeResolverGUI(targetType);
28-
this.showProps = showProps;
29-
this.showPrivate = showPrivate;
30-
this.showObsolete = showObsolete;
31-
this.showMethods = showMethods;
3230
} else {
3331
drawer = InspectorDrawer.GetDrawer(target, targetType, true, showProps, showPrivate, showObsolete, showMethods);
3432
drawer.OnRequireRedraw += Repaint;
3533
}
34+
this.showProps = showProps;
35+
this.showPrivate = showPrivate;
36+
this.showObsolete = showObsolete;
37+
this.showMethods = showMethods;
3638
this.parent = parent;
3739
this.updateProps = updateProps;
3840
ShowUtility();
@@ -58,10 +60,28 @@ private void OnGUI() {
5860
return;
5961
}
6062
GUILayout.BeginHorizontal(EditorStyles.toolbar);
61-
updateProps = GUILayout.Toggle(updateProps, "Update Props", EditorStyles.toolbarButton);
62-
GUILayout.Space(8);
6363
drawer.searchText = Helper.ToolbarSearchField(drawer.searchText ?? string.Empty);
6464
GUILayout.FlexibleSpace();
65+
if (drawer.target is UnityObject uObject) {
66+
if (GUILayout.Button("Ping", EditorStyles.toolbarButton, GUILayout.ExpandWidth(false)))
67+
EditorGUIUtility.PingObject(uObject);
68+
if (GUILayout.Button("Select", EditorStyles.toolbarButton, GUILayout.ExpandWidth(false)))
69+
Selection.activeObject = uObject;
70+
if (GUILayout.Button(EditorGUIUtility.IconContent("TreeEditor.Trash", "Destroy"),
71+
EditorStyles.toolbarButton, GUILayout.ExpandWidth(false)) && EditorUtility.DisplayDialog(
72+
"Destroy object",
73+
$"Destroy {uObject.GetType()} {uObject.name} (Instance ID: {uObject.GetInstanceID()})?",
74+
"Yes", "No"
75+
)) {
76+
DestroyImmediate(uObject);
77+
Close();
78+
}
79+
GUILayout.Space(8);
80+
}
81+
if (GUILayout.Button(EditorGUIUtility.IconContent("_Menu", "Menu"), EditorStyles.toolbarButton, GUILayout.ExpandWidth(false)))
82+
OpenMenu(menuButtonRect);
83+
if (Event.current.type == EventType.Repaint)
84+
menuButtonRect = GUILayoutUtility.GetLastRect();
6585
GUILayout.EndHorizontal();
6686
scrollPos = GUILayout.BeginScrollView(scrollPos);
6787
EditorGUILayout.Space();
@@ -78,6 +98,34 @@ private void OnGUI() {
7898
GUILayout.EndScrollView();
7999
}
80100

101+
private void OpenMenu(Rect position) {
102+
var menu = new GenericMenu();
103+
menu.AddItem(new GUIContent("Update Properties"), updateProps, () => {
104+
updateProps = !updateProps;
105+
});
106+
menu.AddItem(new GUIContent("Show Properties"), showProps, () => {
107+
showProps = !showProps;
108+
RefreshDrawer();
109+
EditorPrefs.SetBool("inspectorplus_props", showProps);
110+
});
111+
menu.AddItem(new GUIContent("Show Methods"), showMethods, () => {
112+
showMethods = !showMethods;
113+
RefreshDrawer();
114+
EditorPrefs.SetBool("inspectorplus_methods", showMethods);
115+
});
116+
menu.AddItem(new GUIContent("Show Private Members"), showPrivate, () => {
117+
showPrivate = !showPrivate;
118+
RefreshDrawer();
119+
EditorPrefs.SetBool("inspectorplus_private", showPrivate);
120+
});
121+
menu.AddItem(new GUIContent("Show Obsolete Members"), showObsolete, () => {
122+
showObsolete = !showObsolete;
123+
RefreshDrawer();
124+
EditorPrefs.SetBool("inspectorplus_obsolete", showObsolete);
125+
});
126+
menu.DropDown(position);
127+
}
128+
81129
private void OnDestroy() => drawer?.Dispose();
82130

83131
private void OnInspectorUpdate() {
@@ -86,6 +134,16 @@ private void OnInspectorUpdate() {
86134
UpdateValues();
87135
}
88136

137+
private void RefreshDrawer() {
138+
if (drawer == null) return;
139+
var target = drawer.target;
140+
if (target.IsInvalid()) return;
141+
drawer.Dispose();
142+
drawer = InspectorDrawer.GetDrawer(target, target.GetType(), true, showProps, showPrivate, showObsolete, showMethods);
143+
drawer.OnRequireRedraw += Repaint;
144+
UpdateValues();
145+
}
146+
89147
private void UpdateValues() {
90148
if (drawer == null) {
91149
if (resolver == null) Close();

Editor/UInspectorPlus/InspectorDrawer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,7 @@ private void DrawRequestRefs() {
218218
foreach (var drawer in MethodPropertyDrawer.drawerRequestingReferences)
219219
if (drawer.requiredType.IsAssignableFrom(targetType) &&
220220
GUILayout.Button($"Assign this object to {drawer.name}")) {
221-
drawer.Value = target;
222-
drawer.SetDirty();
221+
drawer.TryApplyValue(target);
223222
removal = drawer;
224223
}
225224
if (removal != null) MethodPropertyDrawer.drawerRequestingReferences.Remove(removal);

Editor/UInspectorPlus/MethodPropertyDrawer.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,12 @@ public void Draw(bool readOnly, Rect? rect = null) {
366366
public bool UpdateIfChanged() {
367367
if (!Changed)
368368
return false;
369+
return TryApplyValue(rawValue);
370+
}
371+
372+
public bool TryApplyValue(object newValue) {
373+
rawValue = newValue;
374+
hasGetValue = true;
369375
if (Helper.AssignValue(memberInfo, target, Value, indexParams))
370376
return true;
371377
UpdateValue();
@@ -445,8 +451,7 @@ private bool DrawAssignButton(Rect? rect) {
445451
GUI.Button(rect.Value, EditorGUIUtility.IconContent("Linked"), EditorStyles.miniLabel) :
446452
GUILayout.Button(EditorGUIUtility.IconContent("Linked"), EditorStyles.miniLabel, GUILayout.ExpandWidth(false))
447453
) {
448-
drawer.Value = rawValue;
449-
drawer.SetDirty();
454+
drawer.TryApplyValue(rawValue);
450455
finishedDrawer = drawer;
451456
}
452457
}
@@ -768,9 +773,9 @@ private void ShowMenu(Rect position) {
768773
case PropertyType.Unknown:
769774
case PropertyType.Type:
770775
menu.AddItem(new GUIContent("Mode/Construct"), grabValueMode == 2, GrabValueMode, 2);
771-
menu.AddItem(new GUIContent("Mode/From Opened Inspectors"), grabValueMode == 3, GrabValueMode, 3);
772776
break;
773777
}
778+
menu.AddItem(new GUIContent("Mode/From Opened Inspectors"), grabValueMode == 3, GrabValueMode, 3);
774779
}
775780
if (currentType == PropertyType.Enum)
776781
menu.AddItem(new GUIContent("Multiple Selection"), masked, ChangeMultiSelect, !masked);

0 commit comments

Comments
 (0)