Created
November 15, 2023 09:27
-
-
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 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; | |
public class Example : MonoBehaviour | |
{ | |
[SingleLayer] public int layer; | |
[ContextMenu(nameof(Test))] | |
private void Test() | |
{ | |
Debug.Log(gameObject.layer == layer); | |
} | |
} |
This file contains 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 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