Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Displaying a GUI Label using the Oculus Rift

Discussion in 'AR/VR (XR) Discussion' started by steb92, Jan 14, 2015.

  1. steb92

    steb92

    Joined:
    Nov 7, 2014
    Posts:
    29
    Hello,

    I've created a simple timer script (to count down from X seconds) and I want to display the value to the user as he/she plays through the level.

    I've tried adding the script to both eyes of the OVRCameraRig, and adding it to each eye individually but no luck. I must be doing something wrong.

    Any suggestions?

    Below is the Timer.cs I have written. I have tested this script on the standard Unity camera without issue.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Timer : MonoBehaviour {
    5.  
    6.     public float startTime;
    7.     private string currentTime;
    8.  
    9.     public GUISkin skin;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.    
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update ()
    18.     {
    19.         startTime -= Time.deltaTime;
    20.         currentTime = string.Format("{0:0.0}",startTime);
    21.         print(currentTime);
    22.  
    23.         if (startTime <= 0)
    24.         {
    25.             startTime = 0;
    26.             print("GAME OVER!");
    27.             Application.LoadLevel("game_over");
    28.         }
    29.     }
    30.  
    31.     void OnGUI()
    32.     {
    33.         GUI.Label(new Rect(50, 10, 200, 45), currentTime);
    34.     }
    35. }
    36.  
     
  2. steb92

    steb92

    Joined:
    Nov 7, 2014
    Posts:
    29
    I've managed to get it displaying in the top left but it's not attached to the Oculus camera's themselves. Any attempt to centre the timer results in it no longer being visible (I assume it's "behind" the Oculus views)

    Here's a large screenshot of it in action.

    http://i.imgur.com/BFI4Bql.png

    Another small issue is that, when attached to the Oculus player as per the screenshot, the timer will bug out after 3/4 seconds and the text becomes a blur.
     
  3. El Maxo

    El Maxo

    Joined:
    May 23, 2013
    Posts:
    177
    What unity are you using?
     
  4. steb92

    steb92

    Joined:
    Nov 7, 2014
    Posts:
    29
    Unity Free 4.5.5p5.
     
  5. ldb

    ldb

    Joined:
    Apr 30, 2013
    Posts:
    37
    If I were you I'd update to 4.6 and use the new UI system. You can create a canvas in world space, which you can then add as a child of your OVR rig and place a short distance in front of you.

    You'll struggle using the old onGUI, and the update is free so hopefully that's a viable option for you
     
    El Maxo and that-steve-guy like this.
  6. steb92

    steb92

    Joined:
    Nov 7, 2014
    Posts:
    29
    Thank you. I have just updated to 4.6.1p4. Do you have any material regarding how to go about using the new GUI system? Specifically, appending a canvas via a script, as I was originally doing using the onGUI() method?
     
    Last edited: Jan 16, 2015
  7. ldb

    ldb

    Joined:
    Apr 30, 2013
    Posts:
    37
  8. steb92

    steb92

    Joined:
    Nov 7, 2014
    Posts:
    29
    Thank you for all the help. I now have my Timer displaying correctly.

    This is the code that I am using. The //commented code refers to the OP which I have left in.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class Timer : MonoBehaviour {
    6.  
    7.     public float startTime;
    8.     private string currentTime;
    9.  
    10.     Text text;
    11.  
    12.     public GUISkin skin;
    13.  
    14.    
    15.     void Awake () {
    16.         text = GetComponent<Text>();
    17.  
    18.      
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update ()
    23.     {
    24.         startTime -= Time.deltaTime;
    25.         currentTime = string.Format("{0:0.0}",startTime);
    26.         print(currentTime);
    27.  
    28.         text.text = "Time Left: "+currentTime;
    29.  
    30.         if (startTime <= 0)
    31.         {
    32.             startTime = 0;
    33.             print("GAME OVER!");
    34.             Application.LoadLevel("game_over");
    35.         }
    36.     }
    37.  
    38.     //void OnGUI()
    39.     //{
    40.       //  GUI.Label(new Rect(10, 10, 200, 45), currentTime);
    41.    // }
    42. }
    43.