How to detect screen timeout on mobile?
I want to detect if the screen of a mobile device is "timed out", or so to say dimmed. This is to prevent involuntary actions from the player if tapping to "un-dim" the screen.
Right now I am using a timer to estimate the moment when the mobile device triggers the timeout state:
float startTime;
void Start () {
startTime = 0;
}
void Update () {
if (Screen.sleepTimeout > 0) {
if (Input.anyKey) {
startTime = Time.unscaledTime;
} else {
if (Time.unscaledTime > startTime + Screen.sleepTimeout) {
print ("The mobile screen is dimmed !");
}
}
}
}
However in practice the timing is off several seconds. For instance, I set a phone's timeout to 15 seconds, but it dims around 12 seconds after the last action detected.
There is probably a simple variable somewhere containing that information, but I have not been able to find it.
Thanks in advance :) !
Comment