Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Fishypants · Mar 07, 2012 at 11:49 PM · webplayer

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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??

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges