Tanoda
EditorGUIPanel.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;
13using System;
14
15#if UNITY_EDITOR
16using UnityEditor;
17#endif
18
19namespace Leap.Unity.Attributes {
20
23 {
24
25 public const float LINE_HEIGHT = 20f;
26
27 public int heightInLines;
28 public readonly string editorMethodName;
29
50 int heightInLines = 1)
51 {
52 this.heightInLines = heightInLines;
53 this.editorMethodName = editorMethodName;
54 }
55
56#if UNITY_EDITOR
57 private Action<Rect, UnityEngine.Object[]> _cachedDelegate;
58
59 public float GetHeight() {
61 }
62
63 public void Draw(Rect panelRect, SerializedProperty property) {
64 if (_cachedDelegate == null) {
65 Type type = targets[0].GetType();
66
67 MethodInfo method = type.GetMethod(editorMethodName,
68 BindingFlags.Public |
69 BindingFlags.NonPublic |
70 BindingFlags.Static |
71 BindingFlags.Instance |
72 BindingFlags.FlattenHierarchy
73 );
74
75 if (method == null) {
76 Debug.LogWarning("Could not find method of the name " +
77 editorMethodName + " " + "to invoke for the TopButtonPanel " +
78 "attribute.");
79 return;
80 }
81
82 int paramCount = method.GetParameters().Length;
83 if (paramCount == 0) {
84 Debug.LogWarning("Method " + editorMethodName + "needs to accept a " +
85 "Rect arg and Object[] arg to know the size of the panel to draw and " +
86 "which components are currently selected.");
87 }
88 // else if (paramCount == 1) {
89 // Debug.LogWarning("Method " + editorMethodName + "needs to accept a " +
90 // "Rect arg and Object[] arg to know the size of the panel to draw and " +
91 // "which components are currently selected.");
92 // }
93 else if (paramCount == 1 || paramCount == 2) {
94 _cachedDelegate = (rect, targets) => {
95 if (!method.IsStatic) {
96 // Non-static drawing is only valid for single-object selection.
97 if (targets.Length == 1) {
98 object[] argArray = new object[1];
99 argArray[0] = rect;
100 method.Invoke(targets[0], argArray);
101 }
102 }
103 else { // method.IsStatic
104 object[] argArray = new object[2];
105 argArray[0] = rect;
106 argArray[1] = targets;
107 method.Invoke(null, argArray);
108 }
109 };
110 } else {
111 Debug.LogWarning("Could not invoke the method " + editorMethodName +
112 " from TopButtonPanel because the method had more than 1 argument.");
113 }
114 }
115
116 _cachedDelegate.Invoke(panelRect, targets);
117
118 return;
119 }
120
121#endif
122 }
123
124}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
EditorGUIPanelAttribute(string editorMethodName, int heightInLines=1)
Pass the name of a static method in your MonoBehaviour that accepts a Rect and Object[] targets,...