Enable/Disable VR from code

匿名 (未验证) 提交于 2019-12-03 03:10:03

问题:

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.

回答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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!