Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Real time duration that game was minimized on android / ios

Discussion in 'Scripting' started by startas, Jun 11, 2015.

  1. startas

    startas

    Joined:
    Nov 14, 2014
    Posts:
    102
    Hi, i want to calculate real time, for how long game was minimized in android / ios. For now, i'm coding in c# and using System.DateTime structure to get time (DateTime.UtcNow) when game was minimized and time when game was restored, and then i calculate the difference. The problem is, if you change phones date/time, then DateTime.UtcNow will return changed phones date also, so in the end i will have incorrect/cheated time, that game was minimized for. Is there a way to calculate somehow a real time, that game was minimized for on android/ios ? Using internet is not an option for me, also, other games are using some kind of background processes to calculate real time, you cant cheat on them by changing phones time, and also these games do not use internet time, but these are not made on unity. So is it possible to do that on unity for android/ios ?
     
  2. SeriousBusinessFace

    SeriousBusinessFace

    Joined:
    May 10, 2014
    Posts:
    127
    Try starting a System.Diagnostics.Stopwatch in a background thread. That should get you a consistent time that always works, unless the phone OS isn't multi-tasking. In which case, they're probably not playing games on it, either.
     
  3. startas

    startas

    Joined:
    Nov 14, 2014
    Posts:
    102
    Thanks, that worked good, just had to put Stopwatch timer.Start() into code, which is executed, when game is minimized, and timer.Stop() when game is resumed.
     
  4. startas

    startas

    Joined:
    Nov 14, 2014
    Posts:
    102
    We have a problem - this method doesn't always work : when phone is active, then stopwatch timer is counting time and everything is ok, but if phone is put to sleep (which is always happening, if you are not using your phone), then stopwatch timer stops counting time, so this method doesn't really work. I was thinking about calling android native functions and getting phones uptime. Anyone knows a good tutorial, how to call android native functions from unity ? For now, i found another thing in unity - Time.realtimeSinceStartup, will be testing it for now.

    Edit : Time.realtimeSinceStartup is bad too, it is not real time, its fake, it doesnt count time when phone is put to sleep. So i guess the only option is to write native android plugin and get system uptime.
     
    Last edited: Jun 23, 2015
  5. jedla22

    jedla22

    Joined:
    May 16, 2019
    Posts:
    6
    We faced the same problem in our Company. the Stopwatch works correctly when the app goes to background, but when the system goes to sleep the Stopwatch stops ticking (approximately after one minute after screen lock). For iOS, our solution is to use the custom native plugin, that uses macOS/iOS native C function clock_gettime. This function used with theCLOCK_MONOTONIC clock should do the job for you. For more info see
    http://www.manpagez.com/man/3/clock_gettime/ and
    https://stackoverflow.com/questions/12488481/getting-ios-system-uptime-that-doesnt-pause-when-asleep. Currently not sure about the solution for Android. BUt the StopWatch behavior is probably a bug in Unity IL2CPP compiler and Stopwatch implementation. Going to create a new Issue on the Unity Issue tracker.