Tanoda
LeapPreferences.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;
10using System.Linq;
11using System.Reflection;
12using System.Collections.Generic;
13#if UNITY_EDITOR
14using UnityEditor;
15#endif
16
17namespace Leap.Unity {
18
31 [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
32 public class LeapPreferences : Attribute {
33 public readonly string header;
34 public readonly int order;
35
36 public LeapPreferences(string header, int order) {
37 this.header = header;
38 this.order = order;
39 }
40
41#if UNITY_EDITOR
42 private static List<LeapPreferenceItem> _leapPreferenceItems = null;
43
44 private struct LeapPreferenceItem {
45 public Action drawPreferenceGui;
46 public LeapPreferences attribute;
47 }
48
49 private static void ensurePreferenceItemsLoaded() {
50 if (_leapPreferenceItems != null) {
51 return;
52 }
53
54 _leapPreferenceItems = new List<LeapPreferenceItem>();
55
56 var assemblies = AppDomain.CurrentDomain.GetAssemblies();
57 foreach (var type in assemblies.SelectMany(a => a.GetTypes())) {
58 foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)) {
59 var attributes = method.GetCustomAttributes(typeof(LeapPreferences), inherit: true);
60 if (attributes.Length == 0) {
61 continue;
62 }
63
64 var attribute = attributes[0] as LeapPreferences;
65 _leapPreferenceItems.Add(new LeapPreferenceItem() {
66 drawPreferenceGui = () => {
67 EditorGUILayout.LabelField(attribute.header, EditorStyles.boldLabel);
68 using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox)) {
69 method.Invoke(null, null);
70 }
71 EditorGUILayout.Space();
72 EditorGUILayout.Space();
73 EditorGUILayout.Space();
74 },
75 attribute = attribute
76 });
77 }
78 }
79
80 _leapPreferenceItems.Sort((a, b) => a.attribute.order.CompareTo(b.attribute.order));
81 }
82
83 #if UNITY_2018_3_OR_NEWER
84 // Implementations Leap Motion settings using the new SettingsProvider API.
85 private class LeapMotionSettingsProvider : SettingsProvider {
86 public LeapMotionSettingsProvider(string path, SettingsScope scopes = SettingsScope.User)
87 : base(path, scopes)
88 { }
89
90 public override void OnGUI(string searchContext) {
91 DrawPreferencesGUI();
92 }
93 }
94
95 [SettingsProvider]
96 static SettingsProvider GetSettingsProvider()
97 {
98 return new LeapMotionSettingsProvider("Preferences/Leap Motion");
99 }
100#else
101 [PreferenceItem("Leap Motion")]
102#endif
103 public static void DrawPreferencesGUI() {
104 ensurePreferenceItemsLoaded();
105
106 foreach (var item in _leapPreferenceItems) {
107 item.drawPreferenceGui();
108 }
109 }
110 #endif
111 }
112}
This attribute is used to add items to the Leap Motion preferences window. This allows each module to...
LeapPreferences(string header, int order)