10using System.Reflection;
26 public readonly
string label =
"Quick Button";
33 this.label = buttonLabel;
43 public float GetWidth() {
47 public void Draw(Rect rect, SerializedProperty property) {
49 var type =
targets.Query().FirstOrDefault().GetType();
50 var namedMethods = type.GetMethods(BindingFlags.Instance |
52 BindingFlags.NonPublic).
55 MethodInfo method =
null;
56 string errorMessage =
null;
59 if (namedMethods.Count() == 0) {
60 errorMessage =
"QuickButton tried to prepare " +
methodOnPress +
" for calling, " +
61 "but the type " + type.Name +
" has no such method.";
63 var validMethods = namedMethods.Where(m => m.GetParameters().
64 All(p => p.IsOptional));
66 if (validMethods.Count() == 0) {
67 errorMessage =
"QuickButton tried to prepare " +
methodOnPress +
" for calling, " +
68 "but the type " + type.Name +
" had no valid methods.";
69 }
else if (validMethods.Count() > 1) {
70 errorMessage =
"QuickButton tried to prepare " +
methodOnPress +
" for calling, " +
71 "but the type " + type.Name +
" had more than one valid method.";
73 method = validMethods.Single();
77 Color prevColor = GUI.color;
79 Debug.LogError(errorMessage);
80 buttonTooltip = errorMessage;
81 GUI.color =
Color.red;
84 using (
new EditorGUI.DisabledScope(method ==
null)) {
85 if (GUI.Button(rect.PadInner(0, 0, 0, 0),
new GUIContent(
label, buttonTooltip))) {
86 foreach (var target
in targets) {
87 if (target is MonoBehaviour) {
88 Undo.RegisterFullObjectHierarchyUndo((target as MonoBehaviour).gameObject,
89 "Perform QuickButton Action");
91 Undo.RegisterFullObjectHierarchyUndo(target,
"Perform QuickButton Action");
94 foreach (var target
in targets) {
97 method.GetParameters().Select(p => p.DefaultValue).ToArray());
98 }
catch (TargetInvocationException e) {
99 Debug.LogError(
"Quick button received an error while trying to invoke " +
methodOnPress +
" on " + type.Name);
100 Debug.LogException(e.InnerException);
106 GUI.color = prevColor;