Skip to content

Instantly share code, notes, and snippets.

@samloeschen
Last active August 25, 2019 12:48
Show Gist options
  • Save samloeschen/93e81d4a26f6909d01e2402b00127de1 to your computer and use it in GitHub Desktop.
Save samloeschen/93e81d4a26f6909d01e2402b00127de1 to your computer and use it in GitHub Desktop.
LayerMask Extensions
using UnityEngine;
using System.Collections.Generic;
public static class LayerMaskExtensions {
// int extensions
public static int Inverse(this int mask) {
return ~mask;
}
public static int Combined(this int mask, int other) {
return mask | other;
}
public static bool Includes(this int mask, int value) {
return ((1 << value) & mask) != 0;
}
public static bool Includes(this int mask, string layerName) {
return mask.Includes(LayerMask.NameToLayer(layerName));
}
public static bool IncludesAll(this int mask, IEnumerable<int> values) {
foreach (var layer in values) {
if (!mask.Includes(layer)) return false;
}
return true;
}
public static bool IncludesAny(this int mask, IEnumerable<int> values) {
foreach (var layer in values) {
if (mask.Includes(layer)) return true;
}
return false;
}
// LayerMask extensions
public static LayerMask Inverse(this LayerMask layerMask) {
return ~layerMask;
}
public static LayerMask Combined(this LayerMask layerMask, LayerMask other) {
return layerMask | other;
}
public static bool Includes(this LayerMask layerMask, int value) {
return layerMask.value.Includes(value);
}
public static bool Includes(this LayerMask layerMask, string layerName) {
return layerMask.value.Includes(layerName);
}
public static bool IncludesAll(this LayerMask layerMask, IEnumerable<int> values) {
return layerMask.value.IncludesAll(values);
}
public static bool IncludesAny (this LayerMask layerMask, IEnumerable<int> values) {
return layerMask.value.IncludesAny(values);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment