- Home /
Solid color flash entire screen
What's the best way to flash a solid white (or other) color on the entire screen?
Answer by Peter G · May 08, 2011 at 06:50 PM
Draw a GUITexture over the entire Screen.
Create a GUITexture.
Create a 1x1 white square texture and apply it to the GUITexture.
Change the color to whatever you want.
Call the following function from a script on the flash object.
Then add this script:
function Flash (duration : float) { guiTexture.enabled = true; Invoke("Cancel", duration); }
function Cancel () { guiTexture.enabled = false; }
or, you can just put this script on a GameObject, and it will set up a GUITexture that flashes whatever color you want. It creates a GameObject. Adds a GUITexture then flashes it whenever you call Flash(). It is just encapsulating away a few variables that you have above.
private var flash : GUITexture; var flashColor : Color;
function Start () {
var tex : Texture2D = new Texture2D ( 1 , 1 );
tex.SetPixel( 0 , 0 , Color.white );
tex.Apply();
var storageGB = new GameObject("Flash");
storageGB.transform.localScale = new Vector3(0 , 0 , 1);
flash = storageGB.AddComponent(GUITexture);
flash.pixelInset = new Rect(0 , 0 , Screen.width , Screen.height );
flash.color = flashColor;
flash.texture = tex;
flash.enabled = false;
}
function Flash (duration : float) { flash.enabled = true; Invoke("Cancel", duration); }
function Cancel () { flash.enabled = false; }
or you can look at this example I put together that uses properties instead of doing it in Start(). Some people are against a lazy set up, so its your choice.
Just set the scale to (1, 1, 0), and leave the pixel insets at 0. Then you don't need to mess with Screen.width etc. Especially useful if the resolution happens to change.
Nice answer. Could you explain what you mean with a 'lazy setup'?
@Eric, a quick test on my Computer suggests that (2 , 2 , 0 ) takes up the whole screen. ( 1 , 1 , 0 ) only takes up a quarter.
@Joshua Lazy loading would be waiting until an object is needed to load it. http://en.wikipedia.org/wiki/Lazy_loading $$anonymous$$y other answers both provide examples of this.
@Peter G: The coords and scaling are normalized, which means by definition 1.0 is full-screen. $$anonymous$$ake sure the pixel inset values are all 0, and that it's positioned at (.5, .5). I guarantee (1, 1, 0) is full-screen as I've done this a number of times.
Answer by Rabwin · May 08, 2011 at 06:44 PM
I know you're asking for the best way, but as far as I'm aware, it's hard to know which it is. So I guess you could create a guitexture, using a 1pixel image of the colour (yeah it's dodgy but it works) and set the width and height to the resolution of the screen through script. You can then have another script enable and disable the object for a length of time. In c# it might go a little like this...
public GameObject GuiObject;
void start() { //setup the object Rect newSize = new Rect(0, 0, Screen.width, Screen.height); //Pixel Inset - Rect (x, y, width, height) GuiObject.guiTexture.pixelInset = newSize; GuiObject.SetActiveRecursively(false); }
Now you need to just use the SetActiveRecusively function to enable it, wait a while (probably using the in-build wait funciton, or Time.time) then disable the object.
This should make somewhat of a flash on screen.
Ins$$anonymous$$d of using pixel inset values, just set the scale to (1, 1). Then it always covers the entire screen regardless. Also, there's no reason to use SetActiveRecursively; that's for objects with children. Finally, there's nothing "dodgy" about using a 1 pixel image.
Alright, forgive me Eric for I'm still new to unity engine as well. At least you have now set me along the right path.
Your answer
Follow this Question
Related Questions
Screen flashes red on damage? 4 Answers
Disco like GUITexture? 1 Answer
Red flash for each network player when he gets hit 1 Answer
Locking cursor Issue - Flash 1 Answer