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
3
Question by Arrow · Feb 20, 2013 at 03:31 PM · androidmobilescreensleep

How do I reset Screen.sleepTimeout's user input timer?

I made a little game, and this game has three main components: a main menu, a level where the primary user control is tilt (gyro), and a high score screen.

In the level, I use:

 Screen.sleepTimeout = SleepTimeout.NeverSleep;

to keep the user's screen from dimming while they're playing. I'd like to allow the screen to dim if the user leaves their phone on the main menu or high score screen, so I return the timeout to the user's usual settings:

 Screen.sleepTimeout = SleepTimeout.SystemSetting;

There's no way to get to the main menu without user input, so the user touches the menu button and then, if the linger there for SleepTimeout.SystemSetting seconds, the screen dims. Perfect.

But the game rolls from the level to the high score screen on its own, and when I set .sleepTimeout to SleepTimeout.SystemSetting, the screen instantly goes black! I assume this is because the timer doesn't restart just because I've loaded a new level, and because I haven't had user input for the entire duration of my game level ( > than the 60 seconds I have as the default sleep time on my test machine) going back to system settings tells the screen to dim.

So:

Is there any way I can 'reset' the user input timer, so I can have it start counting fresh right before I set the .sleepTimeout back to system settings?

Or is there some way I can fake a user input event so I can reset the timer that way?

Comment
Add comment · Show 3
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 Andy-Block · Mar 15, 2013 at 09:11 AM 0
Share

I don't have an answer, I'm afraid, but am encountering the same problem. Did you find a solution?

avatar image Bunny83 · Mar 15, 2013 at 09:39 AM 0
Share
  • Yes this is a problem and i don't think that Unity rovides a solution for this. $$anonymous$$aybe there's a way when using a native code plugin, however i'm not into native mobile development, so i'm not sure if it is even possible from the OS point of view.

Anyway, good question.

ps: a simple "fix" is to force a user interaction by displaying a dialog at the end of the game before you turn back on the sleepTimeout.

avatar image Arrow · Mar 15, 2013 at 12:15 PM 0
Share

I did not find a real solution. I have a workaround in place right now like Bunny83's - force the user to interact in some way before entering states/levels that allow the screen to sleep.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Andy-Block · Mar 20, 2013 at 04:16 PM

We've actually found a better workaround for our purposes - this does not require user input, but does not respect the system-defined timeout (the timeout is hardcoded). The idea is to leave the sleep timeout as SystemSetting until that timeout has elapsed; setting it back to SystemSetting is taken care of by a Coroutine.

    const int revertSleepTime = 120;
    int screenLockPreventCount = 0;
    bool revertSleepTimeoutToSystemSettingPending = false;

    public void PushScreenLockPrevent() {
            if (++screenLockPreventCount == 1) {
                    if (revertSleepTimeoutToSystemSettingPending) {
                            StopCoroutine("RevertSleepTimeoutToSystemSetting");
                            revertSleepTimeoutToSystemSettingPending = false;
                    }
                    Screen.sleepTimeout = SleepTimeout.NeverSleep;
            }
    }
 
    public void PopScreenLockPrevent() {
            if (--screenLockPreventCount == 0) {
                    revertSleepTimeoutToSystemSettingPending = true;
                    // Tried just setting 'Screen.sleepTimeout = SleepTimeout.SystemSetting' here. However, that resulted 
                    // in immediate sleep timeout, as there had been no input for the period specified by the system
                    // setting. So we instead revert to the system setting after some period has elapsed via this coroutine:
                    StartCoroutine("RevertSleepTimeoutToSystemSetting");
            }
    }

    public IEnumerator RevertSleepTimeoutToSystemSetting() {               
            // What we really want to do here is:
            //    yield return new WaitForSeconds(revertSleepTime);
            // However, this does not work for some reason (at least not for us, on Android). We never get invoked again after the
            // yield. Doing it using 'yield return null' instead seems to resolve the problem.
            float waitUntil = Time.realtimeSinceStartup + revertSleepTime;
            while (Time.realtimeSinceStartup < waitUntil) {
                    yield return null;
            }
            Screen.sleepTimeout = SleepTimeout.SystemSetting;
            revertSleepTimeoutToSystemSettingPending = false;
    }

Hope this helps someone. Regards,

 Andy
Comment
Add comment · Show 1 · 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 sampenguin · Mar 21, 2013 at 04:24 PM 0
Share

Great ti$$anonymous$$g Andy, I just ran into this exact same problem. Gonna try your solution.

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I get Unity to stop making changes to AndroidManifest.xml? 2 Answers

Screen sleeping messes with my game. 0 Answers

Mobile FPS Help 0 Answers

How do I config NGUI screen to fit Mobile device? 2 Answers

Android Communication 1 Answer


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