Skip to content

Instantly share code, notes, and snippets.

@townofdon
Created January 30, 2022 00:08
Show Gist options
  • Save townofdon/97b08565e97d5ba6e1086c77f4b8c62f to your computer and use it in GitHub Desktop.
Save townofdon/97b08565e97d5ba6e1086c77f4b8c62f to your computer and use it in GitHub Desktop.
Unity Layer Utility
using System.Collections.Generic;
using UnityEngine;
/**
* LAYER UTIL - ONE SCRIPT TO HOUSE EVERY LAYER MASK, ETC.
*
* USAGE:
*
* - Add all of your layers to the ULayerType enum
* - Update static getters to include all of your layers
*
* ```
* ULayer.init(); // initialize the ULayer in Awake() or Start()
* ULayer.Water.mask(); // get mask for Water layer
* ULayer.Ground.value(); // get layer number for Ground layer
* ```
*
* PROS:
*
* - one-time setup
* - update single file when adding/updating layers
* - warns if a layer does not exist
* - allows for intellisense autosuggest for layers (e.g. start typing "Layer.Gr" to get the ground layer)
*
* CONS:
*
* - must initialize upon app start
*/
// -- ADD YOUR LAYERS HERE --
enum ULayerType {
Ground,
Water,
UI,
}
public static class ULayer
{
// -- ADD YOUR LAYERS HERE --
public static ULayerMaskItem Ground => layerMaskItems[ULayerType.Ground.ToString()];
public static ULayerMaskItem Water => layerMaskItems[ULayerType.Water.ToString()];
public static ULayerMaskItem UI => layerMaskItems[ULayerType.UI.ToString()];
static Dictionary<string, ULayerMaskItem> layerMaskItems = new Dictionary<string, ULayerMaskItem>();
static bool initialized = false;
/// <summary>Initialize layers (call in Awake or Start)</summary>
public static void Init()
{
if (initialized) return;
foreach(string name in System.Enum.GetNames(typeof(ULayerType))) {
layerMaskItems.Add(name, new ULayerMaskItem(name) );
}
initialized = true;
}
}
public struct ULayerMaskItem {
public ULayerMaskItem(string layerName) {
_name = layerName;
_mask = LayerMask.GetMask(layerName);
if (_mask == 0 && layerName != "Default") Debug.LogWarning("Warning: layer \"" + layerName + "\" may not exist");
}
string _name;
int _mask;
public string name => _name;
public int mask => _mask;
public int value => ULayerUtils.ToLayer(_mask);
public bool ContainsLayer(int layer) { return ULayerUtils.LayerMaskContainsLayer(_mask, layer); }
public override string ToString() { return _name + " | " + value + " | " + _mask; }
}
public static class ULayerUtils {
// check to see whether a LayerMask contains a layer
// see: https://answers.unity.com/questions/50279/check-if-layer-is-in-layermask.html
public static bool LayerMaskContainsLayer(int mask, int layer) {
bool contains = ((mask & (1 << layer)) != 0);
return contains;
}
// get the layer num from a layermask
// see: https://forum.unity.com/threads/get-the-layernumber-from-a-layermask.114553/#post-3021162
public static int ToLayer(int layerMask) {
int result = layerMask > 0 ? 0 : 31;
while( layerMask > 1 ) {
layerMask = layerMask >> 1;
result++;
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment