- Home /
How to change if layer is rendered by camera in script?
I need to know is there is a script that allows me to change the layers rendered because I need it for my start screen so if you click on options(for example) it will make all the other parts of the menu not render.
You can change the Camera.culling$$anonymous$$ask at runtime.
Can I change it to something like.
void On$$anonymous$$ouseButtonUp()
{
//Already defined variables needed
if(isOptions = true)
{
Camera.culling$$anonymous$$ask//what now?
}
}
Answer by ncallaway · Jan 11, 2014 at 08:19 AM
void OnMouseButtonUp()
{
if (isOptions == true) // Make sure to use "==" instead of "=" here!
{
Camera.main.cullingMask |= (1 << LayerMask.NameToLayer("TheNameOfYourLayer");
}
}
A brief explanation of this: Camera.cullingMask is a bitfield that represents which layers the camera will draw. That means if bit 0 is set, the camera will render layer 0. If bit 1 is set the camera will render layer 1, and so on.
The |= operator is a compound assignment operator, so the following two lines are equivalent:
a |= b;
a = a | b;
So, we use the bitshift operator (<<) to create an integer that has a single bit set that represents the layer we want to represent. If we're rendering the 4th layer, that integer would look something like 0b00001000 (I've only included the last 8 bits here, because we don't actually need to see an entire 32 bit binary literal).
By doing a bitwise or with the original culling mask, we are ensuring that the new layer we want is on, while all the other layers will have their original values.
How could I make it so a layer doesn't render? Could I use !! or whatever operator means don't because I need to turn off one layer after making this one active.
To turn off a specific layer you can use a similar technique using the &= operator. The basic idea is that you will create an integer with only the bit you want to turn off as a "0".
Then a bitwise and with the original value will turn off that bit, while leaving all the other bits in their original state.
You can create the desired field by using the complement operator. This inverts each of the bits in the value you give it. The final code looks something like:
Camera.main.culling$$anonymous$$ask &= ~(1 << Layer$$anonymous$$ask.NameToLayer("TheNameOfYourLayer");
You are the biggest help in the world man. Thanks! You should $$anonymous$$ch this stuff! You explain it perfectly.
Wait one more thing. It says an object reference is needed to use camera.cullingmask. What do I need to do