Version: 2019.2
混合现实设备
VR 设备

VR 概述

Unity VR 允许直接从 Unity 中以虚拟现实设备为目标,而无需项目中的任何外部插件。它提供了基本 API 和功能集,可兼容多种设备。还可为未来的设备和软件提供前向兼容性。

通过使用 Unity 中的原生 VR 支持,您将获得:

  • 每个 VR 设备的稳定版本
  • 用于与不同 VR 设备进行交互的单个 API 接口
  • 一个纯净的项目文件夹,无需每个设备的外部插件
  • 在应用程序中包含和切换多个设备的能力
  • 更高的性能(本机设备可以实现更低级别的 Unity 引擎优化)

启用 Unity VR 支持

To enable native VR support for your game builds and the Editor, open the Player Settings (menu: Edit > Project Settings > Player). Select XR Settings and check the Virtual Reality Supported checkbox. Set this for each build target. Enabling virtual reality support in a standalone build doesn’t also enable the support for Android (and vice-versa).

Use the Virtual Reality SDKs list below the checkbox to add and remove VR devices for each build target. The order of the list is the order in which Unity tries to enable VR devices at runtime. The first device that initializes properly is the one enabled. This list order is the same in the built Player.

通过 XR Settings 启用 Virtual Reality Support
通过 XR Settings 启用 Virtual Reality Support

构建的应用程序:选择启动设备

Your built application initializes and enables devices in the same order as the Virtual Reality SDKs list in XR Settings (see Enabling VR Support, above). Devices not present in the list at build time are not available in the final build; the exception to this is None. Device None is equivalent to a non-VR application (that is, a normal Unity application) and can be switched to during runtime without including it in the list.

如果在列表中包含 None 作为设备,则应用程序在尝试初始化 VR 设备之前可默认为非 VR 应用程序。如果将 None 放在列表顶部,则应用程序将在禁用 VR 的情况下启动。然后,可以通过脚本使用 XR.XRSettings.LoadDeviceByName 来启用和禁用列表中存在的 VR 设备。

如果尝试切换到的设备在初始化时失败,Unity 将禁用 VR 并让该设备仍设置为激活的 VR 设备。切换设备 (XRSettings.LoadDeviceByName) 或启用 XR (XR.XRSettings-enabled) 时,构建的应用程序会尝试再次初始化。

使用以下命令行参数来启动特定设备:

-vrmode DEVICETYPE

其中的 DEVICETYPE 是支持的 XR 支持设备列表中的名称之一。

示例:MyGame.exe -vrmode oculus

启用 VR 后时发生什么

在 Unity 中启用 VR 时,会自动执行以下操作:

自动渲染到头戴式显示器

场景中的所有摄像机都可以直接渲染到头戴式显示器 (HMD)。Unity 会自动调整视图和投影矩阵来适应头部跟踪、位置跟踪和视野。

可使用摄像机 (Camera) 组件的 stereoTargetEye 属性禁止渲染到 HMD。或者,也可使用 Target Texture 属性将摄像机设置为渲染到渲染纹理。

  • 使用 stereoTargetEye 属性可将摄像机设置为仅将特定眼睛渲染到 HMD。这种设置对于呈现狙击镜或立体视频等特殊效果非常有用。要实现此目的,请在场景中添加两个摄像机:一个瞄准左眼,另一个瞄准右眼。通过设置层遮罩来配置 Unity 发送到每只眼睛的内容。

自动头部跟踪输入

Unity versions newer than 2019.1 no longer support automatic head tracking for head-mounted devices. Use the Tracked Pose Driver feature of the XR Legacy Input Helpers package to implement this functionality.

You can install the XR Legacy Input Helpers package using the Package Manager window.

了解摄像机

Unity 使用头部跟踪姿势对摄像机变换进行覆盖。要移动或旋转摄像机,请将其附加为另一个游戏对象的子游戏对象。这样一来,所有对摄像机父项进行的变换更改都会影响摄像机本身。使用脚本移动或旋转摄像机也是如此。

