Search Unity

Detect if computer is in "Sleep Mode"?

Discussion in 'Scripting' started by Fishypants, Mar 11, 2012.

  1. Fishypants

    Fishypants

    Joined:
    Jan 25, 2009
    Posts:
    444
    Hey guys, I asked this question in Support, but didn't get many replies. So here goes:

    Is there a way to detect if the user's computer goes into "Sleep Mode / Hibernate Mode" ? I believe on Mac's its called Power Saving Mode? I'm working on an online Webplayer game, and would like to detect this and have the player automatically disconnect instead of waiting for a timeout.

    I have been trying to use this:
    Code (csharp):
    1. using Microsoft.Win32;
    2.  
    3. void Awake(){
    4. Application.runInBackground = true;
    5. SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(Test);
    6. }
    7.  
    8. static void Test(object sender, PowerModeChangedEventArgs e){
    9. Debug.Log("Power Mode Changed!");
    10. }
    But, after reading into what Mono supports, it looks like it doesn't support PowerModeChanged event?

    If it does not, is there a work around for this? ANY information would be very much appreciated, thanks!
     
  2. Fishypants

    Fishypants

    Joined:
    Jan 25, 2009
    Posts:
    444
    Bump!
    Has no one run into this issue before? :(
     
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    you have to hook into the winapi if you want to get any windows events, they will not be provided through the mono framework and unity does not care about these events too in the form of exposing it but thats more due to when it is happening, then the 3d context gets the plug pulled too and unity will pause -> Unitys OnApplicationPause is fired (or if it gets killed then its OnApplicationQuit)
     
  4. Fishypants

    Fishypants

    Joined:
    Jan 25, 2009
    Posts:
    444
    Thanks for the info Dreamora.

    Hmmm, well I would love to avoid having to hook into the winapi at all if OnApplicationPause() gets fired when the computer goes into sleep / hibernate mode. I'm at work at the moment and cannot test this, but I will give it a shot when I get home to see if it does. If so then AWESOME. If not, then I'll have to research the winapi route. Thanks again!
     
  5. Fishypants

    Fishypants

    Joined:
    Jan 25, 2009
    Posts:
    444
    Hmmm, actually just tested it, and both Sleep and Hibernate (on PC) don't seem to fire OnApplicationPause() . . . bummer. So Dreamora, would you happen to have any reference as to how to hook into the winapi from Unity that I could read up on?
     
  6. Fishypants

    Fishypants

    Joined:
    Jan 25, 2009
    Posts:
    444
    Well, it seems like it COULD work, but I'm using smartfox server and running an online game in the webplayer, so I need to have "Run in Background" checked on . . . which doesn't seem to work.

    It's like it ignores everything until the computer goes into sleep mode and then it just stops without detecting it.
     
  7. Bren

    Bren

    Joined:
    Aug 28, 2008
    Posts:
    191
    Yeah, just confirmed this is the case. Unity sees oblivious to Windows going into sleep mode. Not even Time.time/TIme.deltaTime is aware.

    Also odd, Application.OnFocus() doesn't even get fired when you click on the Start button and enter Sleep mode manually...
     
  8. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
  9. sonnyb

    sonnyb

    Joined:
    Nov 5, 2013
    Posts:
    17
    On SystemEvents.PowerModeChanged

    I tested Microsoft.Win32.SystemEvents.PowerModeChanged - it can be called, but it doesn't actually fire. I tested this with these settings:
    • Unity 2018.3
    • Scripting Backend: Mono
    • Runtime: .NET 3.5 Equivalent (Deprecated)
    • Api Compatibility Level: .NET 2.0
    Workaround for Detecting Resume from Sleep

    I couldn't find a workaround for detecting "Entering Sleep", but I did find a workaround for detecting "Resume from Sleep" for applications that have Application.runInBackground project setting on. Unity's Time.unscaledDeltaTime (and Time.unscaledTime) will increase even if the PC is sleeping, so it's possible to detect when the PC has resumed.

    For more details, see the info I posted to the related Unity Answers thread: http://answers.unity.com/answers/1751795/view.html