3

timer在场景的左上角添加了以下行以显示一个简单的场景,这当然可以,但是当我勾选Virtual Reality Supported复选框并戴上 Oculus Rift 时,它就会消失。

void OnGUI()
{
    GUI.Label(new Rect(10, 10, 100, 20), Time.time.ToString());
}

我错过了什么?我应该怎么做才能解决这个问题?

4

2 回答 2

3

OnGUI() 在 VR 中不起作用。而是使用世界空间画布 UI。

我为 Gear-VR 做了以下工作。

将画布(或其他包含“画布”组件的 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);
 }
}
于 2015-09-01T11:07:28.897 回答
2

OnGUI在 VR 中不起作用。您必须使用世界空间画布 UI

于 2015-08-31T16:23:12.370 回答