Skip to content

Instantly share code, notes, and snippets.

@hariedo
Created June 5, 2024 14:02
Show Gist options
  • Save hariedo/10fa553a76bb204b0613bd3b258cf23f to your computer and use it in GitHub Desktop.
Save hariedo/10fa553a76bb204b0613bd3b258cf23f to your computer and use it in GitHub Desktop.
// Unity LayerMask is castable to a 32bit int bit field.
// Add some extension methods to make it easier and more readable to
// determine if a given layer or object is within the bits of a mask.
public static bool Contains(this LayerMask mask, int layer)
{
return (0 != (mask & (1 << layer)));
}
public static bool Contains(this LayerMask mask, GameObject gob)
{
if (gob == null)
return false;
return (0 != (mask & (1 << gob.layer)));
}
public static bool Contains(this LayerMask mask, Component component)
{
if (component == null)
return false;
return (0 != (mask & (1 << component.gameObject.layer)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment