How do I create a white rectangle in Unity 2D?

14,332

This is an old question, never the less I'm here with 2 solutions for you: (These were coded up in C#)

Solution #1: Is exactly what you have asked for in code format. I'm fairly certain you already solved this issue (but this is for anyone else who stumbles upon this problem).

//bFlashed is a boolean (you can name it what ever you like)
void OnGUI()
{
    if (bFlashed)
    {
        Texture2D tx2DFlash = new Texture2D(1,1); //Creates 2D texture
        tx2DFlash.SetPixel(1,1,Color.white); //Sets the 1 pixel to be white
        tx2DFlash.Apply(); //Applies all the changes made
        GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), tx2DFlash); //Draws the texture for the entire screen (width, height)
        StartCoroutine(SetFlashFalse());
    }
}
IEnumerator SetFlashFalse()
{
    yield return new WaitForSeconds(1); //Waits 1 second before setting boolean to false
    bFlashed = false;
}

Another way is to actually mess about with Unity's lights. This is personally my favorite option because you'll be able to manipulate the location of the light, intensity, range (if using spot/point light), color, and many more options.

For the following solution I attached a simple Light component to the Main Camera.

  • Type: Directional
  • Color: White
  • Intensity: 8
  • Enabled: False

Solution #2: Just call StartCoroutine(CameraFlash()); when you want the flash to occur

IEnumerator CameraFlash() //You can name this however you like
{
    //Wait for 1/4 of a second (maybe you want a small sound to play before screen flashes)
    yield return new WaitForSeconds(0.25f);
    //Gets the light component from Main Camera
    Light cameraFlash = GameObject.FindWithTag("MainCamera").GetComponent<Light>();
    //Enable the cameras Flash
    cameraFlash.enabled = true;

    //This will decrease the intensity every 0.05 of a second by 2
    for (float f = 8; f >= 0; f -= 2)
    {
        cameraFlash.intensity = f; //Intensity takes in a float so you can really change this up nicely
        //Just be sure that it sets to 0.0f at some point (so that there is no more excess light
        yield return new WaitForSeconds(0.05f);
    }

    yield return new WaitForSeconds(2); 
    cameraFlash.enabled = false; //Be sure to disable it again (until you need it)
    cameraFlash.intensity = 8; //And reset the intensity back to 8
}
Share:
14,332
Jordan Schuetz
Author by

Jordan Schuetz

Updated on June 28, 2022

Comments

  • Jordan Schuetz
    Jordan Schuetz almost 2 years

    Hello Stack Overflow community.

    I've just started to use Unity to port my video game to multiple platforms. I had a question about creating objects programmatically in Unity. This is what it my game looks like currently:

    enter image description here

    When the user taps the camera button, the camera picture scales bigger onTap and offTap. I want the entire screen to flash white for only a brief second but I have no idea how to do this. Here is the C# code I have already for this question:

    using UnityEngine;
    using System.Collections;
    
    public class question3 : MonoBehaviour {
        int cameraTaps = 0;
        // Use this for initialization
        void Start () {
    
        }
    
        IEnumerator CameraCoroutine() {
            Debug.Log("Before Waiting 3 seconds");
            yield return new WaitForSeconds(3);
            Debug.Log("After Waiting 3 Seconds");
            Application.LoadLevel("question4");
        }
        // Update is called once per frame
        void Update () {
            if (Input.GetMouseButtonDown(0)) 
            {
                RaycastHit hit;
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    
                if (Physics.Raycast(ray, out hit))
                {
                    if (hit.collider.gameObject.name == "camera")
                    {
                        var camera = (hit.collider.gameObject);
                        camera.transform.localScale += new Vector3(.1f, .1f, 0);
                    }
                }
            }
            if (Input.GetMouseButtonUp(0)) 
            {
                RaycastHit hit;
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    
                if (Physics.Raycast(ray, out hit))
                {
                    if (hit.collider.gameObject.name == "camera")
                    {
                        var camera = (hit.collider.gameObject);
                        camera.transform.localScale -= new Vector3(.1f, .1f, 0);
                        cameraTaps = cameraTaps + 1;
                        print (cameraTaps);
                        if (cameraTaps == 5)
                        {
                            StartCoroutine(CameraCoroutine());
    
                        }
                        if (cameraTaps > 5)
                        {
                            Application.LoadLevel("fail");
                        }
    
                    }
                    if (hit.collider.gameObject.name == "turtle")
                    {
    
                    }
                }
            }
        }
    }
    

    Any help would be much appreciated. I really have no idea how to either insert PNG's or create a rectangle that will overlay for a brief second.