本文介绍了从代码启用/禁用 VR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于部署到 Android 设备的应用,如何在 Unity 中以编程方式将显示设置为立体?

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

我想要一个 UI 菜单,用户可以在其中在VR 模式"和正常模式之间切换.默认情况下我不想要 VR 模式,因为它应该是运行时的一个选项.我知道在构建设置中有一个支持虚拟现实"的设置,但同样,我不希望默认启用它.

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.

推荐答案

Include using UnityEngine.XR; 在顶部.

Include using UnityEngine.XR; at the top.

使用空字符串调用XRSettings.LoadDeviceByName(""),后跟XRSettings.enabled = false;在启动函数中禁用VR以禁用VR.

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

如果您想稍后启用它,请使用 VR 名称后跟 XRSettings.enabled = true; 调用 XRSettings.LoadDeviceByName("daydream").

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.

此外,在某些 VR 设备上,您必须转到编辑->项目设置->播放器并确保支持虚拟现实复选框在此之前选中(真)才能起作用.然后您可以在开始功能中禁用它并随时启用它.

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.

编辑:

众所周知,这适用于某些 VR 设备,而不是所有 VR 设备.不过,它应该适用于 Daydream VR.完整的代码示例:

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));
}

调用 EnableVR() 启用 vr 和 DisableVR() 禁用它.如果您使用的是 daydream 以外的任何东西,请将该 VR 设备的名称传递给 EnableVR() 函数中的 LoadDevice 函数.

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.

这篇关于从代码启用/禁用 VR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 14:28