Keep the framerate when tab is not focused

The default behavior of Unity, is to lower the framerate when the tab/window/page is not focused.

Is there a recommended way to disable this?

I guess that it’s registered to “visibilitychange” and checks “document.visibilityState” or “document.hidden”, and then it’ll be possible to ignore the event.
And looking at the framework file, it does that, but I’m not sure if it’s the only data that controls this behavior.

Maybe there’s a setting that already does that?

Thanks

Have you tried the “Run in background” check in the player settings?

Yes. When it is on, the framerate goes down, if it’s off the game stops.

1 Like

No, this is how browsers work. Any code you write is throttled by the browser when the tab is not focused, there’s nothing you can do about this. It isn’t anything to do with Unity

I’m not sure. I saw that window.requestAnimationFrame can keep running in a good framerate even when tab is not focused

As you can see in the sample here

There’s no throttling when the tab lose focus… The cube keeps on rotating smoothly

Hmm I believe we should not be doing any custom framerate throttling in Unity engine itself. It should be based on rAF(), and if “Run in background” is enabled, when we detect that to occur, we switch to running a custom setTimeout() loop to pump the render loop when rAF()s are no longer firing.

If you have a demo that behaves on the contrary compared to how rAF() and page focus works on the web, if you can host it somewhere we could take a peek together. Of course a bug here may be possible, but would be good to check closer what the behavior you are seeing is.

I’m doing some tests again.

It seems that the issue is on my end, while replacing the window.rAF with inline XRSession.rAF, it seems that the inline session does the throttling.
As can also be seen here
https://immersive-web.github.io/webxr-samples/inline-session.html

Testing builds with no inline XRSession, does work as intended and without throttling.

Thanks

if we can handle i think you should uncheck Run in background" in the player settings. when we back the game will be continued
it worked for me