-
Notifications
You must be signed in to change notification settings - Fork 0
LayerMask Extensions
This utility library, part of the LayerMaskExtensions namespace, provides extension methods for Unity's LayerMask class. These methods enhance the functionality of working with layer masks, including checking for containment, obtaining layer indices, inverting masks, and more.
Checks if the layer mask contains the other layer mask or a specified layer index.
// Example usage
LayerMask myLayerMask = /* Instantiate or reference a LayerMask */;
LayerMask otherLayerMask = /* Instantiate or reference another LayerMask */;
bool contains = myLayerMask.Contains(otherLayerMask);
Debug.Log($"Does the layer mask contain the other layer mask? {contains}");Parameters:
-
layerMask(LayerMask): The layer mask to check. -
otherLayerMask(LayerMask): The other layer mask to check for.
Returns:
- bool: True if the layer mask contains the other layer mask, false otherwise.
// Example usage
LayerMask myLayerMask = /* Instantiate or reference a LayerMask */;
int layerIndex = 5; // Layer index to check for
bool containsIndex = myLayerMask.Contains(layerIndex);
Debug.Log($"Does the layer mask contain layer index {layerIndex}? {containsIndex}");Parameters:
-
layerMask(LayerMask): The layer mask to check. -
layerIndex(int): The layer index to check for.
Returns:
- bool: True if the layer mask contains the layer index, false otherwise.
Gets the layer index of the layer mask.
// Example usage
LayerMask myLayerMask = /* Instantiate or reference a LayerMask */;
int layerIndex = myLayerMask.GetLayerIndex();
Debug.Log($"Layer index of the layer mask: {layerIndex}");Parameters:
-
layerMask(LayerMask): The layer mask to get the layer index from.
Returns:
- int: The layer index of the layer mask, or -1 if an error occurred.
Inverts the layer mask.
// Example usage
LayerMask myLayerMask = /* Instantiate or reference a LayerMask */;
LayerMask invertedMask = myLayerMask.Inverse();
Debug.Log($"Inverted layer mask: {invertedMask}");Parameters:
-
layerMask(LayerMask): The layer mask to invert.
Returns:
- LayerMask: The inverted layer mask.
Adds the specified layers to the layer mask.
// Example usage
LayerMask myLayerMask = /* Instantiate or reference a LayerMask */;
string[] layerNamesToAdd = { "Player", "Enemy" }; // Layers to add
LayerMask updatedMask = myLayerMask.AddToMask(layerNamesToAdd);
Debug.Log($"Layer mask with added layers: {updatedMask}");Parameters:
-
layerMask(LayerMask): The layer mask to add to. -
name(params string[]): The names of the layers to add.
Returns:
- LayerMask: The layer mask with the added layers.
Removes the specified layers from the layer mask.
// Example usage
LayerMask myLayerMask = /* Instantiate or reference a LayerMask */;
string[] layerNamesToRemove = { "Obstacle", "Ground" }; // Layers to remove
LayerMask updatedMask = myLayerMask.RemoveFromMask(layerNamesToRemove);
Debug.Log($"Layer mask with removed layers: {updatedMask}");Parameters:
-
layerMask(LayerMask): The layer mask to remove from. -
name(params string[]): The names of the layers to remove.
Returns:
- LayerMask: The layer mask with the removed layers.
Converts the layer mask to an array of layer names.
// Example usage
LayerMask myLayerMask = /* Instantiate or reference a LayerMask */;
string[] layerNames = myLayerMask.MaskToNames();
Debug.Log($"Layer names in the layer mask: {string.Join(", ", layerNames)}");Parameters:
-
layerMask(LayerMask): The layer mask to convert.
Returns:
- string[]: An array of layer names.
Converts the layer mask to a string.
// Example usage
LayerMask myLayerMask = /* Instantiate or reference a LayerMask */;
string layerMaskString = myLayerMask.MaskToString();
Debug.Log($"String representation of the layer mask: {layerMaskString}");Parameters:
-
layerMask(LayerMask): The layer mask to convert.
Returns:
- string: A string representation of the layer mask.
Converts the layer mask to a string with the specified delimiter.
// Example usage
LayerMask myLayerMask = /* Instantiate or reference a LayerMask */;
string delimiter = "|"; // Delimiter to use
string layerMaskString = myLayerMask.MaskToString(delimiter);
Debug.Log($"String representation of the layer mask with delimiter: {layerMaskString}");Parameters:
-
layerMask(LayerMask): The layer mask to convert. -
delimiter(string): The delimiter to use.
Returns:
- string: A string representation of the layer mask with the specified delimiter.