你好 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 或创建一个将覆盖一小段时间的矩形。
最佳答案
这是一个老问题,不过我在这里为您提供了 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 组件附加到 Main Camera。
- 类型:定向
- 颜色:白色
- 强度: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
}
关于c# - 如何在 Unity 2D 中创建一个白色矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20664615/