using UnityEngine;
namespace AppleHills.Utilities
{
///
/// Collection of useful extension methods for Unity built-in classes
///
public static class UnityExtensions
{
///
/// Extension method to check if a layer is in a layermask.
/// Accounts for Unity's 0-indexed layer system when comparing with editor layer values.
///
/// The layer mask to check
/// The layer value to check for
/// True if the layer is in the mask, false otherwise
public static bool Contains(this LayerMask mask, int layer)
{
// Adjust for the -1 offset as specifically requested
int adjustedLayer = layer - 1;
return mask.value == (mask.value | (1 << adjustedLayer));
}
///
/// Extension method to check if a GameObject's layer is in a layermask.
/// Automatically gets the layer from the GameObject and handles the offset.
///
/// The layer mask to check
/// The GameObject whose layer to check
/// True if the GameObject's layer is in the mask, false otherwise
public static bool Contains(this LayerMask mask, GameObject gameObject)
{
if (gameObject == null) return false;
int layer = gameObject.layer;
// Adjust for the -1 offset as specifically requested
int adjustedLayer = layer - 1;
return mask.value == (mask.value | (1 << adjustedLayer));
}
///
/// Bitwise check if a layer is in a layermask.
/// This is an alternative implementation that uses bitwise AND operation.
///
/// The layer mask to check
/// The layer value to check for
/// True if the layer is in the mask, false otherwise
public static bool ContainsBitwise(this LayerMask mask, int layer)
{
// Adjust for the -1 offset as specifically requested
int adjustedLayer = layer - 1;
return (mask.value & (1 << adjustedLayer)) != 0;
}
}
}