Search Unity

add a timer in game screen

Discussion in 'Immediate Mode GUI (IMGUI)' started by Ixquic2D, Oct 30, 2014.

  1. Ixquic2D

    Ixquic2D

    Joined:
    Sep 9, 2014
    Posts:
    25
    ¿¡ HoW to add a timer into game screen ?!
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Here's a tutorial on many types of HUD elements. Jump down to Step 13 for the timer.
     
  3. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    Here is a timer that counts upwards in seconds:
    Code (JavaScript):
    1. var seconds : int;
    2.  
    3. function Start () {
    4.     seconds = 0;
    5.     InvokeRepeating("Count", 0, 1);
    6. }
    7.  
    8. function Count () {
    9.     seconds += 1;
    10.  
    11. }
    12.  
    13. var native_width : float = 1920;
    14. var native_height : float = 1080;
    15.  
    16. function OnGUI () {
    17.  
    18.     var rx : float = Screen.width / native_width;
    19.     var ry : float = Screen.height / native_height;
    20.     GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (rx, ry, 1));
    21.  
    22.     GUI.skin = customSkin;
    23.     GUI.Label(Rect(10, 10, 400, 300), "Time: " + seconds.ToString());
    24. }
    25.