我在场景的左上角加入了以下代码,以显示一个简单的计时器,当然它能正常工作。但是,当我勾选“虚拟现实支持”复选框并戴上Oculus Rift头显时,计时器就消失了。
void OnGUI()
{
GUI.Label(new Rect(10, 10, 100, 20), Time.time.ToString());
}
我错过了什么?我还需要做什么来解决这个问题?
我在场景的左上角加入了以下代码,以显示一个简单的计时器,当然它能正常工作。但是,当我勾选“虚拟现实支持”复选框并戴上Oculus Rift头显时,计时器就消失了。
void OnGUI()
{
GUI.Label(new Rect(10, 10, 100, 20), Time.time.ToString());
}
OnGUI()在VR中不起作用,请改用世界空间画布UI。
我为Gear-VR做了以下操作:
添加画布(或其他包含“canvas”组件的UI元素)到您的场景中。将渲染模式设置为World Space。这可以在UI Canvas对象的渲染模式下拉列表中找到:
最终我选择了800 x 600像素的画布。
对于计时器本身,我使用了Time.deltaTime
。
这是我的整个PlayerController
脚本:
void Start ()
{
timeLeft = 5;
rb = GetComponent<Rigidbody>();
count = 0;
winText.text = "";
SetCountText ();
}
void Update() {
if (gameOver) {
if (Input.GetMouseButtonDown(0)) {
Application.LoadLevel(0);
}
} else {
timeLeft -= Time.deltaTime;
timerText.text = timeLeft.ToString("0.00");
if (timeLeft < 0) {
winner = false;
GameOver(winner);
}
}
}
void GameOver(bool winner) {
gameOver = true;
timerText.text = "-- --";
string tryAgainString = "Tap the touch pad to try again.";
if (!winner) { // case A
winText.text = "Time's up.\n" + tryAgainString;
}
if (winner) { // case B
winText.text = "Well played!\n" + tryAgainString;
}
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Mouse X");
float moveVertical = Input.GetAxis ("Mouse Y");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ( "Pick Up")){
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
if (!gameOver) {
timeLeft += 3;
}
}
}
void SetCountText ()
{
if (!gameOver) {
countText.text = "Count: " + count.ToString ();
}
if (count >= 12) {
winner = true;
GameOver(winner);
}
}
winText
、countText
和timerText
是UI中的text
游戏对象,对吗? - user285372public Text winText;
,你可以从Unity中设置它)。 - RoberttimerText.text = timeLeft.ToString("0.00");
。 - Robert("0.00")
引起的。如果你只想看到秒数,使用("0")
。更多信息请参见此处:https://msdn.microsoft.com/zh-cn/library/0c899ak8(v=vs.110).aspx - RobertTime.time.ToString("0");
,它按照我想要的方式工作。谢谢! - user285372