Search Unity

No update calls while in background

Discussion in 'Web' started by devluz, Sep 28, 2015.

  1. devluz

    devluz

    Joined:
    Aug 20, 2014
    Posts:
    66
    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 :/

    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!
     
  2. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    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.
     
  3. devluz

    devluz

    Joined:
    Aug 20, 2014
    Posts:
    66
    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.

    Code (JavaScript):
    1.  
    2. fixed update tick unity time 57         DateTime: 12:12:43.0360000      frame count: 1773
    3. fixed update tick unity time 57.0625    DateTime: 12:12:43.0370000      frame count: 1773
    4. fixed update tick unity time 57.125     DateTime: 12:12:43.0390000      frame count: 1773
    5. fixed update tick unity time 57.1875    DateTime: 12:12:43.0400000      frame count: 1773
    6. fixed update tick unity time 57.25      DateTime: 12:12:43.0410000      frame count: 1773
    7. fixed update tick unity time 57.3125    DateTime: 12:12:44.0550000      frame count: 1774
    8. fixed update tick unity time 57.375     DateTime: 12:12:44.0560000      frame count: 1774
    9. fixed update tick unity time 57.4375    DateTime: 12:12:44.0570000      frame count: 1774
    10. fixed update tick unity time 57.5       DateTime: 12:12:44.0580000      frame count: 1774
    11. fixed update tick unity time 57.5625    DateTime: 12:12:44.0580000      frame count: 1774
    12. fixed update tick unity time 57.625     DateTime: 12:12:44.0590000      frame count: 1774
    13. fixed update tick unity time 57.6875    DateTime: 12:12:45.0680000      frame count: 1775
    14. fixed update tick unity time 57.75      DateTime: 12:12:45.0700000      frame count: 1775
    15. fixed update tick unity time 57.8125    DateTime: 12:12:45.0710000      frame count: 1775
    16. fixed update tick unity time 57.875     DateTime: 12:12:45.0710000      frame count: 1775
    17. fixed update tick unity time 57.9375    DateTime: 12:12:45.0720000      frame count: 1775
    18. fixed update tick unity time 58         DateTime: 12:12:46.0850000      frame count: 1776
    19.  
    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
     
    Last edited: Sep 29, 2015
  4. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
  5. FamerJoe

    FamerJoe

    Joined:
    Dec 21, 2013
    Posts:
    193
    The "one second" work around is still a thing in Unity 2017 it appears. No way to avoid this still I take it?
     
    zeroisthenew1 likes this.
  6. zeroisthenew1

    zeroisthenew1

    Joined:
    Jun 20, 2018
    Posts:
    8
    Same issue here :/