Set Interaction LayerMask by name

Is there a way to set the Interaction Layer Mask by name? Or just get a layers int value by inputting the string to a method similar to Unity’s built in LayerMask.NameToLayer and LayerMask.LayerToName?

Would also be great to have some convenience methods like AddLayer, RemoveLayer, and SetLayer and SetLayers

I’ve always hated doing the bitwise operations for layers, and considering interaction layer changing during runtime has been suggested as the solution to so many things I’m trying to do, these methods would be extremely helpful ( I have written my own extensions to do this but they are proving unreliable)

Hello again @edwon ,
We do have at least 2 of the main methods you are looking for: InteractionLayerMask.LayerToName(int layer) and InteractionLayerMask.NameToLayer(string layerName). You can find the documentation here.

Adding more convenience methods is a good idea. We’ll add it to our backlog and look into it for a future release.

For anyone who might need it, these are all the extension methods I made for dealing with InteractionLayers
the “SetLayers” method is incredibly hacky, but does the job when you want to set multiple layers

public static bool Contains(this InteractionLayerMask layerMask, int layer)
    {
        if(((1<<layer) & layerMask) != 0)
            return true;
        else
            return false;
    }

    public static InteractionLayerMask SetLayer(this InteractionLayerMask layerMask, int layer)
    {
        layerMask = (1 << layer);
        return layerMask;
    }

    public static InteractionLayerMask SetLayers(this InteractionLayerMask layerMask, int[] layers)
    {
        if (layers.Length == 1)
        {
            layerMask = (1 << layers[0]);
        }
        if (layers.Length == 2)
        {
            layerMask = (1 << layers[0]) | (1 << layers[1]);
        }
        if (layers.Length == 3)
        {
            layerMask = (1 << layers[0]) | (1 << layers[1]) | (1 << layers[2]);
        }
        if (layers.Length == 4)
        {
            layerMask = (1 << layers[0]) | (1 << layers[1]) | (1 << layers[2]) | (1 << layers[3]);
        }
        if (layers.Length == 5)
        {
            layerMask = (1 << layers[0]) | (1 << layers[1]) | (1 << layers[2]) | (1 << layers[3]) | (1 << layers[4]);
        }
        if (layers.Length == 6)
        {
            layerMask = (1 << layers[0]) | (1 << layers[1]) | (1 << layers[2]) | (1 << layers[3]) | (1 << layers[4]) | (1 << layers[5]);
        }
        if (layers.Length > 6)
        {
            Debug.LogError("Too many layers, need to add thsis amount to the SetLayers method");
        }
        return layerMask;
    }

    public static InteractionLayerMask RemoveLayer(this InteractionLayerMask layerMask, int layerToRemove)
    {
        layerMask &= ~(1 << layerToRemove);
        return layerMask;
    }

    public static InteractionLayerMask AddLayer(this InteractionLayerMask layerMask, int layerToAdd)
    {
        layerMask |= (1 << layerToAdd);
        return layerMask;
    }
1 Like

@edwon ,
Those are great extension methods. I think the SetLayer function could probably be simplified with a for loop, no?

    public static InteractionLayerMask SetLayers(this InteractionLayerMask layerMask, int[] layers)
    {
        layerMask = 0;
        for (int i = 0; i < layers.Length ; ++i)
        {
            layerMask = AddLayer(layerMask, layers[i]);
        }
        return layerMask;
    }

Or something to that effect. I haven’t tested that this works as expected, but it would allow for arbitrary sizes. You may want to limit the layers.Length so it is not greater than 32 (layer mask size).

@VRDave_Unity please add those extension methods into the package itself, would save some other poor soul the time I spent doing this

1 Like