Search Unity

Universal timer solutions?

Discussion in 'Scripting' started by hkdtmer, Sep 18, 2014.

  1. hkdtmer

    hkdtmer

    Joined:
    Apr 2, 2014
    Posts:
    35
    Unity's Time is in 32-bit float, that means if it runs over 10000 seconds (about 3 hours) then accuracy is only within 2 decimal places. It will even worse if timeScale is high.

    I've searched msdn for alternative timers. DateTime and Stopwatch are not so useful because they keep running even when Unity Editor is paused or during breakpoints. There are System.Timers and timer in System.Threading but Windows Store API doesn't support them. Windows Store uses DispatcherTimer but Unity doesn't support it.

    Are there any high accuracy timers can be used with Unity and at least usable in both Android and Windows Store apps? Thanks!
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    You can easily make you own, you know?

    Code (CSharp):
    1. public class MyTimer : MonoBehaviour
    2. {
    3.     private double time;
    4.  
    5.     public double Time
    6.     {
    7.         get { return time; }
    8.     }
    9.  
    10.     private void Update()
    11.     {
    12.         time += UnityEngine.Time.unscaledDeltaTime;
    13.     }
    14. }
     
  3. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    If you make the private time variable and the public getter static, then you can do MyTimer.Time to get the time.
    :)
     
  4. hkdtmer

    hkdtmer

    Joined:
    Apr 2, 2014
    Posts:
    35
    Excellent! Thank you very much!