Last active
October 29, 2024 10:02
-
-
Save der-hugo/1b8e7e6af69b6a87183b178305c10cc8 to your computer and use it in GitHub Desktop.
Unity Inspector dropdown for a single layer (as contrary to the LayerMask)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
namespace derHugo | |
{ | |
public class Example : MonoBehaviour | |
{ | |
public LayerMask multiLayers; | |
public Layer singleLayer; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
namespace derHugo | |
{ | |
[Serializable] | |
public struct Layer | |
{ | |
[SerializeField] private int m_Value; | |
public SingleLayer(int value) | |
{ | |
m_Value = value; | |
} | |
public static implicit operator int(SingleLayer layer) | |
{ | |
return layer.m_Value; | |
} | |
public static implicit operator SingleLayer(int value) | |
{ | |
return new SingleLayer(value); | |
} | |
#if UNITY_EDITOR | |
[CustomPropertyDrawer(typeof(Layer))] | |
public class LayerDrawer : PropertyDrawer | |
{ | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
using (new EditorGUI.PropertyScope(position, label, property)) | |
{ | |
position = EditorGUI.PrefixLabel(position, label); | |
var valueProperty = property.FindPropertyRelative(nameof(m_Value)); | |
valueProperty.intValue = EditorGUI.LayerField(position, valueProperty.intValue); | |
} | |
} | |
} | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment