Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Pause Standalone Game via Script (OnApplicationPause)

Discussion in 'Scripting' started by fox_digitalanimals, Oct 7, 2022.

  1. fox_digitalanimals

    fox_digitalanimals

    Joined:
    Oct 18, 2021
    Posts:
    4
    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!
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,148
    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.
     
  3. fox_digitalanimals

    fox_digitalanimals

    Joined:
    Oct 18, 2021
    Posts:
    4
    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...
     
  4. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,505
    fox_digitalanimals likes this.
  5. fox_digitalanimals

    fox_digitalanimals

    Joined:
    Oct 18, 2021
    Posts:
    4
    You're right. I will try this.
     
    adamgolden likes this.
  6. fox_digitalanimals

    fox_digitalanimals

    Joined:
    Oct 18, 2021
    Posts:
    4
    I tested and dropping frame rate to 1 has completly no impact on GPU consumption. So I have to find another way...