Pages

Thursday, January 3, 2013

Unity3d - Screen.sleepTimeout

So I've been working on one of my games that was originally a game jam for FGL, and fleshing it out to distribute it somewhere. I'm not sure where yet, because It's more about using it to learn more about Unity.

Part of that process it to deploy it to as many different platforms as I can. Currently running a Unity 4 trial(said it was extended trial... whatever that means) so I can push out to mobile as well. I only have an android device to test with, nothing on the iOS yet.

One of the issues with the game I'm developing is that it uses the accelerometer on the phone to control the character. This doesn't count as 'activity' therefore it doesn't trip the screen sleep timer. Unity provides some mobile code to handle this.

Now onto the real meat of my question.


Screen.sleepTimeout = SleepTimeout.NeverSleep;
The code above what you use to make the screen not go to sleep. My game has two scenes in it which consist of scene 0 being the menu, and scene 1 being the actual game. The command works great during gameplay... no timeout or dimming of the screen. The problem comes in when the level ends the scene switches back to the menu and loads the score. I have code called during that time that puts the following:

 void Start()
{
Screen.sleepTimeout = 60;
}

Looking at the code reference this 'int' should specify the amount of time to give before the timeout happens. Instead of adhering to this I don't see my onGUI the screen just goes blank. It's almost like the sleepTimeout just kicks in ignoring what I put. I also tried putting the following:


void Start()
{
Screen.sleepTimeout = SleepTimeout.SystemSetting;
}

My thought was this would set it to the default for the phone, which would then start counting down from whatever was set. This didn't seem to make a difference.

I'm curious if anyone else has experienced this and found a workaround. Maybe some kind of timer that switches from NeverSleep to the SystemSetting after a given amount of time... which is what the int value is supposed to do!!

3 comments:

  1. I believe that this code does what you (and I) think it does. It sets the timeout back to the system setting (or 60 seconds, as in your first example).

    Well, why does the screen go black, then? My current hypothesis is that since there hasn't been any "user input" (non-gyro) for the duration of the level + the time it took to open the menu, changing the sleep settings just gives the device permission to sleep! Changing that setting doesn't refresh the timer.

    I'm looking for a way to solve this now - once I do I'll come back and comment.

    ReplyDelete
  2. Note that the docs state "Currently you will only be able to set this property to one of the values predefined in [SleepTimeout] class. A get will return either one of the predefined values, or the actual number of seconds until screen gets dimmed, as specified in system preferences of the device". I guess that is why 'Screen.sleepTimeout = 60;' doesn't work.

    ReplyDelete
  3. private float sleepTimer = 10f;
    if(Screen.sleepTimeout == SleepTimeout.NeverSleep){
    sleepTimer -= Time.deltaTime;
    if(sleepTimer<0){
    sleepTimer = 10f;
    Screen.sleepTimeout = SleepTimeout.SystemSetting;
    }
    }

    This way at least it will give the user 10 seconds leeway ;)

    ReplyDelete