|
| 1 | +using UnityEngine; |
| 2 | +using UnityEditor; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Reflection; |
| 7 | +using ScriptTester; |
| 8 | + |
| 9 | +namespace ScriptTester { |
| 10 | + class ComponentMethodDrawer:IReflectorDrawer { |
| 11 | + UnityEngine.Object component; |
| 12 | + readonly List<ComponentMethod> methods = new List<ComponentMethod>(); |
| 13 | + string[] methodNames; |
| 14 | + int selectedMethodIndex; |
| 15 | + MethodInfo selectedMethod; |
| 16 | + ParameterInfo[] parameterInfo; |
| 17 | + MethodPropertyDrawer[] parameters; |
| 18 | + MethodPropertyDrawer result; |
| 19 | + Exception thrownException; |
| 20 | + bool titleFolded = true, paramsFolded = true, resultFolded = true, |
| 21 | + drawHeader = true, privateFields = true; |
| 22 | + |
| 23 | + public event Action OnRequireRedraw; |
| 24 | + |
| 25 | + public bool ShouldDrawHeader { |
| 26 | + get { return drawHeader; } |
| 27 | + set { |
| 28 | + drawHeader = value; |
| 29 | + paramsFolded &= value; |
| 30 | + resultFolded &= value; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public bool Changed { |
| 35 | + get { return false; } |
| 36 | + } |
| 37 | + |
| 38 | + public object Value { |
| 39 | + get { return result == null ? null : result.Value; } |
| 40 | + } |
| 41 | + |
| 42 | + public bool AllowPrivateFields { |
| 43 | + get { |
| 44 | + return privateFields; |
| 45 | + } |
| 46 | + set { |
| 47 | + privateFields = value; |
| 48 | + InitComponentMethods(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public MemberInfo Info { |
| 53 | + get { return selectedMethod as MemberInfo; } |
| 54 | + } |
| 55 | + |
| 56 | + public bool IsComponentNull() { |
| 57 | + return component == null; |
| 58 | + } |
| 59 | + |
| 60 | + public ComponentMethodDrawer() { |
| 61 | + } |
| 62 | + |
| 63 | + public ComponentMethodDrawer(UnityEngine.Object target, MethodInfo methodInfo) { |
| 64 | + component = target; |
| 65 | + InitComponentMethods(); |
| 66 | + selectedMethodIndex = methods.FindIndex(cm => cm.method == methodInfo); |
| 67 | + InitMethodParams(); |
| 68 | + } |
| 69 | + |
| 70 | + public void Call() { |
| 71 | + if(selectedMethod == null || component == null || parameters == null) |
| 72 | + return; |
| 73 | + try { |
| 74 | + thrownException = null; |
| 75 | + var requestData = parameters.Select(d => d.Value).ToArray(); |
| 76 | + var returnData = selectedMethod.Invoke(component, requestData); |
| 77 | + result = selectedMethod.ReturnType == typeof(void) ? |
| 78 | + null : |
| 79 | + new MethodPropertyDrawer(selectedMethod.ReturnType, "Return data", returnData); |
| 80 | + for(int i = 0; i < Math.Min(parameters.Length, requestData.Length); i++) { |
| 81 | + parameters[i].Value = requestData[i]; |
| 82 | + if(parameters[i].ReferenceMode) |
| 83 | + Helper.AssignValue(parameters[i].RefFieldInfo, parameters[i].Component, requestData[i]); |
| 84 | + } |
| 85 | + } catch(Exception ex) { |
| 86 | + thrownException = ex.InnerException ?? ex; |
| 87 | + Debug.LogException(thrownException); |
| 88 | + throw; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public void Draw() { |
| 93 | + if(drawHeader) { |
| 94 | + EditorGUI.BeginDisabledGroup(component == null); |
| 95 | + titleFolded = EditorGUILayout.InspectorTitlebar(titleFolded, component) || component == null; |
| 96 | + EditorGUI.EndDisabledGroup(); |
| 97 | + } |
| 98 | + GUI.changed = false; |
| 99 | + if(component == null || titleFolded || !drawHeader) { |
| 100 | + if(drawHeader) { |
| 101 | + EditorGUI.indentLevel++; |
| 102 | + EditorGUILayout.BeginVertical(); |
| 103 | + component = EditorGUILayout.ObjectField("Target", component, typeof(UnityEngine.Object), true); |
| 104 | + } |
| 105 | + if(component != null) |
| 106 | + DrawComponent(); |
| 107 | + if(result != null || thrownException != null) |
| 108 | + DrawResult(); |
| 109 | + if(drawHeader) { |
| 110 | + EditorGUILayout.EndVertical(); |
| 111 | + EditorGUI.indentLevel--; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + void AddComponentMethod(UnityEngine.Object target) { |
| 117 | + BindingFlags flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public; |
| 118 | + if(privateFields) |
| 119 | + flag |= BindingFlags.NonPublic; |
| 120 | + methods.AddRange(component.GetType().GetMethods(flag).Select(m => new ComponentMethod { |
| 121 | + method = m, |
| 122 | + target = target |
| 123 | + })); |
| 124 | + } |
| 125 | + |
| 126 | + void InitComponentMethods() { |
| 127 | + methods.Clear(); |
| 128 | + AddComponentMethod(component); |
| 129 | + var gameObject = component as GameObject; |
| 130 | + if(gameObject != null) |
| 131 | + foreach(var c in gameObject.GetComponents(typeof(Component))) |
| 132 | + AddComponentMethod(c); |
| 133 | + methodNames = methods.Select(m => string.Format( |
| 134 | + "{0} ({1})/{2} ({3} parameters)", |
| 135 | + m.target.GetType().Name, |
| 136 | + m.target.GetInstanceID(), |
| 137 | + Helper.GetMemberName(m.method as MemberInfo), |
| 138 | + m.method.GetParameters().Length |
| 139 | + )).ToArray(); |
| 140 | + selectedMethodIndex = -1; |
| 141 | + selectedMethod = null; |
| 142 | + parameterInfo = null; |
| 143 | + parameters = null; |
| 144 | + result = null; |
| 145 | + thrownException = null; |
| 146 | + } |
| 147 | + |
| 148 | + void InitMethodParams() { |
| 149 | + selectedMethod = methods[selectedMethodIndex].method; |
| 150 | + component = methods[selectedMethodIndex].target; |
| 151 | + parameterInfo = selectedMethod.GetParameters(); |
| 152 | + parameters = new MethodPropertyDrawer[parameterInfo.Length]; |
| 153 | + for(int i = 0; i < parameterInfo.Length; i++) { |
| 154 | + var info = parameterInfo[i]; |
| 155 | + parameters[i] = new MethodPropertyDrawer(info.ParameterType, info.Name, info.IsOptional ? info.DefaultValue : null); |
| 156 | + parameters[i].OnRequireRedraw += RequireRedraw; |
| 157 | + } |
| 158 | + result = null; |
| 159 | + thrownException = null; |
| 160 | + } |
| 161 | + |
| 162 | + void DrawComponent() { |
| 163 | + if(GUI.changed) { |
| 164 | + InitComponentMethods(); |
| 165 | + GUI.changed = false; |
| 166 | + } |
| 167 | + if(drawHeader) |
| 168 | + selectedMethodIndex = EditorGUILayout.Popup("Method", selectedMethodIndex, methodNames); |
| 169 | + if(selectedMethodIndex >= 0) |
| 170 | + DrawMethod(); |
| 171 | + } |
| 172 | + |
| 173 | + void DrawMethod() { |
| 174 | + if(GUI.changed) { |
| 175 | + InitMethodParams(); |
| 176 | + GUI.changed = false; |
| 177 | + } |
| 178 | + if(paramsFolded = EditorGUILayout.Foldout(paramsFolded, selectedMethod.Name)) { |
| 179 | + GUI.changed = false; |
| 180 | + EditorGUI.indentLevel++; |
| 181 | + EditorGUILayout.BeginVertical(); |
| 182 | + if(selectedMethod.ContainsGenericParameters) |
| 183 | + EditorGUILayout.HelpBox("Generic method is not supported.", MessageType.Warning); |
| 184 | + else { |
| 185 | + if(parameterInfo.Length == 0) |
| 186 | + EditorGUILayout.HelpBox("There is no parameters required for this method.", MessageType.Info); |
| 187 | + foreach(var drawer in parameters) |
| 188 | + drawer.Draw(); |
| 189 | + } |
| 190 | + EditorGUILayout.EndVertical(); |
| 191 | + EditorGUI.indentLevel--; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + void DrawResult() { |
| 196 | + if(resultFolded = EditorGUILayout.Foldout(resultFolded, "Result")) { |
| 197 | + GUI.changed = false; |
| 198 | + EditorGUI.indentLevel++; |
| 199 | + EditorGUILayout.BeginVertical(); |
| 200 | + if(result != null) |
| 201 | + result.Draw(true); |
| 202 | + if(thrownException != null) |
| 203 | + EditorGUILayout.HelpBox(thrownException.Message, MessageType.Error); |
| 204 | + EditorGUILayout.EndVertical(); |
| 205 | + EditorGUI.indentLevel--; |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + void RequireRedraw() { |
| 210 | + if(OnRequireRedraw != null) |
| 211 | + OnRequireRedraw(); |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments