Getting started with Meta Quest using Unity

Published on Oct 22, 202410 min read

The Meta Quest series of headsets (previously known as the Oculus Quest) are by far the most popular standalone, all-in-One virtual-reality headsets in the market. The Meta Quest 2 is absolutely a great virtual-reality hardware, providing a high-fidelity immersive experience to users.

Though the Quest 2 comes with a built-in processor and runs on Android, the headset can still be used for PCVR with a VR-ready computer using Oculus Link (over USB) or Air Link (over Wi-Fi). In this guide, we will focus on setting up the Meta Quest 2 headset for standalone development using the Unity engine.

Prerequisites

Before we get into Unity for the development, ensure you are ready with the following

Enabling Developer Mode for Meta Quest 2

In order to push and preview your builds, you need to list your Meta Quest 2 as a development device by enabling the Developer Mode. You can use the Meta Quest mobile app (iOS/Android) to enable developer mode.


Meta App Home MenuFig 4.1 Meta Quest App - Home (left), Menu (right)



Meta Device SettingsFig 4.2 Meta Quest App - Devices (left), Developer Settings (right)


That's it! Your Meta Quest 2 headset is now enabled with Developer Mode. If you connect your headset to a PC via a USB cable, it will list under the Run Device field in the Build Settings window for the Android platform in Unity.


Alternatively, you can run the following command to see if your headset is listed as development device. This command will list all the Android devices connected with developer mode enabled and USB Debugging authorized.

bash

adb devices -l

Setting up the Unity project

For creating VR experiences you have two options.

In this guide, we will use the latter option to create a standalone Android build using Unity by implementing Unity's XR framework capabilities.


Create a new Unity project (or you can use an existing project) to start with and the rendering pipeline can be your choice as the built-in or URP. You need to set the project's target platform to Android first.

Change platform to Android

Go to File-> Build Settings. Select Android and click Switch Platform. If the Android option is greyed out, install the Android Platform module using Unity Hub.

Unity Build SettingsFig 4.3 Unity Build Settings - Android

Install XR Dependencies

The project is now Android compatible but not yet ready for the Meta Quest. For that, you need to install certain dependency packages and luckily these are provided by Unity and are available in the Unity package registry. We will be needing 2 packages which are the XR Plugin Management and the XR Interaction Toolkit.


Go to Window-> Package Manager and install the packages.



XR Plugin ManagementFig 4.4 Unity Package - XR Plugin Management



XR Interaction ToolkitFig 4.5 Unity Package - XR Interaction Toolkit


Configure XR Plug-in Management

Now that we have the required dependencies installed, let's configure the XR Plug-in Management first.


Go to Edit-> Project Settings-> XR Plug-in Management


Project SettingsFig 4.6 Project Settings - XR Plug-in Management


We are going to set Oculus for Android as our plug-in for this project. If you take a closer look you can see other options such as AR Core, OpenXR. The AR Core is for creating Augmented Reality experiences for supported Android devices. OpenXR on the other hand is more like an open standard that provides a common API for developing XR experiences across multiple hardwares rather than using individual frameworks. For now, we will stick with Oculus as our plugin for the Meta Quest 2 development as we are only targeting one device.

Setting up the scene

On successful installation of the dependencies, we get additional components for creating our VR experiences which we are going to implement now. Select and delete the Main Camera object so that we can replace it with a XR Rig system.


Right-click over the Hierarchy panel and choose XR-> XR Origin (VR) to add the XR Rig as shown in the right image.


XR OriginFig 4.7 Default Camera replaced with XR Origin (XR Rig)


If you take a look at the Left Controller or Right Controller gameObjects, the action reference fields are empty which means no actions are mapped for those specific functions.


Actions MissingFig 4.8 XR Controller - Action references missing


Here's where the starter assets from the XR Interaction Toolkit come in handy. We need to map those action presets here for both left and right XR Controller (Action-based) components. But mapping them one by one can be a bit of work, so there is a quick workaround. Go to the Presets folder under XR Interactive Toolkit Starter Assets and select XRI Default Left Controller.


Default PresetsFig 4.9 Default controller presets


With this preset asset selected, click the button named Add to ActionBasedController default at the top left of the Inspector panel. Do the same for the right controller by selecting the XRI Default Right Controller preset asset.


Add PresetsFig 4.10 Add controller preset to Preset Manager


This will add both left and right action maps for meta quest controllers to Unity's Preset Manager. You can verify this by going to Edit-> Project Settings-> Preset Manager. Simply enter the values 'left' and 'right' against corresponding presets so that Unity may recognize which preset needs to be applied for which XR Controller (Action-based).


Add Filter KeysFig 4.11 Preset Manager - Add filter keys


This enables Unity Editor to map the action maps automatically for both controllers when you add an action-based XR Rig. In the hierarchy panel delete the XR Origin (XR Rig) you added previously and add it again. This time, if you check the left or right XR Controller (Action-based) components, you will see the required actions are automatically mapped.


Actions MappedFig 4.12 Preset Manager - Action references mapped


One last thing you need to do is to ensure that the scene has an InputActionManager component added and has the XRI Default Input Actions preset asset assigned.


XRI Default Input Action AssetFig 4.13 XRI Default Input Actions starter asset


Select the XR Origin (XR Rig) object and add the 'InputActionManager' component if it's not already added. You can also add this component to a new empty gameObject if required, but make sure only a single instance of this component exists in the Scene. This component has an 'Action Assets' array field. Create a new entry and add the XRI Default Input Actions preset asset from the XR Interaction Toolkit starter asset folder.


XRI Default Input Action AssignedFig 4.14 XRI Default Input Actions assigned to InputActionManager


That's it! Your scene setup is done. If you connect your 'Developer Mode' enabled Meta Quest 2 headset and generate a build, you should be able to view your project in VR. What is happening here is that the head position and orientation values tracked by the device sensors get transformed and applied to the Unity Camera. And the tracking value from the hand controllers gets applied to the left and right controller gameObjects within Unity. So any child gameObject of the 'Left Controller' and 'Right Controller' gameObjects will track and move with the controller.


To understand it better, create two Sphere objects and add them as a child of the 'Left Controller' and 'Right Controller' gameObjects. Set the scale to 0.2f on all axes for both the sphere objects. Now build the project to the device and you can see the movement of the spheres is in sync with the controllers. You can also download Meta Quest 2 controller models from Oculus Controller Art page and replace the spheres with them.

Copyright © 2024 rendercodeninja. All rights reserved.

Made with ❤ by rendercodeninja.GitHub