- Home /
Unity to Detect when computer goes into "Sleep Mode"?
Hey everyone, so here is my problem. I am creating an online web player game, and the problem is the user can force their computer to go into sleep mode. What I would like to do is detect this in the web player and if they do this, auto kick them from the server instead of waiting for a timeout.
I'm using smartfox, and it doesn't appear to be able to handle this at all so I am hoping there is a solution in Unity.
I have looked into SystemEvents.PowerModeChanged, but it appears that this never fires? Or at least I don't know if I am implementing it correctly. This is what I have so far (not working)
using Microsoft.Win32;
void Awake(){
Application.runInBackground = true;
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(Test);
}
static void Test(object sender, PowerModeChangedEventArgs e){
Debug.Log("Power Mode Changed!");
}
Any ideas?
Answer by Fishypants · Mar 09, 2012 at 08:08 AM
Hmmm, after looking at this page, it appears that there is no way to query the PowerModeChanged from within the web player ... is this true? Is there really no way to detect if the user's computer went into sleep mode or hibernate mode??
Answer by sonnyb · Jul 16, 2020 at 05:40 PM
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. (Alternatively, DateTime.UtcNow
can be compared in Update()
to see if there is a big jump from the previous frame due to the PC being in Sleep.)
Limitations of the workaround
Note that it's possible for Time.unscaledDeltaTime
to be large for other reasons (PC stutters, long loading times, etc.), so it may not be appropriate to use this to rely on detecting very short sleep times.
(And if Application.runInBackground
is off, I think it may be possible for this approach to still work if MonoBehaviour.OnApplicationPause
is mixed into the logic, but I haven't tried to get this to work.)
This workaround was sufficient for my use case (where I am only concerned about detecting Resume and I only care about detecting long sleep times). But I'm not sure how robust it is and I've only tested briefly on Unity 2018.3 / Windows 10.
(Note: I also mentioned this post in the related Unity Forums thread)
Your answer
![](https://koobas.hobune.stream/wayback/20220613064846im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why can't I build Web Player in Unity while I have no problems with building standalone versions? 2 Answers
WebPlayer Build 1 Answer
FPS Webplayer problem 0 Answers
Web player performance problem on MAC OS X 3 Answers
Any caching options available for things dynamically loaded by WebPlayer? 0 Answers