- Home /
Stop Mobile screens turning off
How do I stop the screen turning off on an android. I can stop the screen going dim but not to stop turning it off. My game uses the accelerometer.
Thanks
Answer by Macarse · May 02, 2012 at 07:36 PM
You should use:
Screen.sleepTimeout = SleepTimeout.NeverSleep;
I don't have enough user points/karma/whatever to upvote, yet, but this is the real right answer. .NeverSleep didn't used to exist, but it does now, and you should use it.
It's not working for me, should I put it in Update() or is Start() fine?
Yea, as asked by stuartb, where should we put this line of code?
Within a Start function because we need to execute it just once (source: http://answers.unity3d.com/questions/348637/how-to-make-the-android-screen-always-turns-on.html)
Answer by Richard 3 · Feb 04, 2011 at 01:37 PM
this script does both
iPhoneSettings.screenCanDarken = false;
Answer by Grixen · Feb 16, 2016 at 01:01 PM
My Asus ignoring or forget sigle command and stoutly get screen twice darker after a minute. In some little expeiments ive got a solution:
Screen.sleepTimeout = (int)0f;
Screen.sleepTimeout = SleepTimeout.NeverSleep;
in void Update () section, somehow wakes it nonsop.
Duh, so this means that we should always force this value?
Does this work on iOS and Android or do I need to use iPhoneSettings.screenCanDarken?
Is it required to do the double assignment? Why are you using (int)0f ins$$anonymous$$d of just 0?
I think (int)SleepTimeout.NeverSleep equals to 0 as well.
But probably you're showing two alternative methods here, I'm not sure. Could you please explain? Thank you :)
Thanks for your answer, have same issue with Xiaomi phones. Using just NeverSleep line does not prevent dim$$anonymous$$g.
Answer by gokturk1 · Jul 19, 2020 at 11:51 PM
// Prevent screen dimming.
public const int NeverSleep = -1;
//
// Set the sleep timeout to whatever the user has specified in the system settings.
public const int SystemSetting = -2;
I found this on source code so if you want make it never sleep you should use -1 instead of 0 and -2 for system default value if you changed it to any different value. But I recomend Screen.sleepTimeout = SleepTimeout.NeverSleep; as Macarse said. You can hover over neverSleep to see -1 value by yourself . Just wanted to make it clear 0 is not the true answer for never sleep it is -1