MaskLayer - BluePixelDev/utilkit GitHub Wiki

MaskLayer is a serializable struct that simplifies selecting a single Unity layer in the Inspector.

Overview

Unity's LayerMask supports multiple selections, but sometimes only one layer should be selectable. MaskLayer enforces a single layer and integrates with the Unity Editor for clean selection.

Features

  • Single layer selection
  • Unity Editor integration with custom property drawer
  • Implicitly returns layer index and bitmask

Code Example

[SerializeField] private MaskLayer maskLayer;

int layerIndex = maskLayer.layerIndex;
int layerMask = maskLayer.LayerMask;

Members

Fields

  • private int _layerIndex — stores the selected layer index.

Properties

  • int layerIndex — returns the selected layer index.
  • int LayerMask — returns 1 << layerIndex.

Methods

  • void SetLayer(int layer) — sets the layer if the index is between 1 and 31.
  • MaskLayer(int layer) — constructor with validation.

Editor Integration

A CustomPropertyDrawer is provided for Unity Editor that renders a dropdown layer field in the inspector.