【问题标题】:OnGUI in VR not showingVR 中的 OnGUI 不显示
【发布时间】:2015-08-28 12:34:46
【问题描述】:

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

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

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

【问题讨论】:

    标签: user-interface unity3d oculus virtual-reality


    【解决方案1】:

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

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

    将画布(或其他包含“画布”组件的 UI 元素)添加到您的场景。将渲染模式设置为世界空间。这可以在 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);
     }
    }
    

    【讨论】:

    • 谢谢罗伯特。 winTextcountTexttimerText 是 UI text 游戏对象。对吗?
    • 是的(如果你声明例如:public Text winText; 你可以在 Unity 中设置它)。
    • 这一行将我的保留到小数点后两位timerText.text = timeLeft.ToString("0.00");
    • 不,他们是由于("0.00")。如果您只想查看秒数,请使用("0")。请参阅此处了解更多信息:msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx
    • 对不起!我的错 。 .你说的对。我做了Time.time.ToString("0");,它可以按我的意愿工作。谢谢!
    【解决方案2】:

    OnGUI 在 VR 中不起作用。你必须使用world space canvas UI

    【讨论】:

      猜你喜欢
      • 2016-02-09
      • 2023-03-27
      • 2015-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多