I noticed that the “Run in Background” flag in unitys build settings doesn’t seem to work properly with webgl.
If the flag is set and I minimize the window of the browser it will stop calling the update methods. Not a problem for most Singleplayer games but fatal for multiplayer games
The only thing the flag does so far is to keep the game running while the window itself isn’t active but still in the foreground.
Is this a bug or by design? Anything I can do to make it run in the background?
Which version of Unity is this? I noticed that many browsers will stop calling our render loop to let us update the player, when the page is not visible. In Unity 5.2, as a workaround, we have a second update loop which will be used as a fallback, when the render loop is not being called, so the game can keep updating. However, this fallback loop can only be called once a second.
The original problem was from 5.1.3 but I updated now to 5.2.1f1 (free version). I am using firefox 41.0 32 bit for testing.
Like you said I get 1 frame per second if firefox is in minimized. I got confused by my own test to see if it runs in the background:
if(Time.frameCount % 60 == 0) {Debug.Log(“test”);}
As the FPS is 1 it just took a while to see that it is indeed calling the update method.
Anyway I have spent some time testing with 5.2.1f1 and here is what I found out so far:
This happens if I log Time.time, DateTime.Now.TimeOfDay and the frame count in update every frame:
update tick unity time 14.95614 DateTime: 12:04:15.8600000 frame count: 790
update tick unity time 15.28947 DateTime: 12:04:16.8660000 frame count: 791
update tick unity time 15.6228 DateTime: 12:04:17.8620000 frame count: 792
update tick unity time 15.95614 DateTime: 12:04:18.8690000 frame count: 793
The FPS is as expected 1 but unitys time only moves forward by a third of a second each real second.
Same problem affects unitys fixed update method. It is configuration to be called 16 times a second but as 1 sec in unity takes 3 seconds in reality it is way too slow.
Instead of 16 time per second FixedUpdate is called 5-6 times a second.
This only happens while the window is minimized. For a network game this can cause lots of confusion. But at least I can imagine a work around now. Not sure yet how coroutines will behave.
Where does this slow down in unitys time come from?