Search Unity

Display accurate remaining time (code included)

Discussion in 'Scripting' started by getafix99, Feb 5, 2013.

  1. getafix99

    getafix99

    Joined:
    Jan 29, 2013
    Posts:
    40
    I want to display also the miliSeconds so it will be something like this:

    minutes : seconds : miliSeconds

    2 : 34 : 99

    so I need to add miliSeconds to this code
    Code (csharp):
    1.  
    2. var startTime:float;
    3. var timeRemaining:float;
    4.  
    5.  var minutes:int;
    6. var seconds:int;
    7. var miliSeconds:int;
    8. var timeStr:String;
    9.  
    10. function Start () {
    11. startTime = 130.0;
    12. }
    13.  
    14. function Update () {
    15. timeRemaining = startTime - Time.time;
    16.  
    17. minutes = timeRemaining / 60;
    18. seconds = timeRemaining % 60;
    19. //miliSeconds = ?
    20. timeStr = minutes.ToString()+":"+seconds.ToString("D2");
    21.  
    22. guiText.text = timeStr;
    23. }
     
  2. SubatomicHero

    SubatomicHero

    Joined:
    Jan 22, 2013
    Posts:
    24
    Is this counting up or counting down?

    You could possibly use a substr function :D for example:

    var startTime : float = 0.0;
    var minutes : string;
    var seconds : string;
    var milliseconds : string;

    function Update()
    {
    startTime += Time.time;

    minutes = startTime.ToString().Substring(1,2);
    seconds = startTime.ToString().Substring(3,2);
    milliseconds = startTime.ToString().Substring(5,2);

    guiText.text = minutes + ":" + seconds + ":" + milliseconds;
    }

    This may possibly work, give it a whirl
     
    Last edited: Feb 5, 2013
  3. chronos78

    chronos78

    Joined:
    Dec 6, 2009
    Posts:
    154
    Here's another way that you could approach the problem. This way will automatically add and configure a GUIText component simply by adding this script to a GameObject. It doesn't rely on Update() so it uses less resources but still maintains tenth of a second accuracy and stops invoking once the countdown is finished unlike Update(). It can be started or restarted by calling Begin(). It's duration can also changed since it isn't hard coded and by assigning a callback action it is able to perform any actions required once the countdown is over.

    Code (csharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(GUIText))]
    6. public class CountDownTimer : MonoBehaviour
    7. {
    8.     public int DurationInSeconds = 130;
    9.     private TimeSpan duration;
    10.     private DateTime startTime;
    11.     public Action Callback { get; set; }
    12.  
    13.     private void Awake()
    14.     {
    15.         guiText.transform.localScale = Vector3.zero;
    16.         guiText.transform.position = new Vector3(0.5f, 0.5f, 0);
    17.     }
    18.  
    19.     private void Start()
    20.     {
    21.         Begin();
    22.     }
    23.  
    24.     public void Begin()
    25.     {
    26.         duration = new TimeSpan(0, 0, DurationInSeconds);
    27.         startTime = DateTime.Now;
    28.         if (!IsInvoking("CountDown"))
    29.             InvokeRepeating("CountDown", 0, 0.1f);
    30.     }
    31.  
    32.     private void CountDown()
    33.     {
    34.         var timeRemaining = duration - (DateTime.Now - startTime);
    35.         var fractionalSeconds = Math.Round(timeRemaining.Milliseconds / 1000f, 1);
    36.         fractionalSeconds = (fractionalSeconds - (int)fractionalSeconds) * 10;
    37.  
    38.         if (fractionalSeconds < 0)
    39.         {
    40.             fractionalSeconds = 0;
    41.             CancelInvoke("CountDown");
    42.             if (Callback != null)
    43.                 Callback();
    44.         }
    45.  
    46.         guiText.text = string.Format("{0:D2}:{1:D2}.{2}",
    47.                                      timeRemaining.Minutes,
    48.                                      timeRemaining.Seconds,
    49.                                      fractionalSeconds);
    50.     }
    51. }
    52.