Skip to content

Commit 36b1ce7

Browse files
committed
Code tidy up
1 parent 152ca58 commit 36b1ce7

File tree

8 files changed

+229
-221
lines changed

8 files changed

+229
-221
lines changed

UInspectorPlus/ComponentMethodDrawer.cs

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

1010
namespace UInspectorPlus {
11-
class ComponentMethodDrawer: IReflectorDrawer {
12-
object component;
13-
readonly List<ComponentMethod> methods = new List<ComponentMethod>();
14-
AnimBool showMethodOptions;
15-
AnimBool showMethodSelector;
16-
AnimBool showResultSelector;
17-
string[] methodNames;
18-
int selectedMethodIndex;
19-
MemberInfo selectedMember;
20-
ParameterInfo[] parameterInfo;
21-
MethodPropertyDrawer[] parameters;
22-
MethodPropertyDrawer result;
23-
Exception thrownException;
24-
string filter;
25-
Type ctorType;
26-
bool titleFolded = true, paramsFolded = true, resultFolded = true,
11+
internal class ComponentMethodDrawer: IReflectorDrawer {
12+
private object component;
13+
private readonly List<ComponentMethod> methods = new List<ComponentMethod>();
14+
private AnimBool showMethodOptions;
15+
private AnimBool showMethodSelector;
16+
private AnimBool showResultSelector;
17+
private string[] methodNames;
18+
private int selectedMethodIndex;
19+
private MemberInfo selectedMember;
20+
private ParameterInfo[] parameterInfo;
21+
private MethodPropertyDrawer[] parameters;
22+
private MethodPropertyDrawer result;
23+
private Exception thrownException;
24+
private string filter;
25+
private readonly Type ctorType;
26+
private bool titleFolded = true, paramsFolded = true, resultFolded = true,
2727
drawHeader = true, privateFields = true, obsolete = true;
28-
MethodMode mode = 0;
28+
private MethodMode mode = 0;
2929

3030
public bool execButton = true;
3131

@@ -123,7 +123,7 @@ public void Call() {
123123
}
124124
try {
125125
thrownException = null;
126-
var requestData = parameters.Select(d => d.Value).ToArray();
126+
var requestData = Array.ConvertAll(parameters, d => d.Value);
127127
object returnData;
128128
switch (mode) {
129129
case MethodMode.Constructor:
@@ -224,7 +224,7 @@ public bool UpdateValue() {
224224
return false;
225225
}
226226

227-
void AddComponentMethod(Type type) {
227+
private void AddComponentMethod(Type type) {
228228
BindingFlags flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
229229
if (privateFields)
230230
flag |= BindingFlags.NonPublic;
@@ -250,7 +250,7 @@ void AddComponentMethod(Type type) {
250250
);
251251
}
252252

253-
void AddComponentMethod(object target) {
253+
private void AddComponentMethod(object target) {
254254
BindingFlags flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
255255
if (privateFields)
256256
flag |= BindingFlags.NonPublic;
@@ -278,7 +278,7 @@ void AddComponentMethod(object target) {
278278
);
279279
}
280280

281-
void InitComponentMethods(bool resetIndex = true) {
281+
private void InitComponentMethods(bool resetIndex = true) {
282282
methods.Clear();
283283
switch (mode) {
284284
case MethodMode.Constructor:
@@ -316,7 +316,7 @@ void InitComponentMethods(bool resetIndex = true) {
316316
thrownException = null;
317317
}
318318

319-
string GetMethodNameFormatted(ComponentMethod m, int i) {
319+
private string GetMethodNameFormatted(ComponentMethod m, int i) {
320320
string name, formatStr;
321321
ParameterInfo[] parameters;
322322
switch (m.mode) {
@@ -346,7 +346,7 @@ string GetMethodNameFormatted(ComponentMethod m, int i) {
346346
return string.Format(formatStr, name, Helper.JoinStringList(null, parameters.Select(x => x.ParameterType.Name), ", "));
347347
}
348348

349-
void InitMethodParams() {
349+
private void InitMethodParams() {
350350
selectedMember = methods[selectedMethodIndex].member;
351351
switch (mode = methods[selectedMethodIndex].mode) {
352352
case MethodMode.Constructor:
@@ -373,7 +373,7 @@ void InitMethodParams() {
373373
thrownException = null;
374374
}
375375

376-
void DrawComponent() {
376+
private void DrawComponent() {
377377
if (OnClose != null)
378378
EditorGUILayout.BeginHorizontal();
379379
selectedMethodIndex = EditorGUILayout.Popup(mode.ToString(), selectedMethodIndex, methodNames);
@@ -395,7 +395,7 @@ void DrawComponent() {
395395
EditorGUILayout.EndFadeGroup();
396396
}
397397

398-
void DrawMethod() {
398+
private void DrawMethod() {
399399
switch (mode) {
400400
case MethodMode.Constructor:
401401
paramsFolded = EditorGUILayout.Foldout(paramsFolded, "Constructor");
@@ -425,7 +425,7 @@ void DrawMethod() {
425425
}
426426
}
427427

428-
void DrawResult() {
428+
private void DrawResult() {
429429
if (mode == MethodMode.Indexer) {
430430
if (result != null)
431431
result.Draw(false);
@@ -450,7 +450,7 @@ void DrawResult() {
450450
}
451451
}
452452

453-
void DrawExecButton() {
453+
private void DrawExecButton() {
454454
if (selectedMember == null) return;
455455
bool execute = false;
456456
switch (mode) {
@@ -470,7 +470,7 @@ void DrawExecButton() {
470470
Call();
471471
}
472472

473-
void RequireRedraw() {
473+
private void RequireRedraw() {
474474
if (OnRequireRedraw != null)
475475
OnRequireRedraw();
476476
}

UInspectorPlus/Helpers.cs

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using UnityObject = UnityEngine.Object;
1010

1111
namespace UInspectorPlus {
12-
enum PropertyType {
12+
internal enum PropertyType {
1313
Unknown,
1414
Bool,
1515
Enum,
@@ -35,25 +35,25 @@ enum PropertyType {
3535
RectInt,
3636
}
3737

38-
enum MethodMode {
38+
internal enum MethodMode {
3939
Method,
4040
Constructor,
4141
Indexer
4242
}
4343

44-
struct ComponentMethod {
44+
internal struct ComponentMethod {
4545
public MethodMode mode;
4646
public MemberInfo member;
4747
public object target;
4848
}
4949

50-
struct ComponentFields {
50+
internal struct ComponentFields {
5151
public FieldInfo field;
5252
public PropertyInfo property;
5353
public UnityObject target;
5454
}
5555

56-
interface IReflectorDrawer {
56+
internal interface IReflectorDrawer {
5757
void Draw();
5858
bool UpdateIfChanged();
5959
bool UpdateValue();
@@ -68,30 +68,18 @@ interface IReflectorDrawer {
6868
public static class Helper {
6969
internal static readonly Dictionary<Type, PropertyType> propertyTypeMapper = new Dictionary<Type, PropertyType>();
7070
internal static readonly Dictionary<Type, HashSet<string>> blackListedTypes = new Dictionary<Type, HashSet<string>>();
71-
static double clickTime;
72-
71+
private static double clickTime;
72+
7373
// Delegate hacks to access the internal methods
74-
internal static readonly Func<GUIContent, Rect, Gradient, Gradient> doGradientField = Delegate.CreateDelegate(
75-
typeof(Func<GUIContent, Rect, Gradient, Gradient>),
76-
typeof(EditorGUI).GetMethod(
77-
"GradientField",
78-
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static,
79-
null, CallingConventions.Any,
80-
new[] { typeof(GUIContent), typeof(Rect), typeof(Gradient) },
81-
null
82-
)
83-
) as Func<GUIContent, Rect, Gradient, Gradient>;
74+
private delegate Gradient DoGradientField(GUIContent guiContent, Rect rect, Gradient gradient);
75+
private static readonly DoGradientField doGradientField =
76+
GetDelegate<DoGradientField>(typeof(EditorGUI), "GradientField");
8477

85-
internal static readonly Func<GUIContent, Gradient, GUILayoutOption[], Gradient> doLayoutGradiantField = Delegate.CreateDelegate(
86-
typeof(Func<GUIContent, Gradient, GUILayoutOption[], Gradient>),
87-
typeof(EditorGUILayout).GetMethod(
88-
"GradientField",
89-
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static,
90-
null, CallingConventions.Any,
91-
new[] { typeof(GUIContent), typeof(Gradient), typeof(GUILayoutOption[]) },
92-
null
93-
)
94-
) as Func<GUIContent, Gradient, GUILayoutOption[], Gradient>;
78+
private delegate Gradient DoLayoutGradientField(GUIContent guiContent, Gradient gradient, params GUILayoutOption[] options);
79+
private static readonly DoLayoutGradientField doLayoutGradiantField =
80+
GetDelegate<DoLayoutGradientField>(typeof(EditorGUILayout), "GradientField");
81+
82+
private static readonly Hashtable storedState = new Hashtable();
9583

9684
internal static void InitPropertyTypeMapper() {
9785
if (propertyTypeMapper.Count > 0)
@@ -124,20 +112,20 @@ internal static void InitPropertyTypeMapper() {
124112
AddPropertyTypeMap("UnityEngine.Vector3Int, UnityEngine.dll", PropertyType.Vector3Int);
125113
AddPropertyTypeMap("UnityEngine.RectInt, UnityEngine.dll", PropertyType.RectInt);
126114

127-
// Danger properties! Do not use them or they will instanlize junks
115+
// Danger properties! Do not use them or they will instanate junks
128116
AddBlacklistedType("UnityEngine.MeshFilter, UnityEngine.dll", "mesh");
129117
AddBlacklistedType("UnityEngine.Renderer, UnityEngine.dll", "material", "materials");
130118
AddBlacklistedType("UnityEngine.Collider, UnityEngine.dll", "material");
131119
AddBlacklistedType("UnityEngine.Collider2D, UnityEngine.dll", "material");
132120
}
133121

134-
static void AddPropertyTypeMap(string typeName, PropertyType propType) {
122+
private static void AddPropertyTypeMap(string typeName, PropertyType propType) {
135123
Type type = Type.GetType(typeName, false, false);
136124
if (type != null)
137125
propertyTypeMapper.Add(type, propType);
138126
}
139127

140-
static void AddBlacklistedType(string typeName, params string[] props) {
128+
private static void AddBlacklistedType(string typeName, params string[] props) {
141129
Type type = Type.GetType(typeName, false, false);
142130
if (type == null) return;
143131
HashSet<string> list;
@@ -146,8 +134,6 @@ static void AddBlacklistedType(string typeName, params string[] props) {
146134
list.UnionWith(props);
147135
}
148136

149-
static readonly Hashtable storedState = new Hashtable();
150-
151137
internal static void StoreState(object key, object value) {
152138
if (storedState.ContainsKey(key))
153139
storedState[key] = value;
@@ -279,8 +265,8 @@ internal static object EnumField(GUIContent label, Type type, object value, para
279265
return EnumFieldPostProcess(itemValues, newVal);
280266
}
281267

282-
static int EnumFieldPreProcess(Type type, object rawValue, out GUIContent[] itemNames, out Array itemValues) {
283-
itemNames = Enum.GetNames(type).Select(x => new GUIContent(x)).ToArray();
268+
private static int EnumFieldPreProcess(Type type, object rawValue, out GUIContent[] itemNames, out Array itemValues) {
269+
itemNames = Array.ConvertAll(Enum.GetNames(type), x => new GUIContent(x));
284270
itemValues = Enum.GetValues(type);
285271
long val = Convert.ToInt64(rawValue);
286272
for (int i = 0; i < itemValues.Length; i++)
@@ -289,7 +275,7 @@ static int EnumFieldPreProcess(Type type, object rawValue, out GUIContent[] item
289275
return 0;
290276
}
291277

292-
static object EnumFieldPostProcess(Array itemValues, int val) {
278+
private static object EnumFieldPostProcess(Array itemValues, int val) {
293279
return itemValues.GetValue(val);
294280
}
295281

@@ -318,7 +304,7 @@ internal static object MaskedEnumField(GUIContent label, Type type, object mask,
318304
return MaskedEnumFieldPostProcess(type, itemValues, mask, val, newVal);
319305
}
320306

321-
static int MaskedEnumFieldPreProcess(Type type, object rawValue, out string[] itemNames, out Array itemValues) {
307+
private static int MaskedEnumFieldPreProcess(Type type, object rawValue, out string[] itemNames, out Array itemValues) {
322308
itemNames = Enum.GetNames(type);
323309
itemValues = Enum.GetValues(type);
324310
int maskVal = 0;
@@ -334,7 +320,7 @@ static int MaskedEnumFieldPreProcess(Type type, object rawValue, out string[] it
334320
return maskVal;
335321
}
336322

337-
static object MaskedEnumFieldPostProcess(Type enumType, Array itemValues, object rawValue, int maskVal, int newMaskVal) {
323+
private static object MaskedEnumFieldPostProcess(Type enumType, Array itemValues, object rawValue, int maskVal, int newMaskVal) {
338324
int changes = maskVal ^ newMaskVal;
339325
long value = Convert.ToInt64(rawValue), itemValue;
340326
for (int i = 0; i < itemValues.Length; i++)
@@ -429,22 +415,22 @@ internal static UnityObject ObjectField(Rect position, GUIContent label, UnityOb
429415
}
430416

431417
internal static Gradient GradientField(Rect position, GUIContent label, Gradient value) {
432-
return doGradientField.Invoke(label, position, value);
418+
return doGradientField(label, position, value);
433419
}
434420

435421
internal static Gradient GradientField(GUIContent label, Gradient value, params GUILayoutOption[] options) {
436-
return doLayoutGradiantField.Invoke(label, value, options);
422+
return doLayoutGradiantField(label, value, options);
437423
}
438424

439-
static void ClickObject(UnityObject obj) {
425+
private static void ClickObject(UnityObject obj) {
440426
var newClickTime = EditorApplication.timeSinceStartup;
441427
if (newClickTime - clickTime < 0.3 && obj != null)
442428
Selection.activeObject = obj;
443429
clickTime = newClickTime;
444430
EditorGUIUtility.PingObject(obj);
445431
}
446432

447-
static int CountLines(string str) {
433+
private static int CountLines(string str) {
448434
if (string.IsNullOrEmpty(str))
449435
return 1;
450436
int cursor = 0, count = 0, length = str.Length, i = -1;
@@ -551,7 +537,27 @@ internal static GUIStyle GetGUIStyle(string styleName) {
551537
return value == null ? defaultValue : (T)value;
552538
}
553539

554-
[MenuItem("Window/Script Tester/Inspector+")]
540+
internal static T GetDelegate<T>(Type fromType, string methodName) where T : class {
541+
Type t = typeof(T);
542+
if (!t.IsSubclassOf(typeof(Delegate)))
543+
throw new ArgumentException(string.Format("{0} is not delegate.", t.Name));
544+
MethodInfo invokeMethod = t.GetMethod("Invoke");
545+
if (invokeMethod == null)
546+
throw new ArgumentException(string.Format(
547+
"Cannot determine what parameters does {0} have, as no Invoke(...) signature found. Perhaps this is not a valid delegate.",
548+
t.Name
549+
));
550+
MethodInfo method = fromType.GetMethod(
551+
methodName,
552+
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static,
553+
null, CallingConventions.Any,
554+
Array.ConvertAll(invokeMethod.GetParameters(), p => p.ParameterType),
555+
null
556+
);
557+
return method == null ? null : Delegate.CreateDelegate(t, method) as T;
558+
}
559+
560+
[MenuItem("Window/Inspector+")]
555561
public static void ShowInspectorPlus() {
556562
EditorWindow.GetWindow(typeof(InspectorPlus));
557563
}

UInspectorPlus/HexEdit.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
namespace UInspectorPlus {
1010
internal class HexEdit {
11-
[SerializeField] Vector2 scrollPos;
11+
[SerializeField] private Vector2 scrollPos;
1212
public byte[] data;
1313
public int columns = 16;
14-
GUIContent temp = new GUIContent();
14+
private GUIContent temp = new GUIContent();
1515

1616
public float Height {
1717
get {

0 commit comments

Comments
 (0)