Tanoda
QuickButton.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System.Linq;
10using System.Reflection;
11using UnityEngine;
12using Leap.Unity.Query;
13
14#if UNITY_EDITOR
15using UnityEditor;
16#endif
17
18namespace Leap.Unity.Attributes {
19
22 {
23
24 public const float PADDING_RIGHT = 12f;
25
26 public readonly string label = "Quick Button";
27 public readonly string methodOnPress = null;
28 public readonly string tooltip = "";
29
30 public QuickButtonAttribute(string buttonLabel, string methodOnPress,
31 string tooltip = "")
32 {
33 this.label = buttonLabel;
34 this.methodOnPress = methodOnPress;
35 this.tooltip = tooltip;
36 }
37
38#if UNITY_EDITOR
43 public float GetWidth() {
44 return GUI.skin.label.CalcSize(new GUIContent(label)).x + PADDING_RIGHT;
45 }
46
47 public void Draw(Rect rect, SerializedProperty property) {
48
49 var type = targets.Query().FirstOrDefault().GetType();
50 var namedMethods = type.GetMethods(BindingFlags.Instance |
51 BindingFlags.Public |
52 BindingFlags.NonPublic).
53 Where(m => m.Name == methodOnPress);
54
55 MethodInfo method = null;
56 string errorMessage = null;
57 string buttonTooltip = tooltip;
58
59 if (namedMethods.Count() == 0) {
60 errorMessage = "QuickButton tried to prepare " + methodOnPress + " for calling, " +
61 "but the type " + type.Name + " has no such method.";
62 } else {
63 var validMethods = namedMethods.Where(m => m.GetParameters().
64 All(p => p.IsOptional));
65
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.";
72 } else {
73 method = validMethods.Single();
74 }
75 }
76
77 Color prevColor = GUI.color;
78 if (method == null) {
79 Debug.LogError(errorMessage);
80 buttonTooltip = errorMessage;
81 GUI.color = Color.red;
82 }
83
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");
90 } else {
91 Undo.RegisterFullObjectHierarchyUndo(target, "Perform QuickButton Action");
92 }
93 }
94 foreach (var target in targets) {
95 try {
96 method.Invoke(target,
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);
101 }
102 }
103 }
104 }
105
106 GUI.color = prevColor;
107 }
108#endif
109 }
110}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
UnityEngine.Color Color
Definition: TestScript.cs:32
QuickButtonAttribute(string buttonLabel, string methodOnPress, string tooltip="")
Definition: QuickButton.cs:30