Enable/Disable VR from code

后端 未结 3 1145
盖世英雄少女心
盖世英雄少女心 2020-11-27 07:29

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 \"

相关标签:
3条回答
  • 2020-11-27 07:52

    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.

    0 讨论(0)
  • 2020-11-27 08:00

    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();
    
    0 讨论(0)
  • 2020-11-27 08:05
     public void Awake() {
              StartCoroutine(SwitchToVR(()=>{
                    Debug.Log("Switched to VR Mode");
              }));
    
              //For disable VR Mode
              XRSettings.enabled = false;
      }
     
      IEnumerator SwitchToVR(Action callback) {
                // Device names are lowercase, as returned by `XRSettings.supportedDevices`.
                // Google original, makes you specify
                // string desiredDevice = "daydream"; // Or "cardboard".
                // XRSettings.LoadDeviceByName(desiredDevice);
                // this is slightly better;
                
                string[] Devices = new string[] { "daydream", "cardboard" };
                 XRSettings.LoadDeviceByName(Devices);
          
                // Must wait one frame after calling `XRSettings.LoadDeviceByName()`.
                yield return null;
              
                // Now it's ok to enable VR mode.
                XRSettings.enabled = true;
                callback.Invoke();
       }
    
    0 讨论(0)
提交回复
热议问题