Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Enable/Disable SteamVR on focus gain/loss

Discussion in 'AR/VR (XR) Discussion' started by AirRic, Jan 2, 2018.

  1. AirRic

    AirRic

    Joined:
    Apr 10, 2015
    Posts:
    4
    Hi @ all,

    to be able to run multiple instances of a VR game (for networking), I want to enable VR on window focus and disable it on focus loss. In Player Settings > XR, I disable VR support. In a script, I call the following Update()-Method to enable VR support at runtime:

    Code (CSharp):
    1. void Update () {
    2.         if(!Application.isFocused)
    3.         {
    4.             if(!hasFocus) return;
    5.             XRSettings.enabled = false;
    6.             hasFocus = false;
    7.             Debug.Log("Application lost focus");
    8.         }
    9.         else
    10.         {
    11.             if(hasFocus) return;
    12.             XRSettings.enabled = true;
    13.             hasFocus = true;
    14.             Debug.Log("Application gained focus");
    15.         }
    16.     }
    This works very well in editor, but if I try it via Build & Run, it won't start the VR camera.

    Any ideas where the error could be? Or any other approach to achieve this behavior? Many thanks in advance.
     
  2. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    Check if you have the "None" VR SDK added on your player settings, also see this https://docs.unity3d.com/ScriptReference/XR.XRSettings.LoadDeviceByName.html

    And use this code, we are using it at the office, its from someone else but i don't remember where we got it
    Code (CSharp):
    1. IEnumerator SwitchToVR()
    2.     {
    3.         VRSettings.LoadDeviceByName("cardboard"); // Or "cardboard" (both lowercase).
    4.  
    5.         // Wait one frame!
    6.         yield return null;
    7.  
    8.         // Now it's ok to enable VR mode.
    9.         VRSettings.enabled = true;
    10.     }
    11.     IEnumerator SwitchOutOfVr()
    12.     {
    13.         VRSettings.LoadDeviceByName(""); // Empty string loads the "None" device.
    14.  
    15.         // Wait one frame!
    16.         yield return null;
    17.  
    18.  
    19.     }
     
  3. AirRic

    AirRic

    Joined:
    Apr 10, 2015
    Posts:
    4
    Sorry for the late answer. Works perfectly, thank you very much.

    If anyone's interested in the whole code:

    Code (CSharp):
    1. void Update () {
    2.         if(!Application.isFocused)
    3.         {
    4.             if(!hasFocus) return;
    5.             StartCoroutine(SwitchOutOfVr());
    6.             hasFocus = false;
    7.             Debug.Log("Application lost focus");
    8.         }
    9.         else
    10.         {
    11.             if(hasFocus) return;
    12.             Debug.Log(xrdevice);
    13.             StartCoroutine(SwitchToVR());
    14.             hasFocus = true;
    15.             Debug.Log("Application gained focus");
    16.         }
    17.     }
    18.  
    19. IEnumerator SwitchToVR() {
    20.         XRSettings.LoadDeviceByName("OpenVR"); // Or whatever device you wanna use
    21.  
    22.         // Wait one frame!
    23.         yield return null;
    24.  
    25.         // Now it's ok to enable VR mode.
    26.         XRSettings.enabled = true;
    27.     }
    28.  
    29. IEnumerator SwitchOutOfVr() {
    30.         XRSettings.LoadDeviceByName(""); // Empty string loads the "None" device.
    31.  
    32.         // Wait one frame!
    33.         yield return null;
    34.  
    35.  
    36.     }
     
    ireth_86 and whileBreak like this.
  4. ireth_86

    ireth_86

    Joined:
    May 18, 2016
    Posts:
    23
    I can't make it work with newest SteamVR 2.0.1! It keeps calling update functions on poses and actions even after disabling VR.
    Anyone who experienced something similar?
     
    icdb and inod_clement like this.