Mixed Reality Devices
VR 设备

VR 概述

Unity VR lets you target virtual reality devices directly from Unity, without any external plug-ins in projects. It provides a base API and feature set with compatibility for multiple devices. It provides forward compatibility for future devices and software.

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

  • 每个 VR 设备的稳定版本
  • 用于与不同 VR 设备进行交互的单个 API 接口
  • 一个纯净的项目文件夹,无需每个设备的外部插件
  • 在应用程序中包含和切换多个设备的能力
  • Better performance (Lower-level Unity engine optimizations are possible for native devices)

启用 Unity VR 支持

要为您的游戏版本和 Editor 启用原生 VR 支持,请打开 Player Settings(菜单:__Edit__ > Project Settings > Player__)。选择 XR Settings__ 并选中 Virtual Reality Supported 复选框。对每个构建目标都进行此设置。在独立构建中启用虚拟现实支持不会也为 Android 启用该支持(反之亦然)。

从 Unity Editor 导航到 Player Settings
从 Unity Editor 导航到 Player Settings

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 that 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

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

构建的应用程序按照与 XR Settings 中的 Virtual Reality SDKs 列表相同的顺序来初始化和启用设备(请参阅上文的启用 VR 支持)。在构建时未出现在列表中的设备在最终构建中不可用。此原则的例外是 None__;设备 None__ 相当于非 VR 应用程序(即普通的 Unity 应用程序),即使不包含在列表中也可在运行时切换到该类型。

If you include None as a device in the list, the application can default to a non-VR application before it attempts to initialize a VR device. If you place None at the top of the list, the application starts with VR disabled. Then, you can then enable and disable VR devices that are present in your list through script using XR.XRSettings.LoadDeviceByName.

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

Use the following command line argument to launch a specific device:

-vrmode DEVICETYPE

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

示例:MyGame.exe -vrmode oculus

启用 VR 后时发生什么

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

自动渲染到头戴式显示器

All Cameras in your Scene can render directly to the head-mounted display (HMD). Unity automatically adjusts View and Projection matrices to account for head tracking, positional tracking and field of view.

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

  • Use the stereoTargetEye property to set the Camera to only render a specific eye to the HMD. This is useful for special effects such as a sniper scope or stereoscopic videos. To achieve this, add two Cameras to the Scene: one targeting the left eye, the other targeting the right eye. Set layer masks to configure what Unity sends to each eye.

自动头部跟踪输入

Unity automatically applies head tracking and the appropriate field of view (FOV) to the Camera if the target device is head-mounted. You can manually set the FOV to a specific value, but you cannot set the Camera’s transform values directly. See the section below on Understanding the Camera to learn more.

Automatic head tracking and positional tracking ensures that the position and orientation most closely match the user’s position and orientation before Unity renders the frame. This gives a good VR experience, and prevents the user from experiencing nausea.

Understand the Camera

Unity overrides The Camera Transform with the head-tracked pose. To move or rotate the Camera, attach it as a child GameObject of another GameObject. This makes it so that all Transform changes to the parent of the Camera affect the Camera itself. This also applies to moving or rotating the camera using a script.

将主摄像机作为一个游戏对象的子项
将主摄像机作为一个游戏对象的子项

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

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

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

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

Editor 模式

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

The left eye renders to the Game View window if you have stereoTargetEye set to left or both. The right eye renders if you have stereoTargetEye set to right.

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

Note that there is overhead to running in the Editor, because Unity IDE needs to render each window, so you might experience lag or judder. To reduce editor rendering overhead, open the Game View and enable Maximize on Play.

The Unity Profiler is a helpful tool to get an idea of what your performance should be like when it runs outside of the Editor. However, the profiler itself also has overhead. The best way to review game performance is to create a build on your target platform and run it directly. You can see the best performance when you run a non-development build, but development builds allow you to connect the Unity profiler for better performance profiling.

在 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 OS Lollipop 5.1 或更高版本。

  • OS X:OSX 10.9+ 以及 Oculus 0.5.0.1 运行时。但是,Oculus 暂停了 OS X 的相关开发,因此在 Unity 中使用 Windows 提供原生 VR 功能。

  • 显卡驱动程序:确保驱动程序是最新的。每种设备都在不断推出最新的驱动程序,因此可能不支持旧驱动程序。

设备运行时要求

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

Depending on what version of Unity you are using, the runtime versions for each specific device that Unity supports may differ. You can find runtime versions in the release notes of each major and minor Unity release.

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

Unity native VR support does not read plug-ins from within your project’s folder, so including earlier versions of the plug-in with native support fails if you have VR support enabled. If you wish to use an earlier version with a release of Unity that no longer supports that version, disable Native VR Support (go to XR Settings and uncheck Virtual Reality Supported). You can then access the plug-in like any other 3rd party plugin. See the section above on Enabling Unity VR Support for more details.

Mixed Reality Devices
VR 设备