【发布时间】:2016-11-16 19:06:13
【问题描述】:
当按下"Fire2" 时,此脚本控制摄像机的 FOV 以及显示 GUI 纹理。但是,一旦用户按下按钮一次,纹理就会显示并且直到再次按下按钮才会消失 - 我希望它只在按下按钮时显示?该脚本附加到作为播放器预制件的子对象的预制件上。我尝试只使用GetButton,但看到OnGUI() 被每帧调用以及Update() 函数它只是使纹理闪烁。
using UnityEngine;
using System.Collections;
public class awpScopeIn : MonoBehaviour {
public Texture scopeGUI;
private bool _isScoped = false;
public Color color = Color.black;
private Camera cam;
public GameObject awp_graphics;
void Start()
{
cam = GetComponentInParent( typeof(Camera) ) as Camera;
cam.clearFlags = CameraClearFlags.SolidColor;
cam.fieldOfView = 70f;
}
void OnGUI()
{
float width = 600;
float height = 600;
if (_isScoped) {
GUI.DrawTexture (new Rect ((Screen.width / 2) - (width/2), (Screen.height / 2) - (height/2), width, height), scopeGUI);
}
}
void Update()
{
if(Input.GetButtonDown("Fire2"))
{
_isScoped = !_isScoped;
cam.backgroundColor = color;
cam.fieldOfView = 45f;
awp_graphics.gameObject.SetActive(false);
}else if(Input.GetButtonUp("Fire2"))
{
cam.fieldOfView = 70f;
awp_graphics.gameObject.SetActive(true);
}
}
}
【问题讨论】:
标签: c# user-interface unity3d