Unity 2020 Virtual Reality Projects
上QQ阅读APP看书,第一时间看更新

Building for Immersive WindowsMR

This section describes how to set up and build your VR project so that it runs on a Windows Mixed Reality (WMR) immersive VR headset. This allows it to be run on Windows 10. You can choose to target Standalone / Windows in Build Settings or Universal Windows Platform for development. (Alternatively, you can build for OpenVR and run your WMR device in SteamVR, in which case, follow the instructions in that section instead). However, to publish to the Windows Store, you must build to target Universal Windows Platform.

Microsoft's 3D media Mixed Reality strategy is used to support a spectrum of devices and applications, from virtual reality to augmented reality. This book and our projects are about VR. At the other end of this spectrum is the Microsoft HoloLens wearable AR device. Developing for MR requires that you're using a Windows 10 PC and have Visual Studio installed. At the time of writing, the minimum requirement is Visual Studio 2019 (16.2 or higher). See https://docs.microsoft.com/en-us/windows/mixed-reality/install-the-tools for more details. Targeting the Universal Windows Platform and using the Windows Mixed Reality SDK are required if you plan to publish on the Windows Store. We'll set that up now.

Before getting started, be sure you have your WMR headset connected to the Windows 10 PC, that Mixed Reality Portal is running, and that you're able to see and interact with the home environment.

Setting up for Immersive WMR

To target Windows Mixed Reality VR devices, you can target the Standalone / Windows platform, as follows:

  1. Configure your UnityBuild Settingsso that they target the Standalone platform via File | Build Settings... | PC, Mac & Linux Standalone | Switch Platform.
  2. SetTarget Platformto Windows andArchitecture to x86_64.

If you plan to publish on the Windows Store, you need to target the Universal Windows Platform. In that case, I recommend that you develop and build for that target early in your development process by setting it up as follows:

  1. Configure your Unity Build Settings so that they target Universal Windows Platform via File | Build Settings... | Universal Windows Platform | Switch Platform.
  2. If you do not see Universal Windows Platform in the platforms list, or when you pick it if you see a message stating that the module is not loaded, you may need to go back to Unity Hub and install the module, as described earlier in this chapter.
  3. Set Target Device to Any Device.
  4. Set the Architecture to x64.
  5. Set Build Type to D3D.

If you see a warning about missing components in Visual Studio, don't worry – we'll address that soon. Next, add the Windows Mixed Reality plugin to your project using these steps:

  1. OpenXR Plug-in Management in Project Settings using Edit | Project Settings | XR Plugin Management.
  2. In the Plug-in Providers list, check the Windows Mixed Reality checkbox. This will install the SDK files.
  3. You'll now see the Windows Mixed Reality option in the left tab menu. Select it.

Examine the Windows Mixed Reality settings, as follows:

If you press Play now, you should see the scene playing in your VR headset. Before you can build and run your project, though, you need to ensure you have the necessary Visual Studio components installed.

Installing Visual Studio workloads

Visual Studio is a powerful integrated development environment (IDE) for all kinds of projects. When we build for UWP from Unity, we will actually build a Visual Studio-ready project (a .csproj file) that you can then open in Visual Studio to complete the compile, build, and deploy process, which will allow you to run the app on your device.

To build for UWP, ensure you have the required Visual Studio workloads, as follows:

  1. From Unity, open your C# project in Visual Studio (Assets | Open C# Project).
  2. In Visual Studio, select Tools | Get Tools and Features from the main menu.
  1. Select theWorkloadstab and choose the .NET Desktop Development andDesktop Development with C++ workloads, and then pressModifyto install it (if it's not already installed).
  2. Choose the Universal Windows Platform Development workload and then press Modify to install it (if it's not already installed).
  3. You may find that you'll need to download and install additional .NET targeting packs. If you see a message to that effect when you build, follow the instructions provided.

My Workloads section looks as follows. The ones with the checkboxes ticked have been installed:

In order to sideload unsigned UWP apps and debug them, including development projects built with Unity and Visual Studio, you need to enable Developer Mode in Windows on your PC. In your Windows 10 desktop, open the For Developer settings (in Windows, go to Start | Settings | Update & Security | For Developers) and select Developer Mode.

At this point, it's also advised that you Build And Run the project to verify it will build correctly and run as expected on your VR device. The SDK we've installed is sufficient for the projects in this book. However, Microsoft provides a rich collection of optional tools in the Mixed Reality Toolkit.

Installing the Mixed Reality Toolkit (MRTK)

As a Windows MR app, you have the option of using the Microsoft Mixed Reality Toolkit (MRTK) for Unity, which is built on top of the native XR SDK. Microsoft maintains this project, which provides high-level support for both immersive MR headsets, OpenVR headsets, and HoloLens. MRTK is a richly featured, well-designed, device-independent software toolkit. You can read more about its architecture at https://microsoft.github.io/MixedRealityToolkit-Unity/Documentation/Architecture/Overview.html. It is an open-source project and can be found on GitHub at https://github.com/microsoft/MixedRealityToolkit-Unity.

The projects in this book do not depend on you having this toolkit installed. If you would like to use it in your own projects, follow the latest getting started instructions at https://microsoft.github.io/MixedRealityToolkit-Unity/Documentation/GettingStartedWithTheMRTK.html. Note that some of the prerequisites and instructions that are given pertain to HoloLens development and are not necessary for VR devices. To install MRTK, follow these steps:

  1. Browse to the MRTK Releases downloads page at https://github.com/Microsoft/MixedRealityToolkit-Unity/releases.
  2. Click the specific packages to download; such as the following, for example:
    • Required: Microsoft.MixedReality.Toolkit.Unity.Foundation.2.1.0.unitypackage
    • Optional: Microsoft.MixedReality.Toolkit.Unity.Tools.2.1.0.unitypackage
    • Optional:Microsoft.MixedReality.Toolkit.Unity.Extensions.2.1.0.unitypackage
    • Other example packages may be fun and useful
  3. Once downloaded, drag each .unitypackage file into your Project window to install them (or use Assets | Import Package | Custom Package) and click Import.
  4. Note that the package adds a new Mixed Reality Toolkit menu to the main menu, as shown in the following screenshot. Convert the imported materials into Universal RP using Mixed Reality Toolkit | Utilities | Upgrade MRTK Standard Shader to Render Pipeline.

  1. Then, convert any example materials into URP using Edit | Render Pipeline | Universal Render Pipeline | Upgrade Project Materials:

The packages install several folders in your Project Assets, including ones named MixedRealityToolkit/, MixedRealityToolkit.Extensions/, MixedRealityToolkit.SDK/, MixedRealityToolkit.Tools/, and so on. I encourage you to explore the various subfolders, open example scenes, and even read through the scripts.

The toolkit includes a handy build tool that provides a shortcut to the build and player settings. For example, to build the project, follow these steps:

  1. Open the MRTK Build window, which can be opened using Mixed Reality Toolkit | Utilities | Build Window.
  2. Select Any Device in Quick Options.
  3. Click Build Unity Project.

In your own projects, you can choose to replace our XR Rig camera rig with the camera and input prefabs provided in the MRTK.

If you do not need to set up for an additional VR device, you can move onto the next chapter. Now, we will consider setting up Android-based mobile VR devices.