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

Building for Oculus Quest

This section describes how to set up and build your VR project on an Oculus mobile VR device, including Quest. Internally, these devices run Android, so developing from Unity has a lot in common with developing for any Android device. For VR, you set XR Plugin to Oculus and configure your project so that it builds and runs on the mobile VR device.

Before we begin, ensure your device is in Developer Mode, as follows:

  1. Open the Oculus companion app on the phone connected to the device.
  2. Go to the Settings menu (lower-right corner).
  3. Select the device (for example, Oculus Quest).
  4. Select More Settings | Developer Mode and switch it on, as shown in the following screenshot:

Next, we'll make sure you can build from Unity for a target Android platform.

Installing the Android tools

To develop VR apps that will run on Oculus Go and Oculus Quest (and other Android mobile VR devices such as Google Cardboard), you will need to set up your development machine for Android development. The requirements are not specific to virtual reality; they're the same for any Android app from Unity. The process is also well-documented elsewhere, including the Unity documentation athttps://docs.unity3d.com/Manual/android-sdksetup.html.

OK; let's get going. Developing for Android devices requires that you prepare your development system with the Android SDK, NDK, and JDK external tools (short for software development kit, native development kit, and Java development kit, respectively). Your current version of Unity may have been already installed with the Android Build Support module, including Android SDK & NDK Tools and OpenJDK. You can double-check this in Unity Hub by going to the Installs tab, clicking the three-dot button for your current release, and choosing Add Modules, as follows:

If necessary, install the missing tools. Your Unity configuration should then be automatically set up. You can check this by opening Unity and doing the following:

  1. Select Edit | Preferences… to view the configured external tools.
  2. Verify that JDK Installed with Unity is checked.
  3. Verify that Android SDK Tools Installed with Unity is checked.
  4. Verify that Android NDK Installed with Unity is checked.

This Preferences window can be seen in the following screenshot:

If Unity complains it cannot find a required tool version, you may need to install it manually. For Android SDK, you can install just the command-line tools from the Android Studio downloads page at https://developer.android.com/studio/. (Scroll to the bottom of the page for the Command-Line Tools Only section). Then, NDK can be downloaded from https://developer.android.com/ndk/downloads.

Now you can set up your project for Oculus mobile VR devices.

Setting up for Oculus mobile VR

To target Android-based Oculus mobile VR, configure your Unity Build Settings so that they target the Android platform viaFile | Build Settings... | Android | Switch Platform. Next, if it's not already installed, add the Oculus XR Plugin, as follows:

  1. Open XR Plug-in Management in Project Settings using Edit | Project Settings | XR Plugin Management.
  1. Click the Android tab.
  2. In the Plug-in Providers list, check the Oculus checkbox. The Oculus SDK will be installed.
  3. You'll now see anOculusoption in the left tab menu. Select it.
  4. PressCreateto create a serialized instance of the settings data in order to modify the default Oculus settings.
  5. Check the V2 Signing checkbox, especially if you're targeting Quest.

The Oculus settings for Android are shown in the following screenshot:

To preview the scene in your device, you could use OVR Scene Quick Preview, which is included with the Oculus Integration Toolkit. This will be described later in this chapter. If you have an Oculus Quest connected to your PC with an Oculus Link Cable and the Oculus Link settings are enabled, you can view your scenes in VR (for details, seehttps://support.oculus.com/525406631321134). Otherwise, if you pressPlaynow, you can run the scene in the editor's Game window but not remotely on the VR device. To see it in VR, you need to do a build.

Oculus Link connects Quest to a PC. It's marketed as an expansion of the Quest so that you can play PC-based VR games for Rift and SteamVR. But it can also be used in Unity for viewing VR scenes in Play mode.

Other Android and optimization settings

To build for an Android device, you also need to configure other PlayerSettings. Some settings are required for it to run, while others are recommended for it to run better:

  1. At the top, set Company Name and Product Name. The product defaults to the project name that was used when it was created via Unity Hub.
  2. In Other Settings | Package Name, set a unique app ID using a Java package name format. It defaults as a composite of the company and product names; for example, mine is com.UVRP3.VR_is_awesome.
  3. The minimum API level will vary, depending on your target device. Oculus Quest requires Minimum API Level: Android 6.0 Marshmellow (API Level 23). Oculus Go requires a minimum of Android 5.0 Lollipop (API Level 21).

There are many other options in Player Settings. Refer to the Oculus documentation for the current recommendations, including the following:

Some of these current recommended settings include the following:

  • Build Settings | Texture Compress: ASTC.
  • Player | Other Settings | Color Space: Linear.
  • Auto Graphics: Unchecked. OpenGLES3 should be the first in the list. Vulcan should be the second, if at all, but not on Go.
  • Multithreaded Rendering: Checked.
  • Also, review the recommended Graphics pipeline and Quality settings.

Give this a shot by clicking Build And Run.

The SDK we've installed is sufficient for the projects in this book. However, Oculus provides a rich collection of optional tools in the Oculus Integration toolkit.

Installing the Oculus Integration toolkit

As you're building an Oculus app, you have the option of using the Oculus Integration toolkit for Unity, which is built on top of the XR plugins. It provides support for advanced rendering, social platform interaction, audio, avatars, lipsync, and more. This is the same as what we had for Oculus Rift, which we mentioned earlier in the Building for Oculus Rift section. Look there for a discussion of this. In summary, to install the Oculus Integration toolkit from the Asset Store, follow these steps:

  1. In your web browser, go to https://assetstore.unity.com/packages/tools/integration/oculus-integration-82022.
  2. Click Open In Unityand Install it in your project.
  3. If prompted, also allow updates to the OVRPlugin for the Unity Editor or other packages.
  4. Since we're using the Universal Render Pipeline, you should convert the imported materials using Edit | Render Pipeline | Universal Render Pipeline | Upgrade Project Materials.

In addition to the collection of example scenes, frameworks, and Oculus platform support, it installs an Oculus menu in the main menu bar, which includes some shortcuts, especially for mobile VR development, as shown in the following screenshot:

Press the OVR Scene Quick Preview button to play the scene in your device. You may be prompted to install the OVR Transition APK. The Transition APK helps Unity launch your working scene in the device. After doing this, press Build And Deploy Scene to quick-build the scene for preview in the HMD. The dialog box is shown in the following screenshot:

To build and run this, press theOVR Build APK And Run option.

In your own projects, you may choose to replace our XR Rig camera rig with theOVRCameraRigprefab provided in the Oculus Integration package, which can found in the Assets/Oculus/VR/Prefabs/folder and is used in their various example scenes.

One more thing worth mentioning is that the Android Debug Bridge (adb) tool is something you should get to know sooner rather than later for installing Android APK builds and examining runtime logs. We'll take a look at this next.

Using adb

When you build a project from Unity, it creates an .apk file and installs it on the mobile device connected to your development machine. If you already have an .apk file, you can install it manually using the Android adb command-line tool.

The Android Debug Bridge (adb) is an important command-line tool that you should get to know so that you can communicate with an Android device for side-loading apps and debugging. It's included with the Android SDK. The full documentation can be found at https://developer.android.com/studio/command-line/adb. If you do not have the adb command on your system, you can install it as follows:

  1. Download Android Platform Tools from https://developer.android.com/studio/releases/platform-tools.
  2. Unzip the folder.
  3. Navigate to the folder containing adb.exe.
  4. Open the Terminal/Command Prompt at this folder. For example, in Windows Explorer, you can type cmd into the address bar.

I like to move the unzipped platform-tools/ folder into my /Program Files/ folder and add it to the Windows environment PATH.

Now, when you type adb devices at the Command Prompt, you should see the connected Android device. To install the APK file built from Unity, use the adb install [path to apk file] command. Other useful adb commands include the following:

  • adb help: Lists the available adb commands
  • adb devices: Lists the currently connected devices
  • adb install [path to apk file]: Sideload installs an APK package on your device
  • adb install -r [path to apk file]: Installs and replaces a package
  • adb shell pm list packages -3: Lists the packages already installed on the device
  • adb uninstall [packagename]: Removes a package from the device; for example, com.UVRP3.VR_is_Awesome

If you have trouble running adb install, you may need to add the Oculus ADB Drivers. Follow these steps to do so:

  1. Downloadthe zip file containing the driver from https://developer.oculus.com/downloads/package/oculus-adb-drivers/.
  2. Unzip the file.
  3. Right-click on the.inffile and selectInstall.

Adb can also be used to access the Android device logs, which are very useful when you're debugging:

  • adb logcat -v time -s Unity >logs.txt: Streams the logs, filtered for Unity messages, and pipes them to the local logs.txt file. Run this command and then start your app.
  • adb logcat -d -v time -s Unity >logs.txt: Dumps the most recent Unity log messages into logs.txt and exits.
  • adb logcat -c: Clears the logs on the device.

Unity includes an Android Logcat package that adds support for displaying log messages coming from Android devices in the Unity Editor. Install the Android Logcat package via Package Manager (Window | Package Manager | All Packages | Android Logcat | Install). Then, open the window using Window | Analysis | Android Logcat. You can filter the message list; for example, using the Unity string.

If you do not need to set up an additional VR device, you can move onto the next chapter. Next, we'll consider setting up Google Cardboard for either Android or iOS.