Search Unity

WebGL and Time issues when game not focused

Discussion in 'Web' started by DZeb, Aug 7, 2019.

  1. DZeb

    DZeb

    Joined:
    Sep 17, 2015
    Posts:
    10
    Hello,

    I’ve been having issues with keeping track of time on WebAssembly when the game is not focused. I read the docs, this is due to browser throttling the game and affecting Time.time when not focused.
    So I run a few tests on Chrome and Mozilla.

    I used 2 timers, one on Update() with scaled time Time.deltaTime and one in a coroutine with WaitForSecondsRealtime() which, if I understand correctly, utilizes the FixedUpdate and the Time.unscaledTime. Then I changed tabs and minimized the browsers.

    On Mozilla the timers where almost on par with each other with the unscaled time lagging behind 4 seconds on a test of 514 seconds. It kept accurate track of time even when switched tabs and minimized but I saw some serious CPU usage, I am not sure if it was because of the console logs or the browser struggling to not throttle the game.

    On Chrome both timers where pretty bad, scaled time went up to 289 out of 514 seconds and unscaled time up to 386. The CPU usage was minimal, probably because it wasn't trying to run the game in the background at all.

    The above were not unbiased measurements, they were running behind the most demanding scene of my game, as I wanted them to be affected.

    I tried both with 60 and 30 fps for the scaled time, the results were always around half the real time, on Chrome only.

    Is there a better way to keep track of time on non-focused WebGL games? Compatibly with only one browser is out of the question and I really need accurate time as it’s a multiplayer game. Another solution would be to ask the browser for the time with a JavaScript script, but I would like a Unity non-hacky solution.

    Thanks for any answers in advance!
     
  2. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    270
    Don't!

    Are you sure you do?

    To answer your question, there's no Unity non-hacky solution, even if you "asked the browser for the time."
     
    DZeb likes this.
  3. DZeb

    DZeb

    Joined:
    Sep 17, 2015
    Posts:
    10
    Unfortunately I do, its a Quiz a game so focusing out of the game and thinking indefinitely kinda defeats the purpose. Thanks for the answer, at least now I can focus on looking for something else between getting the time from the browser or the server!