Skip to content

Instantly share code, notes, and snippets.

@der-hugo
Created November 15, 2023 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 System;
using UnityEngine;
public class Example : MonoBehaviour
{
[SingleLayer] public int layer;
[ContextMenu(nameof(Test))]
private void Test()
{
Debug.Log(gameObject.layer == layer);
}
}
using System;
using UnityEngine;
#if UNITY_EDITOR
using UnityEngine;
#endif
public class SingleLayerAttribute : PropertyAttribute
{
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(SingleLayerAttribute))]
public class SingleLayerAttributeDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
using (new EditorGUI.PropertyScope(position, label, property))
{
position = EditorGUI.PrefixLabel(position, label);
if (property.propertyType != SerializedPropertyType.Integer)
{
EditorGUI.HelpBox(position, $"{nameof(SingleLayerAttribute)} can only be used on fields of type int!", MessageType.Error);
}
else
{
property.intValue = EditorGUI.LayerField(position, property.intValue);
}
}
}
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment