using UnityEngine;
using System.Collections.Generic;
namespace RootMotion {
///
/// This class contains tools for working with LayerMasks.
/// Most of this was copied from Unity Wiki: http://wiki.unity3d.com/index.php?title=LayerMaskExtensions.
///
public static class LayerMaskExtensions
{
///
/// Does the LayerMask contain a specific layer index?
///
public static bool Contains(LayerMask mask, int layer) {
return mask == (mask | (1 << layer));
}
///
/// Creates a LayerMask from an array of layer names.
///
public static LayerMask Create(params string[] layerNames)
{
return NamesToMask(layerNames);
}
///
/// Creates a LayerMask from an array of layer indexes.
///
public static LayerMask Create(params int[] layerNumbers)
{
return LayerNumbersToMask(layerNumbers);
}
///
/// Creates a LayerMask from a number of layer names.
///
public static LayerMask NamesToMask(params string[] layerNames)
{
LayerMask ret = (LayerMask)0;
foreach(var name in layerNames)
{
ret |= (1 << LayerMask.NameToLayer(name));
}
return ret;
}
///
/// Creates a LayerMask from a number of layer indexes.
///
public static LayerMask LayerNumbersToMask(params int[] layerNumbers)
{
LayerMask ret = (LayerMask)0;
foreach(var layer in layerNumbers)
{
ret |= (1 << layer);
}
return ret;
}
///
/// Inverts a LayerMask.
///
public static LayerMask Inverse(this LayerMask original)
{
return ~original;
}
///
/// Adds a number of layer names to an existing LayerMask.
///
public static LayerMask AddToMask(this LayerMask original, params string[] layerNames)
{
return original | NamesToMask(layerNames);
}
///
/// Removes a number of layer names from an existing LayerMask.
///
public static LayerMask RemoveFromMask(this LayerMask original, params string[] layerNames)
{
LayerMask invertedOriginal = ~original;
return ~(invertedOriginal | NamesToMask(layerNames));
}
///
/// Returns a string array of layer names from a LayerMask.
///
public static string[] MaskToNames(this LayerMask original)
{
var output = new List();
for (int i = 0; i < 32; ++i)
{
int shifted = 1 << i;
if ((original & shifted) == shifted)
{
string layerName = LayerMask.LayerToName(i);
if (!string.IsNullOrEmpty(layerName))
{
output.Add(layerName);
}
}
}
return output.ToArray();
}
///
/// Returns an array of layer indexes from a LayerMask.
///
public static int[] MaskToNumbers(this LayerMask original)
{
var output = new List();
for (int i = 0; i < 32; ++i)
{
int shifted = 1 << i;
if ((original & shifted) == shifted)
{
output.Add(i);
}
}
return output.ToArray();
}
///
/// Parses a LayerMask to a string.
///
public static string MaskToString(this LayerMask original)
{
return MaskToString(original, ", ");
}
///
/// Parses a LayerMask to a string using the specified delimiter.
///
public static string MaskToString(this LayerMask original, string delimiter)
{
return string.Join(delimiter, MaskToNames(original));
}
}
}