Pause Standalone Game via Script (OnApplicationPause)

Hey all!

I’m programming a little app for a windows tablet.
To reduce power consumption, I’m trying to find a way to pause my application via script (timeout).
Manipulating timeScale is not the proper way. I want to completely stop the engine from rendering etc…
Like it is doing when runInBackground is disabled and the game window looses focus. (OnApplicationPause is triggered then). I tried the hacky way activating another window with user32.dll, but result is to random especially on Win11 devices.
I think the pause button in the editor is doing pretty much what I need.

Any suggistions how to do that?

Thanks in advance!

I’ve never seen mention of a magical runtime “pause” to my knowledge. And by that, I mean a simple call that will stop everything from doing anything.

With that in mind, you’re going to have to handle the pause yourself. Timescale is one part of it. The other might be a simple pause bool you can check against. to stop other things. And pause any music that is playing or other sfx if you want.

If it’s a matter of what is on the screen, then maybe a simple black image overlay is enough to reduce power usage. Or, turning off the camera with an overlay?

There are definitely things you can do on your own to reduce usage if you want.

Someone else may have some better suggestions, but those are just ones that come to mind.

Yes. You are right. I did all this stuff and indeed it did reduced GPU consumption to the half. But it didn’t stop the renderer from refreshing. So l can’t reduce GPU usage to nearly zero.
But this happens if I put the whole window in the background (if useInBackground is false). Everything stops where it is and CPU and GPU usage ist close to zero.
By reactivating you are at the same frame you stopped.
So somewhere under the hood there is a method which ist hooked in the window handle and stops the pipeline from refreshing.
Maybe someone knows how to trigger that from code.
I tried to deactivate the window with native windows dlls, but this results in ugly flashing window off and on…

You could set Application.targetFrameRate to 1 (for example), then back to -1 at unpause (or 60, or whatever you want).

1 Like

You’re right. I will try this.

1 Like

I tested and dropping frame rate to 1 has completly no impact on GPU consumption. So I have to find another way…

Did you find a solution?

What might work is to use Thread.Sleep inside Update (of course conditionally). Keep in mind that when you actually block the main thread, your game can not respond to any message or input. So I would never use a sleep time longer than 1000ms (one second).

Apart from that there is the adaptive performance package which may be able to help here. Though I never used it.

1 Like