The feel is fine
This commit is contained in:
55
Assets/Scripts/Utilities/UnityExtensions.cs
Normal file
55
Assets/Scripts/Utilities/UnityExtensions.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace AppleHills.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Collection of useful extension methods for Unity built-in classes
|
||||
/// </summary>
|
||||
public static class UnityExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="mask">The layer mask to check</param>
|
||||
/// <param name="layer">The layer value to check for</param>
|
||||
/// <returns>True if the layer is in the mask, false otherwise</returns>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extension method to check if a GameObject's layer is in a layermask.
|
||||
/// Automatically gets the layer from the GameObject and handles the offset.
|
||||
/// </summary>
|
||||
/// <param name="mask">The layer mask to check</param>
|
||||
/// <param name="gameObject">The GameObject whose layer to check</param>
|
||||
/// <returns>True if the GameObject's layer is in the mask, false otherwise</returns>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Bitwise check if a layer is in a layermask.
|
||||
/// This is an alternative implementation that uses bitwise AND operation.
|
||||
/// </summary>
|
||||
/// <param name="mask">The layer mask to check</param>
|
||||
/// <param name="layer">The layer value to check for</param>
|
||||
/// <returns>True if the layer is in the mask, false otherwise</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Utilities/UnityExtensions.cs.meta
Normal file
3
Assets/Scripts/Utilities/UnityExtensions.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3e92a12c248d4b9ab06fcc0ba9c63ef3
|
||||
timeCreated: 1758717247
|
||||
Reference in New Issue
Block a user