Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Unity 2D中创建白色矩形?_C#_Android_Ios_Unity3d - Fatal编程技术网

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

C# 如何在Unity 2D中创建白色矩形?,c#,android,ios,unity3d,C#,Android,Ios,Unity3d,你好,堆栈溢出社区 我刚刚开始使用Unity将我的视频游戏移植到多个平台。我有一个关于在Unity中以编程方式创建对象的问题。这就是我的游戏目前的样子: 当用户点击相机按钮时,相机图片在“打开”和“关闭”两种情况下缩放得更大。我希望整个屏幕只闪烁一秒钟,但我不知道怎么做。这是我已经为这个问题准备好的C#代码: using UnityEngine; using System.Collections; public class question3 : MonoBehaviour { in

你好,堆栈溢出社区

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

当用户点击相机按钮时,相机图片在“打开”和“关闭”两种情况下缩放得更大。我希望整个屏幕只闪烁一秒钟,但我不知道怎么做。这是我已经为这个问题准备好的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或创建一个将覆盖一秒钟的矩形

这是一个老问题,尽管如此,我还是为你准备了两个解决方案:(这些都是用C#编码的)

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

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

对于以下解决方案,我将一个简单的灯光组件连接到主摄像头

  • 类型:定向
  • 颜色:白色
  • 强度:8
  • 已启用:False
解决方案2:只需调用
start例程(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
}
IEnumerator CameraFlash()//您可以随意命名它
{
//等待1/4秒(可能您希望在屏幕闪烁前播放一个小声音)
收益率返回新WaitForSeconds(0.25f);
//从主摄影机获取灯光组件
Light cameraFlash=GameObject.FindWithTag(“主摄像头”).GetComponent();
//启用照相机闪光灯
cameraFlash.enabled=true;
//这将使强度每0.05秒降低2
对于(浮点f=8;f>=0;f-=2)
{
cameraFlash.intensity=f;//强度采用浮点数,因此您可以很好地将其更改
//只需确保在某个点将其设置为0.0f(以便不再有多余的光线)
收益率返回新WaitForSeconds(0.05f);
}
产生返回新WaitForSeconds(2);
cameraFlash.enabled=false;//确保再次禁用它(直到您需要它)
cameraFlash.intensity=8;//并将强度重置回8
}

在这段时间内你需要输入吗?如果不需要,你可以使用OnGUI创建一个对象,绘制一个全屏白色矩形,直到你不需要它,然后隐藏或销毁游戏对象。如果你在白化期间需要交互性,你可以使用顶点着色器创建一个对象,绘制一个全屏四元体,并将其附加到Unity四元体上对象。我不需要任何输入。基本上我需要整个屏幕闪烁一秒钟。每次hit.collider被激活时,GetMouseButtonUp上的整个屏幕都应该闪烁白色。你能帮我吗?我对unity非常陌生,我不明白如何编写代码:你真正需要做的是创建一个unity四元组并附加它给它一个简单的着色器,比如unlight,并定位它,使它完全覆盖摄影机视口(你可以将它设置为摄影机的父对象,使它始终跟随摄影机)。然后从脚本中启用/禁用它。你能发布一些代码来回答这个问题吗?Plz:D
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
}