- Home /
Best way to set a GameObject layer programmatically?
In my game I have an ArmamentController, a Monobehaviour that attaches to Actors (players or enemies). This controller has a public LayerMask variable so that I can set the layer for the projectiles (for the collision matrix; friendly bullets don't collide with the player, enemy bullets don't collide with enemies). The really nice thing about this is that in the editor, I get the list of layers (as opposed to stringly typing, which sucks).
This, unfortunately, fails horribly with GameObject.layer, as the LayerMask is an object while GameObject.layer is an int. LayerMask provides the value
getter, but that actually returns the bitmask, not the integer of the layer. There doesn't seem to be a function that does this (and I'm not sure why - why not add it as a property to the LayerMask?)
I could get the integer value, but unfortunately the only way to inverse a bitshift function is to use a logarithm, and those are pretty expensive....so if there are a lot of shots, that's something you don't want, right?
Is there another Unity object type besides LayerMask? Layer, perhaps? Or something like that? Or am I forced to use the logarithm method (or some wacky loop or something) or resort to strings? Is there perhaps a built-in enumerator for this, or can I access the Layer list directly...?
Edit: I tried using the ToString()
method of my LayerMask variable, hoping that behind the scenes it was using the [Enum]
tag somewhere (like this question) but alas, that idea didn't work either.
Answer by Meltdown · Jul 31, 2014 at 05:04 AM
gameObject.layer = LayerMask.NameToLayer("YourLayerName");
This requires the string name, which I specifically don't have.
I tried this already and it does not work for me. How do you do it in Unity 5?
That is definitely the way to go. Additionally, I would suggest keeping all of the layers in a static class. Like this: https://www.anton.website/quick-tip-for-constants/
Answer by MakeCodeNow · Jul 31, 2014 at 05:06 AM
Using LayerMask is correct. Just use the bitshift operators . No log required!
Care to elaborate on using bitshift operators ins$$anonymous$$d?
Well, it highly depends on what your layermask represents. If it contains more than 2 layers, which one do you actually want? For example layer 8 and layer 11 could be set in the mask. Which one do you want? When you are sure there's only a single layer set in the mask, you can simply scan through the layers and see if it is set or not. Once you found a set one, you know the index.
This could be even optimised into several distinct steps (essentially performs a binary search) to reduce the number of steps. For example
public static int GetLayerFrom$$anonymous$$ask(Layer$$anonymous$$ask a$$anonymous$$ask)
{
uint val = (uint)a$$anonymous$$ask.value;
if (val == 0)
return -1;
int layer = 0;
if (val > 0xFFFF) // XXXX XXXX XXXX XXXX 0000 0000 0000 0000
{
layer += 16;
val >>= 16;
}
if (val > 0xFF) // XXXX XXXX 0000 0000
{
layer += 8;
val >>= 8;
}
if (val > 0xF) // XXXX 0000
{
layer += 4;
val >>= 4;
}
if (val > 0x3) // XX00
{
layer += 2;
val >>= 2;
}
if (val & 0x2 != 0) // X0
layer += 1;
return layer;
}
Note that this approach would always return the highest layer set in the mask
The more traditional approach would be this
public static int GetLayerFrom$$anonymous$$ask(Layer$$anonymous$$ask a$$anonymous$$ask)
{
uint val = (uint)a$$anonymous$$ask.value;
if (val == 0)
return -1;
for (int i = 0; i < 32; i++)
{
if( (val & (1<<i)) != 0)
return i;
}
return -1;
}
This approach would always return the smallest layer set in the mask, however the worst case (when looking for layer 31) would require 32 iterations. That's not really something you should be concerned about, even when used several hundreds of times per frame. Though the first approach has a fix runtime no matter which layer and requires 5 steps (for a 32 bit layermask). This has a logarithmic scale. If you had a 64 bit layermask, only a single additional step would be required. Though since Unity uses an int we only have 32 bits.
Note: I've written both functions here from scratch, no syntax checking has been performed and of course they aren't tested. Though you can play through every possible layermask in your head to convince yourself that it works ^^. The XXX in the comments indicate the new "active" area in the mask. The 000 are dumped
Just as example:
Assume layer 23 is set
0000 0000 1000 0000 0000 0000 0000 0000
// since the number is greater than 65536 we strip the lower 16 bits and layer is set to "16"
0000 0000 1000 0000
// the value is not larger than 255 so we skip that part and implicitly strip the first 8 bits
1000 0000
// the value is larger than 15 so we strip the lower 4 bits and add 4 to the layer and get "20"
1000
// the value is larger than 3 so we strip the lower 2 bits and add 2 --> layer == 22
10
// bit 1 is set (value 2) so add another one to layer and end up at 23
For the layer 22 it would be exactly the same steps except we don't add 1 in the last step since the bits would be "01"
Your answer
Follow this Question
Related Questions
Is it possible to allow a raycast to pass through a collider to hit things behind it? 6 Answers
Return Objects hit by raycast, check against a array of all objects? 1 Answer
some quesitions about layermask, culling mask, raycast. 0 Answers
Get LayerMask that current GameObject collides with? 1 Answer
Ignore one layermask question 1 Answer