2013-12-18 55 views
2

你好堆棧溢出社區。如何在Unity 2D中創建白色矩形?

我剛剛開始使用Unity將我的視頻遊戲移植到多個平臺。我有一個關於在Unity中以編程方式創建對象的問題。這是它我的比賽看起來像目前:

enter image description here

當用戶點擊拍照按鈕,攝像機圖像縮放大和的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或創建一個矩形,這個矩形會在短時間內重疊。

+1

在那段時間你需要輸入嗎?如果沒有,你可以用一個OnGUI創建一個對象,直到你不需要它爲止,然後隱藏或銷燬gameObject。如果在whiteout期間需要交互性,則可以使用頂點着色器創建一個對象,繪製全屏幕四邊形並將其附加到Unity四邊形對象。 – theodox

+0

我不需要任何輸入。基本上我需要整個屏幕閃爍白色一秒鐘。每次啓動hit.collider時,整個屏幕都會在GetMouseButtonUp上閃爍白色。你可以幫幫我嗎?我很團結,我不懂如何編寫代碼:D –

+4

所有你真正需要做的就是創建一個統一的四邊形,並將其附加到你的相機,使它呈現在其他任何東西前面。給它一個簡單的着色器,例如Unlit,並將它放置爲使其完全覆蓋相機視口(您可以將它放在相機上,以便它始終跟在相機上)。然後啓用/禁用腳本 – theodox

回答

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組件。

  • 類型:定向
  • 顏色:白色
  • 強度: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 
}