Skip to content

Instantly share code, notes, and snippets.

@giacomelli
Created January 23, 2021 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giacomelli/0b67968e915cdb1ffd2ae1c2000f5bad to your computer and use it in GitHub Desktop.
Save giacomelli/0b67968e915cdb1ffd2ae1c2000f5bad to your computer and use it in GitHub Desktop.
LayerMaskExtensions
using System.Collections.Generic;
using UnityEngine;
namespace Giacomelli.Framework
{
public static class LayerMaskExtensions
{
public static bool HasLayer(this LayerMask mask, int layer) => mask == (mask | (1 << layer));
public static IEnumerable<string> ToNames(this LayerMask mask)
{
// Unity generates 32 layers.
// https://docs.unity3d.com/ScriptReference/GameObject-layer.html
for (int i = 0; i < 32; i++)
{
if (mask.HasLayer(i))
yield return LayerMask.LayerToName(i);
}
}
public static string JoinNames(this LayerMask mask, string separator = ", ")
{
return string.Join(separator, mask.ToNames());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment