// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using UnityEngine;
namespace Microsoft.MixedReality.Toolkit
{
///
/// Extension methods for Unity's LayerMask struct
///
public static class LayerExtensions
{
///
/// The Invalid Layer Id.
///
public const int InvalidLayerId = -1;
///
/// Look through the layerMaskList and find the index in that list for which the supplied layer is part of
///
/// Layer to search for
/// List of LayerMasks to search
/// LayerMaskList index, or -1 for not found
public static int FindLayerListIndex(this int layer, LayerMask[] layerMasks)
{
for (int j = 0; j < layerMasks.Length; j++)
{
if (layer.IsInLayerMask(layerMasks[j]))
{
return j;
}
}
return -1;
}
///
/// Checks whether a layer is in a layer mask
///
/// True if the layer mask contains the layer
public static bool IsInLayerMask(this int layer, int layerMask)
{
return ((1 << layer) & layerMask) != 0;
}
///
/// Combines provided layers into a single layer mask.
///
/// The combined layer mask
public static int Combine(this LayerMask[] layerMaskList)
{
int combinedLayerMask = 0;
for (int i = 0; i < layerMaskList.Length; i++)
{
combinedLayerMask |= layerMaskList[i].value;
}
return combinedLayerMask;
}
///
/// Transform layer id to LayerMask
///
public static LayerMask ToMask(int layerId)
{
return 1 << layerId;
}
///
/// Gets a valid layer id using the layer name.
///
/// The cached layer id.
/// The name of the layer to look for if the cache is unset.
/// The layer id.
public static int GetLayerId(ref int cache, string layerName)
{
if (cache == InvalidLayerId)
{
cache = LayerMask.NameToLayer(layerName);
}
return cache;
}
}
}