Created
August 21, 2024 17:58
-
-
Save ryan-at-melcher/5b2a2b9f4bf76c95692ab1a7290c20da to your computer and use it in GitHub Desktop.
A simple attribute for Unity to display an int field in the inspector as a GameObject layer dropdown that is limited to one selection.
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 UnityEngine; | |
/// <summary> | |
/// Allows a single GameObject layer to be selected from the inspector when applied to an | |
/// <see langword="int" /> field. Intended to be used when a <see cref="LayerMask" />'s | |
/// multiselection is not suitable. | |
/// </summary> | |
public class LayerFieldAttribute : PropertyAttribute | |
{ | |
} |
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 UnityEngine; | |
using UnityEditor; | |
[CustomPropertyDrawer(typeof(LayerFieldAttribute))] | |
public class LayerFieldAttributeDrawer : PropertyDrawer | |
{ | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
using (new EditorGUI.PropertyScope(position, label, property)) | |
{ | |
property.intValue = EditorGUI.LayerField(position, label, property.intValue); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So simple. Yet, so robust.