Search Unity

How do I stop my screen from going to sleep while in VR??

Discussion in 'Daydream' started by Tuitive, Oct 21, 2016.

  1. Tuitive

    Tuitive

    Joined:
    Mar 25, 2013
    Posts:
    36
    I followed the settings at https://developers.google.com/vr/daydream/dev-kit-setup -- I thought Do Not Disturb access for Google VR Services would take care of this -- but my screen just shuts off according to the regaulr Sleep settings. My expectation was that VR would override this and stay "awake" but it's not doing that. I had this issue on both my Nexus 6P as well as the new Pixel XL phone that I just arrived today.
     
  2. Tuitive

    Tuitive

    Joined:
    Mar 25, 2013
    Posts:
    36
    Just realized something: my Pixel phone has no idea when it's placed inside a non-Daydream headset, which is all I have until Daydream View ships in a few weeks. I understand those have an NFC chip that tells the phone to kick into VR, and I assume this will also tell the phone to keep its screen awake. (Am I right, Google?)
     
  3. thep3000

    thep3000

    Unity Technologies

    Joined:
    Aug 9, 2013
    Posts:
    400
    I think the Google plugin sets Screen.sleepTimeout to zero. iirc google didn't want us to do this by default, because in the future it may be up to the OS to control that. @mira_leung can you confirm?
     
  4. mira_leung

    mira_leung

    Official Google Employee

    Joined:
    May 17, 2016
    Posts:
    70
    Screen.sleepTimeout is set to Never in GvrController.cs - if that ever moves to the OS, we'll change accordingly.
     
    Tuitive likes this.
  5. jonathanhook

    jonathanhook

    Joined:
    Feb 11, 2015
    Posts:
    10
    Hi,

    We're having the opposite problem. We've got a Google VR app that's being deployed in a museum. We'd like it to go to sleep when not being used. We've written code (see below) that changes the sleepTimeout in Unity to be under the control of the OS when the device hasn't been moved for a while. However, whatever we do we can't seem to get the phone to goto sleep -- it always seems to stick in NeverSleep mode.

    I've posted the code below. However, we've tried even simpler scripts (e.g. Screen.sleepTimeout = SleepTimeout.SystemSetting in the Update function) and it seems to be impossible to take control of screen sleeping. We've got the OS sleep time setting set to 15 secs, and it works in other apps.

    Thanks for your help in advance,

    Jon

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ScreenSleep : MotionDetector
    5. {
    6.     public float sensitivity = 0.1f;
    7.     public float timeToSleep = 60.0f;
    8.  
    9.     private float lastMovement;
    10.     private bool awake;
    11.  
    12.     void Start ()
    13.     {
    14.         awake = true;
    15.         Screen.sleepTimeout = SleepTimeout.NeverSleep;
    16.         lastMovement = Time.timeSinceLevelLoad;
    17.     }
    18.  
    19.     void Update ()
    20.     {
    21.         base.Update ();
    22.  
    23.         if(hp.magnitude > sensitivity)
    24.         {
    25.             if (awake)
    26.             {
    27.                 lastMovement = Time.timeSinceLevelLoad;
    28.             }
    29.             else
    30.             {
    31.                 awake = true;
    32.                 Screen.sleepTimeout = SleepTimeout.NeverSleep;
    33.             }
    34.         }
    35.         else  if (awake && Time.timeSinceLevelLoad - lastMovement > timeToSleep)
    36.         {
    37.             awake = false;
    38.             Screen.sleepTimeout = SleepTimeout.SystemSetting;
    39.         }
    40.     }
    41. }
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class MotionDetector : MonoBehaviour
    7. {
    8.     public float hpSensitivity = 0.1f;
    9.     public Text debug;
    10.     public bool showDebug;
    11.  
    12.     protected Vector3 lp;
    13.     protected Vector3 hp;
    14.  
    15.     void Start ()
    16.     {
    17.         hp = new Vector3 (0.0f, 0.0f, 0.0f);
    18.         debug.text = "";
    19.     }
    20.  
    21.     protected virtual void Update ()
    22.     {
    23.         Vector3 a = Input.acceleration;
    24.         lp.x = ((1.0f - hpSensitivity) * lp.x) + (hpSensitivity * a.x);
    25.         lp.y = ((1.0f - hpSensitivity) * lp.y) + (hpSensitivity * a.y);
    26.         lp.z = ((1.0f - hpSensitivity) * lp.z) + (hpSensitivity * a.z);
    27.  
    28.         hp.x = a.x - lp.x;
    29.         hp.x = a.y - lp.y;
    30.         hp.x = a.z - lp.z;
    31.  
    32.         if (showDebug)
    33.         {
    34.             string msg = string.Format ("{0}, {1}, {2}, {3}", hp.x, hp.y, hp.z, hp.magnitude);
    35.             debug.text = msg;
    36.         }
    37.     }
    38. }
    39.