Skip to content

LayerMask Extensions

Andrzej Kebab edited this page Jan 23, 2024 · 1 revision

UtilityLibrary.Unity.Runtime

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.

Contains

Checks if the layer mask contains the other layer mask or a specified layer index.

Contains(LayerMask otherLayerMask)

// 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.

Contains(int layerIndex)

// 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.

GetLayerIndex

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.

Inverse

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.

AddToMask

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.

RemoveFromMask

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.

MaskToNames

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.

MaskToString

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.

MaskToString with Delimiter

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.

Clone this wiki locally