Skip to content

Instantly share code, notes, and snippets.

@der-hugo
Last active October 29, 2024 10:02
Show Gist options
  • Save der-hugo/1b8e7e6af69b6a87183b178305c10cc8 to your computer and use it in GitHub Desktop.
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)
using UnityEngine;
namespace derHugo
{
public class Example : MonoBehaviour
{
public LayerMask multiLayers;
public Layer singleLayer;
}
}
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