Tanoda
SingleLayerEditor.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 UnityEngine;
10using UnityEditor;
11using System.Linq;
12using System.Collections.Generic;
13
14namespace Leap.Unity {
15
16 [CustomPropertyDrawer(typeof(SingleLayer))]
17 public class SingleLayerEditor : PropertyDrawer {
18 private GUIContent[] _layerNames;
19 private List<int> _layerValues;
20
21 public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
22 ensureLayersInitialized();
23
24 SerializedProperty layerProperty = property.FindPropertyRelative("layerIndex");
25 if (layerProperty == null) {
26 Debug.LogWarning("Could not find the layer index property, was it renamed or removed?");
27 return;
28 }
29
30 int index = _layerValues.IndexOf(layerProperty.intValue);
31 if (index < 0) {
32 if (Application.isPlaying) {
33 //If application is playing we dont want to change the layers on the fly
34 //Instead, just display them as an int value
35 layerProperty.intValue = EditorGUI.IntField(position, property.displayName, layerProperty.intValue);
36 return;
37 } else {
38 //If the application is not running, reset the layer to the default layer
39 layerProperty.intValue = 0;
40 index = 0;
41 }
42 }
43
44 var tooltipAttribute = fieldInfo.GetCustomAttributes(typeof(TooltipAttribute), true).
45 Cast<TooltipAttribute>().
46 FirstOrDefault();
47
48 if (tooltipAttribute != null) {
49 label.tooltip = tooltipAttribute.tooltip;
50 }
51
52 bool originalMixedValue = EditorGUI.showMixedValue;
53 if (layerProperty.hasMultipleDifferentValues) {
54 EditorGUI.showMixedValue = true;
55 }
56
57 EditorGUI.BeginChangeCheck();
58 index = EditorGUI.Popup(position, label, index, _layerNames);
59 if (EditorGUI.EndChangeCheck()) {
60 layerProperty.intValue = _layerValues[index];
61 }
62
63 EditorGUI.showMixedValue = originalMixedValue;
64 }
65
66 private void ensureLayersInitialized() {
67 if (_layerNames == null) {
68 Dictionary<int, GUIContent> valueToLayer = new Dictionary<int, GUIContent>();
69 for (int i = 0; i < 32; i++) {
70 string layerName = LayerMask.LayerToName(i);
71 if (!string.IsNullOrEmpty(layerName)) {
72 valueToLayer[i] = new GUIContent(layerName);
73 }
74 }
75
76 _layerValues = valueToLayer.Keys.ToList();
77 _layerNames = valueToLayer.Values.ToArray();
78 }
79 }
80 }
81}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
override void OnGUI(Rect position, SerializedProperty property, GUIContent label)