Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

GUI Time

Discussion in 'Immediate Mode GUI (IMGUI)' started by MikeC, Nov 22, 2006.

  1. MikeC

    MikeC

    Joined:
    Jul 15, 2006
    Posts:
    63
    More GUI goodness! ;)

    I'm creating a nice timer that will show how long the level has been played for. I used the script from the marble tutorial and ended up with this:

    Code (csharp):
    1. var startTime = 0;
    2.  
    3. function Update () {
    4. //The time taken so far
    5. timeTaken = startTime + Time.time;
    6. // Format the time nicely
    7. guiText.text = FormatTime (timeTaken);
    8.  
    9. }
    10.  
    11.  
    12. //Format time like this
    13. // 17[minutes]:21[seconds]:05[fraction]
    14.  
    15. function FormatTime (time)
    16.  
    17. {
    18.  
    19.      var intTime : int = time;
    20.      var minutes : int = intTime / 60;
    21.      var seconds : int = intTime % 60;
    22.      var fraction : int = time * 10;
    23.      fraction = fraction % 10;
    24.      
    25.      //Build string with format
    26.      // 17[minutes]:21[seconds]:05[fraction]
    27.      
    28.      timeText = minutes.ToString () + ":";
    29.      timeText = timeText + seconds.ToString ();
    30.      timeText += ":" + fraction.ToString ();
    31.      return timeText;
    32. }
    But the GUI Text just displays a regular "5.475893738" style timed instead...

    Where am I going wrong? Or is that tutorial really out of date now?

    Mike
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Seems fine. Are you sure you have the right script assigned and you are not getting any compile errors?
     
  3. MikeC

    MikeC

    Joined:
    Jul 15, 2006
    Posts:
    63
    Yup, I just remembered what you told me last time and sure enough, no complie errors = things work fine! :oops:

    One thing though; The timer is now 0:0:0 and doesn't go into double digits automatically. How can I make it start as 00:00:00?

    Thanks!
    Mike
     
  4. Talzor

    Talzor

    Joined:
    May 30, 2006
    Posts:
    197
    Try pasing the ToString method a custem format string. See MSDN for a full description. I guess you want
    Code (csharp):
    1.  
    2. timeText = minutes.ToString("00") + ":";
    3. timeText = timeText + seconds.ToString("00");
    4. timeText += ":" + fraction.ToString("00");
    5. return timeText;
     
  5. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Instead of concatintating a lot of small strings, you could also use the String.Format method, which looks nicer as you compose all the formatting into one string:

    Code (csharp):
    1.  
    2. //...
    3. return String.Format("{0:2,D}:{1:2,D}:{2:2,D}", minutes, seconds, fraction);
    4.  
     
  6. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Thank you for this thread. I've been wondering if there was a better way of doing this. Thanks!
     
  7. MikeC

    MikeC

    Joined:
    Jul 15, 2006
    Posts:
    63
    Thanks guys! Got it working! Didn't use the string.format as I'm too dense to figure that out right now (It's been a busy week ;)).

    Thanks!
    Mike
     
  8. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Could someone please tell me what painfully obvious thing I'm missing here?

    When I attach the JavaScriptbelow to a GUIText object, all it prints is "2D:2D:2D".


    Code (csharp):
    1.  
    2.  
    3.  
    4. import System;
    5.  
    6.  
    7. var startTime;
    8.  
    9.  
    10. function Awake() {
    11.  
    12.     startTime = Time.time;
    13.    
    14. }
    15.  
    16.  
    17. function Update () {
    18.  
    19.     var guiTime = Time.time - startTime;
    20.    
    21.     var minutes : int = guiTime / 60;
    22.     var seconds : int = guiTime % 60;
    23.     var fraction : int = (guiTime * 10) % 10;
    24.    
    25.     guiText.text = String.Format ("{0:2,D}:{1:2,D}:{2:2,D}", minutes, seconds, fraction);
    26.    
    27. }
    28.  
    29.  
    30.  


    [/code]
     
  9. ron

    ron

    Joined:
    Dec 21, 2007
    Posts:
    35
    marty, try the following code:

    Code (csharp):
    1.  
    2. guiText.text = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
    3.  
    4.  
     
  10. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Thanks!
     
  11. GameDevDave

    GameDevDave

    Joined:
    Oct 23, 2008
    Posts:
    29
    Could i use this timer for a start and stop like for a race? I want the timer to start at the beginning and stop after they pass the finish line
     
  12. Bluestrike

    Bluestrike

    Joined:
    Sep 26, 2009
    Posts:
    256
    Why are there no
    var timeTaken and
    var timeText?

    I'm still new at Java but know some c++ so I find it weird timeTaken and timeText are not generating errors :)
     
  13. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Why are you mentioning Java, we don't support Java at all. We do however support JavaScript, and JavaScript is a very different beast than Java. And on to the question, it's because JavaScript lets you be sloppy in many ways, it's the nature of the beast.


    I know, you probably meant JavaScript but my hoped for point remains, accuracy in terminology usage is important so let's remember, it's JavaScript that's supported in Unity, not Java. :)
     
  14. ashwin911

    ashwin911

    Joined:
    Oct 20, 2009
    Posts:
    1
    thanks for the code guys, but

    Code (csharp):
    1.    var fraction : int = (guiTime * 10) % 10;
    2.  
    3. //should be
    4.  
    5.    var fraction : int = (guiTime * 100) % 100;
    6.  
    otherwise ,when the time is 5.51 in seconds for example, you will get 00:05:05 instead of 00:05:51
     
  15. Stig_The_Ghost

    Stig_The_Ghost

    Joined:
    Sep 4, 2009
    Posts:
    12
    I made this in AS3 and converted it for unity it works fine I think sec++; would not be the best way to do it in unity but it will work fine this way anyway

    Code (csharp):
    1.  
    2.  
    3. private var sec : float;
    4. private var timeIs : boolean; // you can use this for a pause
    5.  
    6. function Update(){
    7.    
    8. if(timeIs == false){
    9. sec = sec;
    10.  
    11. }else{
    12. sec++;
    13. }
    14.  
    15. guiText.text = secondsConverter(sec);
    16.  
    17. }
    18.  
    19. function secondsConverter(sec : float) : String
    20. {
    21.     var h : float = Mathf.Floor(sec/3600);
    22.     var m : float = Mathf.Floor((sec%3600)/60);
    23.     var s : float = Mathf.Floor((sec%3600)%60);
    24.    
    25.     return(h==0?"":(h<10?"0"+h.ToString()+":":h.ToString()+":"))+(m<10?"0"+m.ToString():m.ToString())+":"+(s<10?"0"+s.ToString():s.ToString());
    26. }
    27.  
    28.  
     
  16. Bluestrike

    Bluestrike

    Joined:
    Sep 26, 2009
    Posts:
    256
    I based my countdown timer on the first examples,
    but now I have a weirdness going on I didn't notice in the past.
    My timer starts at 15:59, whats suposed to be 15:00. When I reach 15:31 it goes to 14:30.
    Then it shows 14:00 and next 14:60 when it comes to 14:31 it goes to 13:30 and so on.

    Code (csharp):
    1.  
    2. private var startTime           : int = 15 ;   // start time in minutes.
    3. private var passedTime      ;
    4. private var minutes             : int ;
    5. private var seconds             : int ;
    6.  
    7. function Awake()
    8. {
    9.     // Adjusting time from minutes to seconds.
    10.     startTime = startTime * 60;
    11. }
    12.  
    13.  
    14. function Update()
    15. {  
    16.     var theTime : String ;
    17.     var timer ;
    18.     timer = startTime ;
    19.     passedTime = timer - Time.time ;
    20.  
    21.     minutes = passedTime / 60 ;
    22.     seconds = passedTime % 60 ;
    23.  
    24.     theTime = String.Format ("{0:00}:{1:00}", minutes, seconds);
    25. }
    26.  
     
  17. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    The code above doesn't produce that behavior for me at all, it starts at 15:00 and counts down properly from there.

    Unity 2.6.1, Mac OS X 10.5.8
     
  18. Bluestrike

    Bluestrike

    Joined:
    Sep 26, 2009
    Posts:
    256
    hmm weird, I copied the original code in this thread and it does the same counting up,
    at 0:30 it goes to 1:30.
    Also tried it in the island demo in case it was something in my project.

    Windows vista 64 bit, Unity 2.6.1
     
  19. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Anyone else here tried this script? If so let's see those results as the only differences I'm seeing are that I'm on OS X and you're on Vista, and you might be set to a different language setting than me (you French? me English). I don't know why those would make a difference in this script though...
     
  20. Bluestrike

    Bluestrike

    Joined:
    Sep 26, 2009
    Posts:
    256
    I tried the timer script I posted with unity 1.5.1 on my brothers win XP machine but I have the same results. We both use the dutch language.

    I'm sure the script worked fine back in november/december when I added it to my game.
     
  21. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    You're confusing me here... Unity 1.5.1 on Windows? Did you mean Unity 2.5.1 on Windows, or something like Unity iPhone 1.5.1 on a Mac (iPhone builds are Mac-only)? Can you please clarify? Also, if you really are using Unity 1.5.1/2.5.1 then please be sure to upgrade to 2.6.1 now (Download Page).
     
  22. Bluestrike

    Bluestrike

    Joined:
    Sep 26, 2009
    Posts:
    256
    Oops 2.5.1 on windows, I am currently using 2.6.1 but I tried to see if it still happened in 2.5.1 because thats probably what I used when I added the timer too my game. There is a small chance I had a english version of Windows Vista installed but I am not completely sure.
     
  23. Bluestrike

    Bluestrike

    Joined:
    Sep 26, 2009
    Posts:
    256