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

乔丹·舒茨

你好堆栈溢出社区。

我刚刚开始使用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或创建一个可以短暂覆盖的矩形。

RedPeepin

这是一个古老的问题,尽管如此,我仍然为您提供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:只需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
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Unity 2D中播放视频?

来自分类Dev

如何在Smalltalk中创建2D数组

来自分类Dev

如何在Unity中创建动态表

来自分类Dev

如何找到由各种2D矩形的集合创建的边框长度?

来自分类Dev

如何在Unity3D中为2D云设置动画?

来自分类Dev

如何在Unity 2D面板中重新排列项目?

来自分类Dev

如何在Unity 5中绘制2D三角形?

来自分类Dev

如何在Unity Game Engine(2D)中检测触摸

来自分类Dev

如何在Unity 2D中删除切换用户帐户选项?

来自分类Dev

如何在Unity 2D中为按钮设置动画?

来自分类Dev

如何在Unity 2D中删除切换用户帐户选项?

来自分类Dev

如何在Unity 2D面板中重新排列项目?

来自分类Dev

如何在Unity 2D中自动隐藏启动器?

来自分类Dev

如何在 Unity 中调整相机大小(2D 项目)

来自分类Dev

如何在 Unity 2d 中制作推进器?

来自分类Dev

如何在Unity,Unity 2D和Gnome之间切换当前会话?

来自分类Dev

如何在Unity,Unity 2D和Gnome之间切换当前会话?

来自分类Dev

如何在 Unity2d 中创建“交互式”网格?

来自分类Dev

Unity如何在Unity中制作视觉JoyStick

来自分类Dev

如何在 Unity HUB 中添加最新的 Unity 版本?

来自分类Dev

如何解决2D网格中两个矩形之间的冲突?

来自分类Dev

如何在Unity 4.3中旋转精灵?

来自分类Dev

如何在Unity中更改材料的颜色?

来自分类Dev

如何在Vuforia for Unity中停止跟踪

来自分类Dev

如何在Unity中更改字体类型?

来自分类Dev

如何在Unity中向左滑动按钮?

来自分类Dev

如何在Unity中输入IP地址?

来自分类Dev

如何在Unity中随机选择GameObject

来自分类Dev

如何在Unity中处理很长的句子?

Related 相关文章

热门标签

归档