Skip to content

Commit 6ae4c21

Browse files
committed
Add New method to grab value from other opened inspector windows.
Expose classes for extending the inspector.
1 parent 6bcd760 commit 6ae4c21

12 files changed

+340
-229
lines changed

.editorconfig

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# 如需深入了解 .editorconfig,請參閱 https://aka.ms/editorconfigdocs
2+
root = true
3+
4+
[*.xml]
5+
indent_style = space
6+
7+
# My C# coding style (Modified K&R)
8+
[*.cs]
9+
csharp_new_line_before_open_brace = none
10+
csharp_new_line_before_else = false
11+
csharp_new_line_before_catch = false
12+
csharp_new_line_before_finally = false
13+
csharp_new_line_before_members_in_object_initializers = true
14+
csharp_new_line_before_members_in_anonymous_types = true
15+
csharp_new_line_between_query_expression_clauses = true
16+
csharp_indent_case_contents = true
17+
csharp_indent_switch_labels = true
18+
csharp_indent_labels = no_change
19+
csharp_space_after_cast = false
20+
csharp_space_after_keywords_in_control_flow_statements = true
21+
csharp_space_between_method_declaration_parameter_list_parentheses = false
22+
csharp_space_between_method_call_parameter_list_parentheses = false
23+
csharp_space_before_colon_in_inheritance_clause = false
24+
csharp_space_after_colon_in_inheritance_clause = true
25+
csharp_space_around_binary_operators = before_and_after
26+
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
27+
csharp_space_between_method_call_name_and_opening_parenthesis = false
28+
csharp_space_between_method_call_empty_parameter_list_parentheses = false

UInspectorPlus/ComponentMethodDrawer.cs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using UnityObject = UnityEngine.Object;
99

