public static void DrawDefaultMethods(this Editor editor) { string[] ignoreMethods = new string[] { }; var script = editor.target; List <MethodInfo> methods = editor.target.GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) // Instance methods, both public and private/protected //.Where(x => x.DeclaringType == editor.target.GetType()) // Only list methods defined in our own class .Where(x => !ignoreMethods.Any(n => n == x.Name)) // Don't list methods in the ignoreMethods array (so we can exclude Unity specific methods, etc. .Select(x => x).ToList(); foreach (var method in methods) { string name; //ButtonAttribute buttonAtt = (ButtonAttribute)Attribute.GetCustomAttribute(target.GetType().GetMethod(method).GetCustomAttributes(true).OfType<ButtonAttribute>().FirstOrDefault(), typeof(ButtonAttribute)); var buttonAtt = method.GetCustomAttributes(typeof(ButtonAttribute), true).FirstOrDefault(); name = (buttonAtt != null) ? " [Button]" : ""; // name = target.GetType().GetMethod(method).GetCustomAttributes if (buttonAtt != null) { var argList = method.GetParameters().ToList(); var argValue = argList.Select(x => x.DefaultValue).ToList(); GUILayout.BeginHorizontal(); if (argList.Count > 0) { GUILayout.BeginVertical(); for (int i = 0; i < argList.Count; i++) { if (argList[i].ParameterType.Equals(typeof(int))) { argValue[i] = EditorGUILayout.IntField(argList[i].Name, (int)argValue[i]); } } GUILayout.EndVertical(); } if (GUILayout.Button(method.Name)) { if (argList.Count == 0) { try { Debug.Log("Method \"" + method.Name + "\" returned: " + method.Invoke(script, null)); } catch { ((MonoBehaviour)script).SendMessage(method.Name, SendMessageOptions.DontRequireReceiver); } } else { MethodButtonWindow.ShowWindow(script, method, argList, argValue); } } GUILayout.EndHorizontal(); } } }
public static void ShowWindow(object script, MethodInfo method, List <ParameterInfo> argList, List <object> argValue) { MethodButtonWindow newWindow = GetWindow <MethodButtonWindow>(method.Name); newWindow._script = script; newWindow._method = method; newWindow._argList = argList; newWindow._argValue = argValue; }