-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLayerAttribute.cs
More file actions
60 lines (48 loc) · 1.77 KB
/
LayerAttribute.cs
File metadata and controls
60 lines (48 loc) · 1.77 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
using System;
using System.Diagnostics;
using UnityEngine;
using Debug = UnityEngine.Debug;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditorInternal;
#endif
namespace Jackey.Utilities.Attributes {
/// <summary>
/// Displays a LayerField in the inspector
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
[Conditional("UNITY_EDITOR")]
public sealed class LayerAttribute : PropertyAttribute { }
#if UNITY_EDITOR
namespace PropertyDrawers {
[CustomPropertyDrawer(typeof(LayerAttribute))]
public class LayerPropertyDrawer : PropertyDrawer {
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
if (property.propertyType != SerializedPropertyType.Integer && property.propertyType != SerializedPropertyType.String) {
Debug.LogWarning("The Layer attribute only supports application on int and string fields");
return;
}
label = EditorGUI.BeginProperty(position, label, property);
position = EditorGUI.PrefixLabel(position, label);
string layerName = property.propertyType == SerializedPropertyType.Integer
? LayerMask.LayerToName(property.intValue)
: property.stringValue;
int layer = property.propertyType == SerializedPropertyType.Integer
? property.intValue
: LayerMask.NameToLayer(layerName);
if (Array.IndexOf(InternalEditorUtility.layers, layerName) == -1)
GUI.backgroundColor = Color.yellow;
EditorGUI.BeginChangeCheck();
int layerFieldValue = EditorGUI.LayerField(position, layer);
if (EditorGUI.EndChangeCheck()) {
if (property.propertyType == SerializedPropertyType.Integer)
property.intValue = layerFieldValue;
else
property.stringValue = LayerMask.LayerToName(layerFieldValue);
}
EditorGUI.EndProperty();
}
}
}
#endif
}