private void RefreshMethodButtons(NotifyCollectionChangedAction action, ConsoleMethodInfo method) { switch (action) { case NotifyCollectionChangedAction.Add: if (methodInfo2Go.ContainsKey(method)) { Debug.LogWarning($"重复添加method-{method.method.Name}"); return; } var btn = Instantiate(commandBtnTemplate, commandBtnContent, false); btn.Init(method); btn.gameObject.SetActive(true); methodInfo2Go[method] = btn; break; case NotifyCollectionChangedAction.Remove: if (!methodInfo2Go.ContainsKey(method)) { Debug.LogWarning($"删除不存在的method-{method.method.Name}"); return; } methodInfo2Go.Remove(method); break; } }
public void SetContent(ConsoleMethodInfo methodInfo, int index) { this.MethodInfo = methodInfo; this.Index = index; Vector2 size = transformComponent.sizeDelta; size.y = manager.ItemHeight; transformComponent.sizeDelta = size; text.text = methodInfo.signature; }
public void Init(ConsoleMethodInfo consoleMethodInfo) { Input.placeholder.GetComponent <Text>().text = "请输入参数,类型在上面,每个参数用空格分隔"; Input.gameObject.SetActive(consoleMethodInfo.parameterTypes.Length > 0); oldName = $">{consoleMethodInfo.command}({string.Join("",consoleMethodInfo.parameters)})"; NameTxt.text = oldName; DescTxt.text = consoleMethodInfo.description; Btn.onClick.AddListener(() => { DebugLogConsole.ExecuteCommand($"{consoleMethodInfo.command} {Input.text}"); btnImg.color = Color.green; NameTxt.text = "执行成功!!"; StartCoroutine(Delay(1, () => { btnImg.color = Color.white; NameTxt.text = oldName; })); }); }
private static void AddCommand(string command, string description, MethodInfo method, object instance = null) { // Fetch the parameters of the class ParameterInfo[] parameters = method.GetParameters(); if (parameters == null) { parameters = new ParameterInfo[0]; } bool isMethodValid = true; // Store the parameter types in an array Type[] parameterTypes = new Type[parameters.Length]; for (int k = 0; k < parameters.Length; k++) { Type parameterType = parameters[k].ParameterType; if (parseFunctions.ContainsKey(parameterType)) { parameterTypes[k] = parameterType; } else { isMethodValid = false; break; } } // If method is valid, associate it with the entered command if (isMethodValid) { StringBuilder methodSignature = new StringBuilder(256); methodSignature.Append(command).Append(": "); if (!string.IsNullOrEmpty(description)) { methodSignature.Append(description).Append(" -> "); } methodSignature.Append(method.DeclaringType.ToString()).Append(".").Append(method.Name).Append("("); for (int i = 0; i < parameterTypes.Length; i++) { Type type = parameterTypes[i]; string typeName; if (!typeReadableNames.TryGetValue(type, out typeName)) { typeName = type.Name; } methodSignature.Append(typeName); if (i < parameterTypes.Length - 1) { methodSignature.Append(", "); } } methodSignature.Append(")"); Type returnType = method.ReturnType; if (returnType != typeof(void)) { string returnTypeName; if (!typeReadableNames.TryGetValue(returnType, out returnTypeName)) { returnTypeName = returnType.Name; } methodSignature.Append(" : ").Append(returnTypeName); } methods[command] = new ConsoleMethodInfo(method, parameterTypes, instance, methodSignature.ToString()); } }
private static void AddCommand(string command, string description, MethodInfo method, object instance = null) { if (string.IsNullOrEmpty(command)) { Debug.LogError("Command name can't be empty!"); return; } command = command.Trim(); if (command.IndexOf(' ') >= 0) { Debug.LogError("Command name can't contain whitespace: " + command); return; } // Fetch the parameters of the class ParameterInfo[] parameters = method.GetParameters(); if (parameters == null) { parameters = new ParameterInfo[0]; } bool isMethodValid = true; // Store the parameter types in an array Type[] parameterTypes = new Type[parameters.Length]; for (int k = 0; k < parameters.Length; k++) { Type parameterType = parameters[k].ParameterType; if (parseFunctions.ContainsKey(parameterType) || typeof(Component).IsAssignableFrom(parameterType)) { parameterTypes[k] = parameterType; } else { Debug.LogError("Invalid method \"" + command + "\" can not parse: " + parameterType); isMethodValid = false; break; } } // If method is valid, associate it with the entered command if (isMethodValid) { StringBuilder methodSignature = new StringBuilder(256); methodSignature.Append(command).Append(": "); if (!string.IsNullOrEmpty(description)) { methodSignature.Append(description).Append(" -> "); } methodSignature.Append(method.DeclaringType.ToString()).Append(".").Append(method.Name).Append("("); for (int i = 0; i < parameterTypes.Length; i++) { Type type = parameterTypes[i]; string typeName; if (!typeReadableNames.TryGetValue(type, out typeName)) { typeName = type.Name; } methodSignature.Append(typeName); if (i < parameterTypes.Length - 1) { methodSignature.Append(", "); } } methodSignature.Append(")"); Type returnType = method.ReturnType; if (returnType != typeof(void)) { string returnTypeName; if (!typeReadableNames.TryGetValue(returnType, out returnTypeName)) { returnTypeName = returnType.Name; } methodSignature.Append(" : ").Append(returnTypeName); } methods[command] = new ConsoleMethodInfo(method, parameterTypes, instance, methodSignature.ToString()); } }
// Create a new command and set its properties private static void AddCommand(string command, string methodName, System.Type ownerType, object instance = null) { // Get the method from the class MethodInfo method = ownerType.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); if (method == null) { Debug.LogError(methodName + " does not exist in " + ownerType); return; } // Fetch the parameters of the class ParameterInfo[] parameters = method.GetParameters(); if (parameters == null || parameters.Length == 0) { // Method takes no parameters string methodSignature = method.ToString(); methodSignature = methodSignature.Replace("Void ", ""); methodSignature = command + ": " + ownerType.ToString() + "." + methodSignature; // Associate the method with the entered command methods[command] = new ConsoleMethodInfo(method, new System.Type[] { }, instance, methodSignature); } else { // Method takes parameter(s), check their types // to see if this method is valid (can be called by a command) bool isMethodValid = true; // Store the parameter types in an array System.Type[] parameterTypes = new System.Type[parameters.Length]; for (int k = 0; k < parameters.Length; k++) { System.Type parameterType = parameters[k].ParameterType; if (parameterType == typeof(int) || parameterType == typeof(float) || parameterType == typeof(bool) || parameterType == typeof(string) || parameterType == typeof(Vector2) || parameterType == typeof(Vector3) || parameterType == typeof(Vector4)) { parameterTypes[k] = parameterType; } else { isMethodValid = false; break; } } // If method is valid, associate it with the entered command if (isMethodValid) { string methodSignature = method.ToString(); methodSignature = methodSignature.Replace("Int32", "Integer"); methodSignature = methodSignature.Replace("Single", "Float"); methodSignature = methodSignature.Replace("System.", ""); methodSignature = methodSignature.Replace("Void ", ""); methodSignature = command + ": " + ownerType.ToString() + "." + methodSignature; methods[command] = new ConsoleMethodInfo(method, parameterTypes, instance, methodSignature); } } }