【问题标题】:How do I create a white rectangle in Unity 2D?如何在 Unity 2D 中创建一个白色矩形?
【发布时间】:2013-12-18 17:29:18
【问题描述】:

你好 Stack Overflow 社区。​​p>

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

当用户点击相机按钮时,相机图片在 onTap 和 offTap 上放大。我希望整个屏幕只闪烁一秒钟,但我不知道该怎么做。这是我已经为这个问题准备的 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 或创建一个覆盖一秒钟的矩形。

【问题讨论】:

  • 那段时间你需要输入吗?如果没有,您可以使用 OnGUI 创建一个对象,该对象绘制一个全屏白色矩形,直到您不需要它,然后隐藏或销毁游戏对象。如果您需要在白化期间进行交互,您可以创建一个带有顶点着色器的对象,该对象绘制一个全屏四边形并将其附加到 Unity 四边形对象。
  • 我不需要任何输入。基本上我需要整个屏幕闪烁一秒钟。每次激活 hit.collider 时,GetMouseButtonUp 上的整个屏幕都应闪烁白色。你可以帮帮我吗?我对统一很陌生,我不明白如何编写该代码:D
  • 您真正需要做的就是创建一个统一四边形并将其附加到您的相机上,以便它呈现在其他所有东西的前面。给它一个简单的着色器,例如 Unlit 并定位它,使其完全覆盖相机视口(您可以将它作为相机的父对象,使其始终跟随相机)。然后只需从脚本中启用/禁用它
  • 你能发布一些代码来回答这个问题吗?请:D

标签: c# android ios unity3d


【解决方案1】:

这是一个老问题,我在这里为您提供 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 组件附加到主摄像机。

  • 类型:定向
  • 颜色:白色
  • 强度:8
  • 启用:错误

解决方案 #2:当您希望 Flash 发生时,只需调用 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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    相关资源
    最近更新 更多