12using System.Collections.Generic;
16 [CustomPropertyDrawer(typeof(SingleLayer))]
18 private GUIContent[] _layerNames;
19 private List<int> _layerValues;
21 public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
22 ensureLayersInitialized();
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?");
30 int index = _layerValues.IndexOf(layerProperty.intValue);
32 if (Application.isPlaying) {
35 layerProperty.intValue = EditorGUI.IntField(position, property.displayName, layerProperty.intValue);
39 layerProperty.intValue = 0;
44 var tooltipAttribute = fieldInfo.GetCustomAttributes(typeof(TooltipAttribute),
true).
45 Cast<TooltipAttribute>().
48 if (tooltipAttribute !=
null) {
49 label.tooltip = tooltipAttribute.tooltip;
52 bool originalMixedValue = EditorGUI.showMixedValue;
53 if (layerProperty.hasMultipleDifferentValues) {
54 EditorGUI.showMixedValue =
true;
57 EditorGUI.BeginChangeCheck();
58 index = EditorGUI.Popup(position, label, index, _layerNames);
59 if (EditorGUI.EndChangeCheck()) {
60 layerProperty.intValue = _layerValues[index];
63 EditorGUI.showMixedValue = originalMixedValue;
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);
76 _layerValues = valueToLayer.Keys.ToList();
77 _layerNames = valueToLayer.Values.ToArray();
override void OnGUI(Rect position, SerializedProperty property, GUIContent label)