在VR中使用OnGUI无法显示

3

我在场景的左上角加入了以下代码,以显示一个简单的计时器,当然它能正常工作。但是,当我勾选“虚拟现实支持”复选框并戴上Oculus Rift头显时,计时器就消失了。

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

我错过了什么?我还需要做什么来解决这个问题?
2个回答

3

OnGUI()在VR中不起作用,请改用世界空间画布UI。

我为Gear-VR做了以下操作:

添加画布(或其他包含“canvas”组件的UI元素)到您的场景中。将渲染模式设置为World Space。这可以在UI Canvas对象的渲染模式下拉列表中找到:

enter image description here

最终我选择了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);
 }
}

1
谢谢罗伯特。winTextcountTexttimerText是UI中的text游戏对象,对吗? - user285372
1
是的(如果你声明了public Text winText;,你可以从Unity中设置它)。 - Robert
1
这行代码将保留两位小数 timerText.text = timeLeft.ToString("0.00"); - Robert
1
不,它们是由("0.00")引起的。如果你只想看到秒数,使用("0")。更多信息请参见此处:https://msdn.microsoft.com/zh-cn/library/0c899ak8(v=vs.110).aspx - Robert
1
抱歉!我的错。你是对的。我使用了Time.time.ToString("0");,它按照我想要的方式工作。谢谢! - user285372
显示剩余2条评论

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接