Search Unity

Alternative to Time.realtimeSinceStartup for mobile builds

Discussion in 'Scripting' started by ironfiftynine, Sep 25, 2015.

  1. ironfiftynine

    ironfiftynine

    Joined:
    Jan 22, 2013
    Posts:
    71
    Good day. I have implemented an energy system that regenerates through time. This time should update even if the app is in the background. I used Time.realtimeSinceStartup since it seems to fit my needs. However, I discovered that it returns incorrect values for mobile builds if the app regains focus after being placed from the background. I searched if this is a Unity issue and look for alternative solutions. I found these posts.

    http://answers.unity3d.com/questions/748919/timerealtimesincestartup-behaves-weirdly-when-send.html

    http://answers.unity3d.com/questions/139636/timerealtimesincestartup-returns-negative-number.html

    http://forum.unity3d.com/threads/real-time-duration-that-game-was-minimized-on-android-ios.332547/

    There were two viable options:
    1. Use a web server to get the server time and compute the time difference. However this will require the players to be connected to the internet.
    2. Use System.DateTime.Now. However, this can be cheated by adjusting the device time.
    With the cons of the two approaches above, I was wondering if there are any approaches that I missed? Is the bug for realtimeSinceStartup already fixed in the latest versions? I'm using v. 5.1.0f3.
     
  2. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    I think you can access utc time with System.Datetime.UtcNow, can't test atm im on phone. This should at least stop people from cheating by changing timezones. Then if they changed the clock manually, I'm not sure what could be done using a time system.
     
  3. ironfiftynine

    ironfiftynine

    Joined:
    Jan 22, 2013
    Posts:
    71
    Yeah. That's the problem. I guess compromise is inevitable. I'll just use the system time for now. Thanks for the help. :)