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?

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