如何在Unity 2D中创建白色矩形?

时间:2013-12-18 17:29:18

标签: c# android ios unity3d

Hello Stack Overflow社区。

我刚刚开始使用Unity将我的视频游戏移植到多个平台。我有一个关于在Unity中以编程方式创建对象的问题。这就是我目前的游戏:

enter image description here

当用户点按相机按钮时,相机图片会在点击和关闭时缩小。我希望整个屏幕仅短暂闪烁白色,但我不知道如何做到这一点。以下是我对此问题的C#代码:

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")
                {

                }
            }
        }
    }
}

非常感谢任何帮助。我真的不知道如何插入PNG或创建一个将覆盖短暂的矩形。

1 个答案:

答案 0 :(得分:5)

这是一个老问题,我在这里为您提供2种解决方案:(这些都是用C#编写的)

解决方案#1:正是您所要求的代码格式。我相当肯定你已经解决了这个问题(但这是针对偶然发现这个问题的其他人的。)

//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;
}

另一种方法是实际上弄乱Unity的灯光。这是我个人最喜欢的选项,因为您可以操纵光线,强度,范围(如果使用光点/点光源),颜色和更多选项的位置。

对于以下解决方案,我将一个简单的Light组件附加到主摄像头。

  • 类型:Directional
  • 颜色:白色
  • 强度:8
  • 已启用:错误

解决方案#2:只需在需要闪光灯时调用StartCoroutine(CameraFlash());

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
}