1010
namespace UInspectorPlus {
11-
internal class ComponentMethodDrawer: IReflectorDrawer {
11+
internal class ComponentMethodDrawer: IReflectorDrawer, IDisposable {
1212
private object component;
1313
private readonly List<ComponentMethod> methods = new List<ComponentMethod>();
1414
private AnimBool showMethodOptions;
@@ -42,13 +42,9 @@ public bool ShouldDrawHeader {
4242
}
4343
}
4444

45-
public bool Changed {
46-
get { return false; }
47-
}
45+
public bool Changed => false;
4846

49-
public object Value {
50-
get { return result == null ? null : result.Value; }
51-
}
47+
public object Value => result?.Value;
5248

5349
public bool AllowPrivateFields {
5450
get { return privateFields; }
@@ -66,13 +62,9 @@ public bool AllowObsolete {
6662
}
6763
}
6864

69-
public MemberInfo Info {
70-
get { return selectedMember; }
71-
}
65+
public MemberInfo Info => selectedMember;
7266

73-
public bool IsComponentNull() {
74-
return component == null;
75-
}
67+
public bool IsComponentNull => component == null;
7668

7769
public ComponentMethodDrawer() {
7870
showMethodSelector = new AnimBool(false);
@@ -153,7 +145,7 @@ public void Call() {
153145
obsolete);
154146
break;
155147
}
156-
for (int i = 0; i < Math.Min(parameters.Length, requestData.Length); i++) {
148+
for (int i = 0, l = Math.Min(parameters.Length, requestData.Length); i < l; i++) {
157149
parameters[i].Value = requestData[i];
158150
if (parameters[i].ReferenceMode)
159151
Helper.AssignValue(parameters[i].RefFieldInfo, parameters[i].Component, requestData[i]);
@@ -216,6 +208,13 @@ public bool UpdateValue() {
216208
return false;
217209
}
218210

211+
public void Dispose() {
212+
if (parameters != null)
213+
foreach (var parameter in parameters)
214+
parameter.Dispose();
215+
result?.Dispose();
216+
}
217+
219218
private bool FilterMemberInfo(MemberInfo m) {
220219
return (obsolete || !Attribute.IsDefined(m, typeof(ObsoleteAttribute))) &&
221220
(string.IsNullOrEmpty(filter) ||
@@ -291,12 +290,7 @@ private void InitComponentMethods(bool resetIndex = true) {
291290
if (gameObject != null)
292291
foreach (var c in gameObject.GetComponents(typeof(Component)))
293292
AddComponentMethod(c);
294-
methodNames = methods.Select((m, i) => string.Format(
295-
"{0} ({1})/{2}",
296-
m.target.GetType().Name,
297-
Helper.ObjIdOrHashCode(m.target),
298-
GetMethodNameFormatted(m, i)
299-
)).ToArray();
293+
methodNames = methods.Select((m, i) => $"{m.target.GetType().Name} ({Helper.ObjIdOrHashCode(m.target)})/{GetMethodNameFormatted(m, i)}").ToArray();
300294
} else {
301295
methodNames = methods.Select((m, i) => GetMethodNameFormatted(m, i)).ToArray();
302296
}
@@ -451,7 +445,7 @@ private void DrawResult() {
451445

452446
private void DrawExecButton() {
453447
if (selectedMember == null) return;
454-
bool execute = false;
448+
bool execute;
455449
switch (mode) {
456450
case MethodMode.Constructor:
457451
execute = GUILayout.Button("Construct");
@@ -469,9 +463,6 @@ private void DrawExecButton() {
469463
Call();
470464
}
471465

472-
private void RequireRedraw() {
473-
if (OnRequireRedraw != null)
474-
OnRequireRedraw();
475-
}
466+
private void RequireRedraw() => OnRequireRedraw?.Invoke();
476467
}
477468
}

UInspectorPlus/Helpers.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,7 @@ private static object MaskedEnumFieldPostProcess(Type enumType, Array itemValues
338338
if ((changes & (1 << i)) != 0) {
339339
itemValue = Convert.ToInt64(itemValues.GetValue(i));
340340
if ((newMaskVal & (1 << i)) != 0) {
341-
if (itemValue == 0) {
342-
rawValue = 0;
343-
break;
344-
}
341+
if (itemValue == 0) break;
345342
value |= itemValue;
346343
} else
347344
value &= ~itemValue;
@@ -444,10 +441,10 @@ private static void ClickObject(UnityObject obj) {
444441
private static int CountLines(string str) {
445442
if (string.IsNullOrEmpty(str))
446443
return 1;
447-
int cursor = 0, count = 0, length = str.Length, i = -1;
444+
int cursor = 0, count = 0, length = str.Length;
448445
bool isCR = false;
449446
while (cursor < length) {
450-
i = str.IndexOf('\r', cursor);
447+
int i = str.IndexOf('\r', cursor);
451448
if (i >= 0) {
452449
count++;
453450
isCR = true;
@@ -544,9 +541,7 @@ internal static GUIStyle GetGUIStyle(string styleName) {
544541
return GUI.skin.FindStyle(styleName) ?? EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector).FindStyle(styleName);
545542
}
546543

547-
internal static T GetOrDefault<T>(object value, T defaultValue = default(T)) {
548-
return value == null ? defaultValue : (T)value;
549-
}
544+
internal static T GetOrDefault<T>(object value, T defaultValue = default(T)) => value == null ? defaultValue : (T)value;
550545

551546
internal static TDelegate GetDelegate<TDelegate>(string fromTypeName, string methodName, object target = null) where TDelegate : class {
552547
if (fromTypeName == null)

UInspectorPlus/HexEdit.cs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,48 @@
44
using UnityEditor;
55

66
namespace UInspectorPlus {
7-
internal class HexEdit {
7+
internal class HexEdit: InspectorDrawer {
88
[SerializeField] private Vector2 scrollPos;
9-
public byte[] data;
9+
public byte[] Data { get => target as byte[]; }
1010
public int columns = 16;
1111
private GUIContent temp = new GUIContent();
1212

13+
static HexEdit() => RegisterCustomInspectorDrawer<HexEdit>(typeof(byte[]), -1);
14+
15+
1316
public float Height {
1417
get {
15-
if (data == null) return 0;
16-
return (data.Length + columns) / columns * (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing);
18+
if (Data == null) return 0;
19+
return (Data.Length + columns) / columns * (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing);
1720
}
1821
}
1922

20-
public void DrawGUI(bool hasLabel = false, params GUILayoutOption[] options) {
21-
Draw(EditorGUILayout.GetControlRect(hasLabel, Height, options));
23+
public HexEdit(object target, Type targetType, bool shown, bool showProps, bool showPrivateFields, bool showObsolete, bool showMethods) :
24+
base(target, targetType, shown, showProps, showPrivateFields, showObsolete, showMethods) {
2225
}
2326

24-
public void Draw(Rect viewport) {
27+
protected override void Draw(bool readOnly) {
28+
if (Data != null)
29+
Draw(EditorGUILayout.GetControlRect(
30+
false, Height, GUILayout.MinHeight(EditorGUIUtility.singleLineHeight * 3), GUILayout.ExpandHeight(true)
31+
));
32+
base.Draw(readOnly);
33+
}
34+
35+
36+
private void Draw(Rect viewport) {
2537
float height = EditorGUIUtility.singleLineHeight;
2638
float padHeight = height + EditorGUIUtility.standardVerticalSpacing;
27-
if (data != null) {
28-
temp.text = data.Length.ToString("X8");
39+
if (Data != null) {
40+
temp.text = Data.Length.ToString("X8");
2941
Vector2 labelSize = GUI.skin.label.CalcSize(temp);
3042
Rect contentRect = new Rect(0, 0, labelSize.x + (columns * 1.7F + 2) * height, Height);
3143
GUI.Box(viewport, GUIContent.none, GUI.skin.textArea);
3244
scrollPos = GUI.BeginScrollView(viewport, scrollPos, contentRect);
3345
bool changed = GUI.changed;
3446
GUI.changed = false;
3547
for (int start = Mathf.FloorToInt(scrollPos.y / padHeight) * columns,
36-
end = Math.Min(data.Length, start + Mathf.CeilToInt(viewport.height / padHeight) * columns),
48+
end = Math.Min(Data.Length, start + Mathf.CeilToInt(viewport.height / padHeight) * columns),
3749
col = start; col < end; col++) {
3850
if (col % columns == 0) {
3951
temp.text = col.ToString("X8");
@@ -45,29 +57,29 @@ public void Draw(Rect viewport) {
4557
col / columns * padHeight,
4658
height * 1.6F, height
4759
),
48-
data[col].ToString("X2"),
60+
Data[col].ToString("X2"),
4961
2, GUI.skin.label
5062
);
5163
if (GUI.changed) {
5264
GUI.changed = false;
5365
changed = true;
5466
int val;
5567
if (int.TryParse(newValue, NumberStyles.HexNumber, null, out val))
56-
data[col] = unchecked((byte)val);
68+
Data[col] = unchecked((byte)val);
5769
}
5870
string newStr = GUI.TextField(
5971
new Rect(
6072
labelSize.x + (columns * 1.2F + col % columns * 0.5F + 2) * height,
6173
col / columns * padHeight,
6274
height * 0.8F, height
6375
),
64-
Byte2String(data[col]),
76+
Byte2String(Data[col]),
6577
1, GUI.skin.label
6678
);
6779
if (GUI.changed) {
6880
GUI.changed = false;
6981
changed = true;
70-
data[col] = newStr.Length > 0 ? unchecked((byte)newStr[0]) : (byte)0;
82+
Data[col] = newStr.Length > 0 ? unchecked((byte)newStr[0]) : (byte)0;
7183
}
7284
}
7385
GUI.changed = changed;
Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using UnityEngine;
22
using UnityEditor;
33
using System;
4-
using System.Linq;
54

65
namespace UInspectorPlus {
76
internal class InspectorChildWindow: EditorWindow {
@@ -11,20 +10,15 @@ internal class InspectorChildWindow: EditorWindow {
1110
private bool updateProps;
1211
private bool isReadOnly;
1312

14-
public static void Open(object target, bool showProps, bool showPrivate, bool showObsolete, bool showMethods, bool updateProps, MethodPropertyDrawer parent) {
15-
CreateInstance<InspectorChildWindow>()
16-
.InternalOpen(target, target.GetType(), showProps, showPrivate, showObsolete, showMethods, updateProps, parent);
17-
}
13+
public static void Open(object target, bool showProps, bool showPrivate, bool showObsolete, bool showMethods, bool updateProps, MethodPropertyDrawer parent) =>
14+
CreateInstance<InspectorChildWindow>().InternalOpen(target, target.GetType(), showProps, showPrivate, showObsolete, showMethods, updateProps, parent);
1815

19-
public static void OpenStatic(Type targetType, bool showProps, bool showPrivate, bool showObsolete, bool showMethods, bool updateProps, MethodPropertyDrawer parent)
20-
{
21-
CreateInstance<InspectorChildWindow>()
22-
.InternalOpen(null, targetType, showProps, showPrivate, showObsolete, showMethods, updateProps, parent);
23-
}
16+
public static void OpenStatic(Type targetType, bool showProps, bool showPrivate, bool showObsolete, bool showMethods, bool updateProps, MethodPropertyDrawer parent) =>
17+
CreateInstance<InspectorChildWindow>().InternalOpen(null, targetType, showProps, showPrivate, showObsolete, showMethods, updateProps, parent);
2418

2519
private void InternalOpen(object target, Type targetType, bool showProps, bool showPrivate, bool showObsolete, bool showMethods, bool updateProps, MethodPropertyDrawer parent) {
26-
titleContent = new GUIContent(string.Format("{0} - Inspector+", target ?? targetType));
27-
drawer = new InspectorDrawer(target, targetType, true, showProps, showPrivate, showObsolete, showMethods);
20+
titleContent = new GUIContent($"{target ?? targetType} - Inspector+");
21+
drawer = InspectorDrawer.GetDrawer(target, targetType, true, showProps, showPrivate, showObsolete, showMethods);
2822
drawer.OnRequireRedraw += Repaint;
2923
this.parent = parent;
3024
this.updateProps = updateProps;
@@ -57,31 +51,24 @@ private void OnGUI() {
5751
if (drawer.changed) {
5852
drawer.changed = false;
5953
if (parent != null && !parent.IsReadOnly &&
60-
((parent.requiredType != null && parent.requiredType.IsValueType) || parent.Value != drawer.target))
61-
if (!Helper.AssignValue(parent.Info, parent.Target, drawer.target)) {
62-
object reverted;
63-
if (Helper.FetchValue(parent.Info, parent.Target, out reverted))
64-
drawer.target = reverted;
65-
}
54+
((parent.requiredType != null && parent.requiredType.IsValueType) || parent.Value != drawer.target) &&
55+
!Helper.AssignValue(parent.Info, parent.Target, drawer.target) && Helper.FetchValue(parent.Info, parent.Target, out var reverted))
56+
drawer.target = reverted;
6657
}
6758
GUILayout.FlexibleSpace();
6859
GUILayout.EndScrollView();
6960
}
7061

62+
private void OnDestroy() {
63+
drawer.Dispose();
64+
}
65+
7166
private void OnInspectorUpdate() {
7267
if (EditorGUIUtility.editingTextField)
7368
return;
7469
UpdateValues();
7570
}
7671

77-
private void UpdateValues() {
78-
drawer.UpdateValues(updateProps);
79-
}
80-
81-
private void IterateDrawers<T>(Action<T> each) where T : IReflectorDrawer
82-
{
83-
foreach (var methodDrawer in drawer.drawer.OfType<T>())
84-
each(methodDrawer);
85-
}
72+
private void UpdateValues() => drawer.UpdateValues(updateProps);
8673
}
8774
}

0 commit comments

Comments
 (0)