LeapPreferences.cs
3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/******************************************************************************
* Copyright (C) Leap Motion, Inc. 2011-2017. *
* Leap Motion proprietary and confidential. *
* *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
******************************************************************************/
using System;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Leap.Unity {
/// <summary>
/// This attribute is used to add items to the Leap Motion preferences window.
/// This allows each module to define their own preferences and still have them
/// all show up under the same window.
///
/// The usage is very similar to the built-in PreferenceItem attribute. You
/// add the attribute onto a static method that should be run whenever the
/// preference window is visited. This method is a gui method and should use
/// GuiLayout and EditorGuiLayout in order to draw the preferences. You can
/// specify the name of the preferences as well as an order value to specify
/// how the preferences are ordered relative to other preferences.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class LeapPreferences : Attribute {
public readonly string header;
public readonly int order;
public LeapPreferences(string header, int order) {
this.header = header;
this.order = order;
}
#if UNITY_EDITOR
private static List<LeapPreferenceItem> _leapPreferenceItems = null;
private struct LeapPreferenceItem {
public Action drawPreferenceGui;
public LeapPreferences attribute;
}
private static void ensurePreferenceItemsLoaded() {
if (_leapPreferenceItems != null) {
return;
}
_leapPreferenceItems = new List<LeapPreferenceItem>();
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var type in assemblies.SelectMany(a => a.GetTypes())) {
foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)) {
var attributes = method.GetCustomAttributes(typeof(LeapPreferences), inherit: true);
if (attributes.Length == 0) {
continue;
}
var attribute = attributes[0] as LeapPreferences;
_leapPreferenceItems.Add(new LeapPreferenceItem() {
drawPreferenceGui = () => {
EditorGUILayout.LabelField(attribute.header, EditorStyles.boldLabel);
using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox)) {
method.Invoke(null, null);
}
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.Space();
},
attribute = attribute
});
}
}
_leapPreferenceItems.Sort((a, b) => a.attribute.order.CompareTo(b.attribute.order));
}
[PreferenceItem("Leap Motion")]
private static void preferenceMenu() {
ensurePreferenceItemsLoaded();
foreach (var item in _leapPreferenceItems) {
item.drawPreferenceGui();
}
}
#endif
}
}