Enable/Disable VR from code

13,776

Solution 1

Include using UnityEngine.XR; at the top.

Call XRSettings.LoadDeviceByName("") with empty string followed by XRSettings.enabled = false; to disable VR in the start function to disable VR.

When you want to enable it later on, call XRSettings.LoadDeviceByName("daydream") with the VR name followed by XRSettings.enabled = true;.

You should wait for a frame between each function call. That requires this to be done a corutine function.

Also, On some VR devices, you must go to Edit->Project Settings->Player and make sure that Virtual Reality Supported check-box is checked(true) before this will work. Then you can disable it in the Start function and enable it whenever you want.

EDIT:

This is known to work on some VR devices and not all VR devices. Although, it should work on Daydream VR. Complete code sample:

IEnumerator LoadDevice(string newDevice, bool enable)
{
    XRSettings.LoadDeviceByName(newDevice);
    yield return null;
    XRSettings.enabled = enable;
}

void EnableVR()
{
    StartCoroutine(LoadDevice("daydream", true));
}

void DisableVR()
{
    StartCoroutine(LoadDevice("", false));
}

Call EnableVR() to enable vr and DisableVR() to disable it. If you are using anything other than daydream, pass the name of that VR device to the LoadDevice function in the EnableVR() function.

Solution 2

For newer builds of Unity (e.g. 2019.4.0f1) you can use the XR Plugin Management package.

To enable call:

XRGeneralSettings.Instance.Manager.InitializeLoader();

To disable call:

XRGeneralSettings.Instance.Manager.DeinitializeLoader();
Share:
13,776
Sofia Clover
Author by

Sofia Clover

Updated on July 24, 2022

Comments

  • Sofia Clover
    Sofia Clover almost 2 years

    How can I set the display to stereoscopic programmatically in Unity for an app deployed to an Android device?

    I want a UI menu where the user can toggle between "VR mode" and normal mode. I do not want VR mode by default as it should be an option at run-time. I know there is a setting for "Virtual Reality Supported" in the build settings, but again, I do not want this enabled by default.

  • Sofia Clover
    Sofia Clover about 8 years
    I tried that and it did not do anything. Is there somewhere specific this snippet to enable it must go ?
  • Programmer
    Programmer about 8 years
    Nope. What Unity version are you using?
  • Programmer
    Programmer about 8 years
    This should work for that version. According to Unity, it wont work in the Editor. It will work in Standalone builds. Try it.
  • Sofia Clover
    Sofia Clover about 8 years
    I tried enabling it in Awake() and then logged the value for UnityEngine.VR.VRSettings.enabled and it was still false. Like the setting did not take.
  • Programmer
    Programmer about 8 years
    Edited my answer. Take a look at the edit. Also don't try this in the Editor. Try it for a build and it should work. This works here as I am typing this.
  • Programmer
    Programmer about 8 years
  • Fattie
    Fattie over 7 years
    You know I actually can not get that to work, @Programmer. (I have an unusual app that can launch either as an ordinary app, or, as the VR app. I'll probably have to split it in two.) For me this is on Oculus.
  • Programmer
    Programmer over 7 years
    Hi Joe, long time.....This didn't work for you? Try ` VRSettings.loadedDevice = VRDeviceType.None;. Also try VRSettings.LoadDeviceByName ("");` and let me know. Let me know
  • Programmer
    Programmer over 7 years
    @JoeBlow Did you try that? I want to know if this works or not
  • Fattie
    Fattie over 7 years
    long time ... you're right it's been like 10 days which is 5 years in software years!! :) hang on I will try that tomorrow 4 sure ........ sorry didn't see your msg here ...
  • Programmer
    Programmer over 7 years
    @JoeBlow That's fine. Take your time. I just wanted to make sure you saw my message.
  • Captain GouLash
    Captain GouLash over 3 years
    Finally the correct answer. Thank you for this. Here is the documentation: docs.unity3d.com/Packages/[email protected]/api/…
  • Captain GouLash
    Captain GouLash over 3 years
    And for anyone facing the same problem, you need to include this line first: using UnityEngine.XR.Management;