Childing the Main Camera to a GameObject - in this case, a camera container
Childing the Main Camera to a GameObject - in this case, a camera container

可以将摄像机的位置和方向想象为用户在中立位置观察的位置。

坐姿和房间规模 VR 体验之间存在差异:

  • 如果设备支持房间规模体验,则摄像机的起始位置是用户游戏空间的中心位置。
  • 采用坐姿体验时,可使用 XR.InputTracking.Recenter() 将摄像机重置为中立位置。

渲染到设备的每个摄像机会自动使用用户在每个 VR SDK 的软件设置中输入的 FOV 替换摄像机的 FOV。用户无法在运行时更改视野,因为目前已知此行为会导致晕动症。

Editor 模式

如果 VR 设备支持 Unity Editor 模式,请在 Editor 中按 Play 直接在设备上进行测试。

如果已将 stereoTargetEye 设置为左眼或双眼,则左眼将渲染到 Game 视图窗口。如果已将 stereoTargetEye 设置为右眼,则将渲染右眼。

没有左眼和右眼的自动并排视图。要在 Game 视图中查看并排视图,请创建两个摄像机,将一个摄像机设置为左眼,另一个设置为右眼,并设置视口来并排显示它们。

请注意,在 Editor 中运行会产生开销,因为 Unity 集成开发环境 (IDE) 需要渲染每个窗口,因此可能会遇到延迟或抖动。要减少 Editor 渲染开销,请打开 Game 视图并启用 Maximize on Play

Unity Profiler 是一个有用的工具,可用于了解在 Editor 外运行时的性能。但是,性能分析器 (Profiler) 本身也有开销。检查游戏性能的最佳方法是在目标平台上进行构建并直接运行。运行非开发版本时可以看到最佳性能,但开发版本允许连接 Unity Profiler 来进行更好的性能分析。

在 Unity 中进行 VR 开发的硬件和软件建议

硬件

实现与目标 HMD 类似的帧率对于获得良好的 VR 体验至关重要。此帧率必须与 HMD 中使用的显示器的刷新率相匹配。如果帧率低于 HMD 的刷新率,则会特别明显,并通常导致玩家恶心。

下表列出了常见 VR 头盔的设备刷新率:

VR 设备 刷新率
Gear VR 60hz
Oculus Rift 90hz
Vive 90hz

软件

  • Windows:Windows 7、8、8.1 和 Windows 10 全都兼容。
  • Android: Android 5.1 (Lollipop) or higher.
  • macOS:
  • Oculus: macOS 10.9+ with the Oculus 0.5.0.1 runtime. However, Oculus have paused development for macOS, so use Windows for native VR functionality in Unity.
  • HTC Vive: macOS 10.13.
  • 显卡驱动程序:确保驱动程序是最新的。每种设备都在不断推出最新的驱动程序,因此可能不支持旧驱动程序。

设备运行时要求

每个 VR 设备都要求在计算机上安装适当的运行时。例如,要在 Unity 中开发和运行 Oculus,必须在计算机上安装 Oculus 运行时(也称为 Oculus Home)。对于 Vive,则需要安装 Steam 和 SteamVR。

根据所使用的 Unity 版本,Unity 支持的每个特定设备的运行时版本可能会有所不同。您可以在 Unity 每个主要和次要版本的发行说明中找到运行时版本。

在推出某些版本更新时,不再支持以前的运行时版本。这意味着原生 Unity VR 支持不再适用于早期的运行时版本,但继续适用于新的运行时版本。

Unity 原生 VR 支持不会从项目文件夹中读取插件,因此如果启用了 VR 支持,则将早期版本的插件与原生支持包含在一起将导致插件失效。如果希望将早期版本的插件与不再支持该版本的 Unity 版本结合使用,请禁用 Native VR Support__(选择 XR Settings__ 并取消选中 __Virtual Reality Supported__)。然后,即可像任何其他第三方插件一样访问该插件。有关更多详细信息,请参阅上文关于启用 Unity VR 支持的部分。


  • Automatic head tracking support removed in 2019.1.

NewIn20191

混合现实设备
VR 设备