你好堆栈溢出社区。
我刚刚开始使用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组件。
解决方案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] 删除。
我来说两句