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

En/Disable Virtual Reality through code

Discussion in 'AR/VR (XR) Discussion' started by rob_vld, Feb 22, 2018.

  1. rob_vld

    rob_vld

    Joined:
    Jul 9, 2013
    Posts:
    187
    UnityEngine.VR.VRSettings.enabled = false;
    http://docs.unity3d.com/ScriptReference/VR.VRSettings-enabled.html

    This only works for the editor, why can't we software disable it for builds too ?

    I need both desktop as vr builds to run along side my editor to see whats happening on the network... this really slows down my workflow...

    Who can i address for my request?
     
  2. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    958
    Umm... not sure what you mean there. That works in editor and at runtime. There is nothing to disable or enable at build time so not clear on the issue you are having?
     
  3. phonk

    phonk

    Joined:
    Dec 20, 2014
    Posts:
    17
    Not sure if this answers your question, I´m making a GoogleVR app and I was struggling enabling and disabling VR through script, and after some loooong hours I found the solution... (unity 2017.3.0f3)

    I needed an intro scene with just a UI, and a second scene with GoogleVR. (So somehow I had to force LeftLandscape orientation here aswell...). I found out VRSettings.enabled didn´t work, apparently now you need to use XRSettings.enabled

    I´m quite a noob, will always be i´m afraid... But I hope I´m finally able to answer a question ;) If not my sincere apologies..

    -In player XR settings add both None and Cardboard (in that order)
    -Add StopVR.cs to an empty GameObject in Scene 1 (the "normal" scene)
    -Add StartVR.cs to an empty GameObject in Scene 2 (the GoogleVR scene)

    Maybe/probably this can be made into one script, but this is how I solved it...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.XR;
    6.  
    7. public class StopVR : MonoBehaviour {
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         Screen.orientation = ScreenOrientation.AutoRotation ;
    12.         XRSettings.enabled = false;
    13.     }
    14.  
    15. }
    16.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR;
    5.  
    6. public class StartVR : MonoBehaviour {
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         Screen.orientation = ScreenOrientation.LandscapeLeft;
    11.         StartCoroutine(LoadDevice("cardboard"));
    12.         XRSettings.enabled = true;
    13.     }
    14.  
    15.     IEnumerator LoadDevice(string newDevice)
    16.     {
    17.         XRSettings.LoadDeviceByName(newDevice);
    18.         yield return null;
    19.         Screen.orientation = ScreenOrientation.LandscapeLeft;
    20.         XRSettings.enabled = true;
    21.     }
    22.  
    23.  
    24. }
    25.  
     
    mahmoudabdelhamid336 likes this.
  4. scvnathan

    scvnathan

    Joined:
    Apr 29, 2016
    Posts:
    75
  5. cwoh

    cwoh

    Joined:
    Oct 6, 2015
    Posts:
    17
    Seems like you are facing ??? the same Problem as me , i start via a script (XRSettings.enabled = true;)
    this works in the edtor but NOT during runtime, it makes me angry that Unity is messing soo much with the stereo fkt. and from version to version it always failed. 4->5 5->2017 ??????????? 2017->2018 ????????????????
     
  6. Gruguir

    Gruguir

    Joined:
    Nov 30, 2010
    Posts:
    340
    It definitely works. May take some time to get used to tho. They are many ressources on this forum section and in the doc.
     
  7. cwoh

    cwoh

    Joined:
    Oct 6, 2015
    Posts:
    17
    Yes i hope so but the problem is new version --> update code this is what makes me angry ? You cannot ( not always) go back (yes make backup of all your projects) even you make a package.