通过代码启用/禁用VR [英] Enable/Disable VR from code

查看:703
本文介绍了通过代码启用/禁用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.

推荐答案

在顶部包含using UnityEngine.XR;.

在启动函数中使用空字符串调用XRSettings.LoadDeviceByName("")并后按XRSettings.enabled = false;禁用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;.

您应该在每个函数调用之间等待一帧.这就需要完成一个corutine函数.

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()禁用它.如果您使用的不是白日梦,请将该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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