Skip to content

Commit e304534

Browse files
committed
Added constructor and grab value mode
1 parent 0440473 commit e304534

File tree

4 files changed

+155
-72
lines changed

4 files changed

+155
-72
lines changed

Assets/Script Tester/Editor/ComponentMethodDrawer.cs

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ class ComponentMethodDrawer:IReflectorDrawer {
1616
AnimBool showResultSelector;
1717
string[] methodNames;
1818
int selectedMethodIndex;
19+
ConstructorInfo selectedCtor;
1920
MethodInfo selectedMethod;
2021
ParameterInfo[] parameterInfo;
2122
MethodPropertyDrawer[] parameters;
2223
MethodPropertyDrawer result;
2324
Exception thrownException;
2425
string filter;
26+
Type ctorType;
2527
bool titleFolded = true, paramsFolded = true, resultFolded = true,
26-
drawHeader = true, privateFields = true, obsolete = true;
28+
drawHeader = true, privateFields = true, obsolete = true, ctorMode = false;
2729

2830
public event Action OnRequireRedraw;
2931

@@ -65,7 +67,7 @@ public bool AllowObsolete {
6567
}
6668

6769
public MemberInfo Info {
68-
get { return selectedMethod as MemberInfo; }
70+
get { return ctorMode ? selectedCtor as MemberInfo : selectedMethod as MemberInfo; }
6971
}
7072

7173
public bool IsComponentNull() {
@@ -89,6 +91,15 @@ public ComponentMethodDrawer(object target)
8991
InitComponentMethods();
9092
}
9193

94+
public ComponentMethodDrawer(Type type)
95+
: this() {
96+
ctorMode = true;
97+
ctorType = type;
98+
drawHeader = false;
99+
showMethodSelector.value = true;
100+
InitComponentMethods();
101+
}
102+
92103
public string Filter {
93104
get { return filter; }
94105
set {
@@ -98,15 +109,20 @@ public string Filter {
98109
}
99110

100111
public void Call() {
101-
if(selectedMethod == null || component == null || parameters == null)
112+
if(selectedMethod == null || ctorMode ? ctorType == null : component == null || parameters == null)
102113
return;
103114
try {
104115
thrownException = null;
105116
var requestData = parameters.Select(d => d.Value).ToArray();
106-
var returnData = selectedMethod.Invoke(component, requestData);
107-
result = selectedMethod.ReturnType == typeof(void) ?
117+
if(ctorMode) {
118+
var returnData = selectedCtor.Invoke(requestData);
119+
result = new MethodPropertyDrawer(selectedCtor.ReflectedType, "Constructed object", returnData, privateFields, obsolete);
120+
} else {
121+
var returnData = selectedMethod.Invoke(component, requestData);
122+
result = selectedMethod.ReturnType == typeof(void) ?
108123
null :
109124
new MethodPropertyDrawer(selectedMethod.ReturnType, "Return data", returnData, privateFields, obsolete);
125+
}
110126
for(int i = 0; i < Math.Min(parameters.Length, requestData.Length); i++) {
111127
parameters[i].Value = requestData[i];
112128
if(parameters[i].ReferenceMode)
@@ -132,7 +148,7 @@ public void Draw() {
132148
EditorGUILayout.BeginVertical();
133149
component = EditorGUILayout.ObjectField("Target", component as UnityEngine.Object, typeof(UnityEngine.Object), true);
134150
}
135-
if(component != null) {
151+
if(component != null || ctorMode) {
136152
if(GUI.changed) {
137153
InitComponentMethods();
138154
GUI.changed = false;
@@ -143,7 +159,7 @@ public void Draw() {
143159
if(EditorGUILayout.BeginFadeGroup(showMethodSelector.faded))
144160
DrawComponent();
145161
EditorGUILayout.EndFadeGroup();
146-
showResultSelector.target = result != null || thrownException != null;
162+
showResultSelector.target = (!ctorMode && result != null) || thrownException != null;
147163
if(EditorGUILayout.BeginFadeGroup(showResultSelector.faded))
148164
DrawResult();
149165
EditorGUILayout.EndFadeGroup();
@@ -154,6 +170,20 @@ public void Draw() {
154170
}
155171
}
156172

173+
void AddComponentMethod(Type type) {
174+
BindingFlags flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
175+
if(privateFields)
176+
flag |= BindingFlags.NonPublic;
177+
methods.AddRange(
178+
type.GetConstructors(flag)
179+
.Where(t => obsolete || !Attribute.IsDefined(t, typeof(ObsoleteAttribute)))
180+
.Where(t => string.IsNullOrEmpty(filter) || t.Name.IndexOf(filter, StringComparison.CurrentCultureIgnoreCase) >= 0)
181+
.Select(m => new ComponentMethod {
182+
ctorInfo = m
183+
})
184+
);
185+
}
186+
157187
void AddComponentMethod(object target) {
158188
BindingFlags flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
159189
if(privateFields)
@@ -171,8 +201,16 @@ void AddComponentMethod(object target) {
171201

172202
void InitComponentMethods(bool resetIndex = true) {
173203
methods.Clear();
174-
AddComponentMethod(component);
175-
if(drawHeader) {
204+
if(ctorMode)
205+
AddComponentMethod(ctorType);
206+
else
207+
AddComponentMethod(component);
208+
if(ctorMode)
209+
methodNames = methods.Select(m => string.Format(
210+
"Constructor ({0} parameters)",
211+
m.ctorInfo.GetParameters().Length
212+
)).ToArray();
213+
else if(drawHeader) {
176214
var gameObject = component as GameObject;
177215
if(gameObject != null)
178216
foreach(var c in gameObject.GetComponents(typeof(Component)))
@@ -198,16 +236,23 @@ void InitComponentMethods(bool resetIndex = true) {
198236
}
199237
selectedMethodIndex = -1;
200238
selectedMethod = null;
239+
selectedCtor = null;
201240
parameterInfo = null;
202241
parameters = null;
203242
result = null;
204243
thrownException = null;
205244
}
206245

207246
void InitMethodParams() {
208-
selectedMethod = methods[selectedMethodIndex].method;
209-
component = methods[selectedMethodIndex].target;
210-
parameterInfo = selectedMethod.GetParameters();
247+
if(ctorMode) {
248+
selectedCtor = methods[selectedMethodIndex].ctorInfo;
249+
component = null;
250+
parameterInfo = selectedCtor.GetParameters();
251+
} else {
252+
selectedMethod = methods[selectedMethodIndex].method;
253+
component = methods[selectedMethodIndex].target;
254+
parameterInfo = selectedMethod.GetParameters();
255+
}
211256
parameters = new MethodPropertyDrawer[parameterInfo.Length];
212257
for(int i = 0; i < parameterInfo.Length; i++) {
213258
var info = parameterInfo[i];
@@ -219,7 +264,7 @@ void InitMethodParams() {
219264
}
220265

221266
void DrawComponent() {
222-
selectedMethodIndex = EditorGUILayout.Popup("Method", selectedMethodIndex, methodNames);
267+
selectedMethodIndex = EditorGUILayout.Popup(ctorMode ? "Constructor" : "Method", selectedMethodIndex, methodNames);
223268
if(selectedMethodIndex >= 0) {
224269
if(GUI.changed) {
225270
InitMethodParams();
@@ -234,11 +279,11 @@ void DrawComponent() {
234279
}
235280

236281
void DrawMethod() {
237-
if(paramsFolded = EditorGUILayout.Foldout(paramsFolded, selectedMethod.Name)) {
282+
if(paramsFolded = EditorGUILayout.Foldout(paramsFolded, ctorMode ? "Constructor" : selectedMethod.Name)) {
238283
GUI.changed = false;
239284
EditorGUI.indentLevel++;
240285
EditorGUILayout.BeginVertical();
241-
if(selectedMethod.ContainsGenericParameters)
286+
if(ctorMode ? selectedCtor.ContainsGenericParameters : selectedMethod.ContainsGenericParameters)
242287
EditorGUILayout.HelpBox("Generic method is not supported.", MessageType.Warning);
243288
else {
244289
if(parameterInfo.Length == 0)
@@ -256,7 +301,7 @@ void DrawResult() {
256301
GUI.changed = false;
257302
EditorGUI.indentLevel++;
258303
EditorGUILayout.BeginVertical();
259-
if(result != null)
304+
if(result != null && !ctorMode)
260305
result.Draw(true);
261306
if(thrownException != null)
262307
EditorGUILayout.HelpBox(thrownException.Message, MessageType.Error);

Assets/Script Tester/Editor/Helpers.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ enum PropertyType {
3030
}
3131

3232
struct ComponentMethod {
33+
public ConstructorInfo ctorInfo;
3334
public MethodInfo method;
3435
public object target;
3536
}
@@ -416,9 +417,9 @@ internal static bool FetchValue(MemberInfo info, object target, out object value
416417

417418
internal static int ObjIdOrHashCode(object obj) {
418419
var unityObj = obj as UnityEngine.Object;
419-
if (unityObj != null)
420+
if(unityObj != null)
420421
return unityObj.GetInstanceID();
421-
if (obj != null)
422+
if(obj != null)
422423
return obj.GetHashCode();
423424
return 0;
424425
}

Assets/Script Tester/Editor/InspectorDrawer.cs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,79 +19,79 @@ public InspectorDrawer(object target, bool shown, bool showProps, bool showPriva
1919
this.target = target;
2020
this.drawer = new List<IReflectorDrawer>();
2121
BindingFlags flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
22-
if (showPrivateFields)
22+
if(showPrivateFields)
2323
flag |= BindingFlags.NonPublic;
2424
targetType = target.GetType();
2525
var fields = targetType.GetFields(flag);
2626
var props = !showProps ? null : targetType.GetProperties(flag).Where(prop => prop.GetIndexParameters().Length == 0).ToArray();
2727
isInternalType = !(target is MonoBehaviour) || Attribute.IsDefined(target.GetType(), typeof(ExecuteInEditMode));
28-
foreach (var field in fields)
28+
foreach(var field in fields)
2929
try {
30-
if (!showObsolete && Attribute.IsDefined(field, typeof(ObsoleteAttribute)))
30+
if(!showObsolete && Attribute.IsDefined(field, typeof(ObsoleteAttribute)))
3131
continue;
3232
drawer.Add(new MethodPropertyDrawer(field.FieldType, Helper.GetMemberName(field, true), field.GetValue(target), showPrivateFields, showObsolete) {
3333
AllowReferenceMode = false,
3434
Info = field
3535
});
36-
} catch (Exception ex) {
36+
} catch(Exception ex) {
3737
Debug.LogException(ex);
3838
}
39-
if (showProps)
40-
foreach (var prop in props)
39+
if(showProps)
40+
foreach(var prop in props)
4141
try {
42-
if (!showObsolete && Attribute.IsDefined(prop, typeof(ObsoleteAttribute)))
42+
if(!showObsolete && Attribute.IsDefined(prop, typeof(ObsoleteAttribute)))
4343
continue;
4444
drawer.Add(new MethodPropertyDrawer(prop.PropertyType, Helper.GetMemberName(prop, true), prop.CanRead && EditorApplication.isPlaying ? prop.GetValue(target, null) : null, showPrivateFields, showObsolete) {
4545
AllowReferenceMode = false,
4646
Info = prop,
4747
Updatable = isInternalType || Helper.GetState<bool>(prop, true),
4848
ShowUpdatable = !isInternalType
4949
});
50-
} catch (Exception ex) {
50+
} catch(Exception ex) {
5151
Debug.LogException(ex);
5252
}
53-
if (showMethods)
53+
if(showMethods)
5454
drawer.Add(new ComponentMethodDrawer(target) { AllowPrivateFields = showPrivateFields });
55-
foreach (var d in drawer)
55+
foreach(var d in drawer)
5656
d.OnRequireRedraw += RequireRedraw;
5757
this.shown = Helper.GetState<bool>(target, shown);
5858
}
5959

6060
public void Draw(bool drawHeader = true, bool readOnly = false) {
61-
if (drawHeader) {
61+
if(drawHeader) {
6262
shown = EditorGUILayout.InspectorTitlebar(shown, target as UnityEngine.Object);
6363
Helper.StoreState(target, shown);
64-
if (!shown)
64+
if(!shown)
6565
return;
6666
}
6767
EditorGUI.indentLevel++;
6868
EditorGUILayout.BeginVertical();
69-
foreach (var item in drawer) {
69+
foreach(var item in drawer) {
7070
var methodDrawer = item as ComponentMethodDrawer;
7171
var fieldDrawer = item as MethodPropertyDrawer;
72-
if (methodDrawer != null) {
72+
if(methodDrawer != null) {
7373
EditorGUI.indentLevel--;
7474
EditorGUILayout.BeginHorizontal();
7575
EditorGUILayout.LabelField(GUIContent.none, GUILayout.Width(EditorGUIUtility.singleLineHeight));
7676
EditorGUILayout.BeginVertical();
7777
methodDrawer.Draw();
78-
if (methodDrawer.Info != null && GUILayout.Button("Execute " + methodDrawer.Info.Name))
78+
if(methodDrawer.Info != null && GUILayout.Button("Execute " + methodDrawer.Info.Name))
7979
methodDrawer.Call();
8080
EditorGUILayout.EndVertical();
8181
EditorGUILayout.EndHorizontal();
8282
EditorGUI.indentLevel++;
83-
} else if (item != null) {
84-
if (item.Info != null && !string.IsNullOrEmpty(searchText) && item.Info.Name.IndexOf(searchText, StringComparison.CurrentCultureIgnoreCase) < 0)
83+
} else if(item != null) {
84+
if(item.Info != null && !string.IsNullOrEmpty(searchText) && item.Info.Name.IndexOf(searchText, StringComparison.CurrentCultureIgnoreCase) < 0)
8585
continue;
86-
if (fieldDrawer != null)
86+
if(fieldDrawer != null)
8787
fieldDrawer.Draw(readOnly);
8888
else
8989
item.Draw();
90-
if (item.Changed) {
91-
if (!Helper.AssignValue(item.Info, target, item.Value)) {
90+
if(item.Changed) {
91+
if(!Helper.AssignValue(item.Info, target, item.Value)) {
9292
object value;
9393
var propDrawer = item as MethodPropertyDrawer;
94-
if (propDrawer != null && Helper.FetchValue(propDrawer.Info, target, out value)) {
94+
if(propDrawer != null && Helper.FetchValue(propDrawer.Info, target, out value)) {
9595
propDrawer.Value = value;
9696
propDrawer.GetException = null;
9797
} else
@@ -105,15 +105,15 @@ public void Draw(bool drawHeader = true, bool readOnly = false) {
105105
}
106106

107107
public void UpdateValues(bool updateProps) {
108-
foreach (var drawerItem in drawer) {
108+
foreach(var drawerItem in drawer) {
109109
var propDrawer = drawerItem as MethodPropertyDrawer;
110-
if (propDrawer == null)
110+
if(propDrawer == null)
111111
continue;
112112
var isPropInfo = propDrawer.Info is PropertyInfo;
113-
if (!isInternalType && (!updateProps || !propDrawer.Updatable) && isPropInfo)
113+
if(!isInternalType && (!updateProps || !propDrawer.Updatable) && isPropInfo)
114114
continue;
115115
object value;
116-
if (Helper.FetchValue(propDrawer.Info, target, out value)) {
116+
if(Helper.FetchValue(propDrawer.Info, target, out value)) {
117117
propDrawer.Value = value;
118118
propDrawer.GetException = null;
119119
} else
@@ -122,7 +122,7 @@ public void UpdateValues(bool updateProps) {
122122
}
123123

124124
void RequireRedraw() {
125-
if (OnRequireRedraw != null)
125+
if(OnRequireRedraw != null)
126126
OnRequireRedraw();
127127
}
128128
}

0 commit comments

Comments
 (0)