No update calls while in background

Hi

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 :confused:

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?

Thanks!

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.

Thank you for the fast reply.

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.

fixed update tick unity time 57         DateTime: 12:12:43.0360000      frame count: 1773
fixed update tick unity time 57.0625    DateTime: 12:12:43.0370000      frame count: 1773
fixed update tick unity time 57.125     DateTime: 12:12:43.0390000      frame count: 1773
fixed update tick unity time 57.1875    DateTime: 12:12:43.0400000      frame count: 1773
fixed update tick unity time 57.25      DateTime: 12:12:43.0410000      frame count: 1773
fixed update tick unity time 57.3125    DateTime: 12:12:44.0550000      frame count: 1774
fixed update tick unity time 57.375     DateTime: 12:12:44.0560000      frame count: 1774
fixed update tick unity time 57.4375    DateTime: 12:12:44.0570000      frame count: 1774
fixed update tick unity time 57.5       DateTime: 12:12:44.0580000      frame count: 1774
fixed update tick unity time 57.5625    DateTime: 12:12:44.0580000      frame count: 1774
fixed update tick unity time 57.625     DateTime: 12:12:44.0590000      frame count: 1774
fixed update tick unity time 57.6875    DateTime: 12:12:45.0680000      frame count: 1775
fixed update tick unity time 57.75      DateTime: 12:12:45.0700000      frame count: 1775
fixed update tick unity time 57.8125    DateTime: 12:12:45.0710000      frame count: 1775
fixed update tick unity time 57.875     DateTime: 12:12:45.0710000      frame count: 1775
fixed update tick unity time 57.9375    DateTime: 12:12:45.0720000      frame count: 1775
fixed update tick unity time 58         DateTime: 12:12:46.0850000      frame count: 1776

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?

Edit: fixed formatting

It comes from Time.maximumDeltaTime (which defaults to 1/3):

http://docs.unity3d.com/ScriptReference/Time-maximumDeltaTime.html

The “one second” work around is still a thing in Unity 2017 it appears. No way to avoid this still I take it?

1 Like

Same issue here :confused: