Solid color flash entire screen

What's the best way to flash a solid white (or other) color on the entire screen?

Draw a GUITexture over the entire Screen.

  1. Create a GUITexture.

  2. Create a 1x1 white square texture and apply it to the GUITexture.

  3. Change the color to whatever you want.

  4. 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.

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.