There is a much simpler solution. Don’t create a custom class to store your value, but rather just create an attribute - then the entire process becomes WAY simpler than @Kyle_WG 's answer.
First: you need just an empty Attribute class.
/// <summary>
/// Attribute to select a single layer.
/// </summary>
public class LayerAttribute : PropertyAttribute
{
// NOTHING - just oxygen.
}
And the PropertyDrawer (which becomes an editor for the attribute, instead of some excessive class to store value that shouldn’t need one.
This should go into your “Editor” scripts folder - or if you write assemblies instead - into your Whatever.Editor.dll assembly.
[CustomPropertyDrawer(typeof(LayerAttribute))]
class LayerAttributeEditor : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// One line of oxygen free code.
property.intValue = EditorGUI.LayerField(position, label, property.intValue);
}
}
So all you need to do to use it is the following (See Layer Attribute):
[SerializeField, Layer] int m_EditorLayer = 31;
And voila, you have a layer selection without gnarly code: