I have a project I’m working on where I want the screen to be black until a button is pressed. I’ve tried disabling the camera, but that leaves the screen gray. I’ve also tried blocking the camera with a plane, but that doesn’t really work. Is there a good way to make the camera go from black to normal and vice-versa using script?
Hey,
OK, the easiest way to do this would be with a GUI texture just over the screen, this way you can easily enable and disable it.
The first thing you want to do is make sure that there is a black texture somewhere in your project. If not, just use one of these, or you can easily make one in Paint on Windows.
Once you have imported or found a black texture in your project (it can be any image that is black) create a new script with this in it:
var blackTexture : Texture;
var screenBlackOut = false;
function Update(){
if(Input.GetButtonDown("THEBUTTONTOCHANGE")){
if(screenBlackOut == false){
screenBlackOut = true;
}
else{
screenBlackOut = false;
}
}
}
function OnGUI(){
if(screenBlackOut == true){
GUI.color.a = 1;
}
else{
GUI.color.a = 0;
}
GUI.DrawTexture(Rect(0,0,Screen.width, Screen.height), blackTexture);
}
Then attach that script to any Game Object.
Then just set the variable `screenBlackOut` to true if you want the screen to go black, or false if you want it to be normal!!!!!!!!!!!!!!!!!!!
The way this is working is, the script is constantly drawing the black texture that we specified over the full size of the screen because in the size properties of `GUI.DrawTexture` we specified the width to be `Screen.width` and the height `Screen.height`!!! But we are changing the `alpha` value (`color.`a) which is the transparency. If we set the `alpha` value to 1, then we will see the colour, if we set it to 0, then we won't because it will be fully transparent!!!!!!!!! And we are setting the value depending on whether the `screenBlackOut` variable is enabled or disabled!!!!
All the `Update(`) function is doing, is checking when the button that you want to change from black to normal and vice versa is pressed, then it will just swap the `screenBlackOut` variable to true if it is already false, or false if it's true (basically just swapping around to turn on/off)!!!!!
Hope this helps, and if you need more help, or if my explanation is too confusing, then feel free to comment back!!!!!!!!!! :D
-Grady
The easiest way would be either:
- use a second camera with it’s cullingmask set to “nothing” and the clear color black (or whatever you want)
- change the culling mask of your actual camera to nothing.
The first way is the preferred way since you can keep the culling mask of your actual camera. Just disable your main camera and enable your black-screen-camera.
This will also prevent unnecessary rendering since nothing is drawn in this case.
ps. you could even have both cameras enabled and just disable / enable the main cam. Just make sure your main cam have a greater depth than your black screen cam. Of course, in this case the screen get cleared twice each frame, but it shouldn’t be a problem.