Setting up Unity for VR and AR
May 9, 2022
The VR and AR templates in Unity use the built-in renderer so if you want to use URP renderer you will need to set things up manually. It's presumed you have downloaded Unity Hub and have installed a version of Unity. This tutorial uses Unity version 2021.2.19 so if you use a different version it might be a little different. When installing Unity, for VR and AR on Android you will need to check Android Build Support, Android SDK & NDK Tools and Open JDK, and for AR on iOS you will need to check iOS Build Support. For iOS you will also need to have XCode installed.
VR with Quest 2
- To side load apps onto the Quest 2 you will need to sign-up as a developer using this link.
- Open the Oculus app on your phone. Tap on
Menu
in the tab bar, thenDevices
, scroll down toDeveloper Mode
and turn it on. - Connect the Quest 2 to your computer with a USB cable. The USB charging cable that came with the headset will work as long as you have a USB-C socket on your computer or else you will need to get a USB-C to USB-A adaptor.
- In Unity Hub press
New project
in the top right. - Select
3D (URP)
template and enter a project name and location and pressCreate project
. - After Unity opens go to
Edit > Project Settings
, selectXR Plugin Management
on the left. PressInstall XR Plugin Management
button. - Click on the
Android
tab inXR Plugin-in Management
and checkOculus
. - If you are on Windows select the desktop tab and check
Oculus
. This allows you to preview your app on the Quest 2 when you press play in Unity. You will need to install the Oculus PC app for this to work. - Select
Player
from the left and underGraphics APIs
on the right removeOpenGLES2 (Deprecated)
. - Go to
File > Build Settings
, selectAndroid
on the left. - Plugin in your Quest 2 and in the
Run Device
drop down select the Quest 2 device. If it doesn't show up, pressRefresh
on the right. - Press
Switch Platform
in the bottom right. - Go to
Window > Package Manager
and selectPackages: Unity Registry
from the drop down in the top left. - Press the gear icon and select
Advanced Project Settings
and checkEnable Pre-release Packages
. - Close the project settings and go back to the Package Manager. In Unity 2020 type
xr interaction
into the search field and theXR Interaction Toolkit
package should show up. Select it and pressInstall
. For Unity 2021 press the plus button in the top left and selectAdd package from git URL
. Then typecom.unity.xr.interaction.toolkit
and press enter. The package will then install. - A dialog will appear asking if you want to enable the new input system. Press
Yes
. It will then ask if you want to update your project from the old to the new input system. Since this is a new project there is no need to do this so pressNo Thanks
. Unity will then restart. - Open the package manager again and select
XR Interaction Toolkit
on the left. Expand samples and pressImport
to the right ofStarter Assets
. In older versions of the package it's calledDefault Input Actions
. If you want to test the project on a PC with a keyboard also importXR Device Simulator
. This allows you to use the keyboard to control the controllers but it's a bit awkward to use. - In the Project window open
Assets > Samples > XR Interaction Toolkit > <version> > Starter Assets
. Select each of the items in that folder apart fromXRI Default Input Actions
and pressAdd to ...
button at the top of the inspector. - Open
Edit > Project Settings
and selectPreset Manager
on the left. UnderActionBasedController
enterRight
in the text field to the left ofXRI Default Right Controller
and enterLeft
in the text field to the left ofXRI Default Left Controller
. - Go to
GameObject > XR > XR Origin (Action-based)
- Select
XR Origin
in the hierarchy window and pressAdd Component
in the inspector and addInput Action Manager
. - In the
Input Action Manager
expandAction Assets
and press the plus button and click on the circle button to the right ofElement 0
and selectXRI Default Input Actions
. - Go to
GameObject > XR > Locomotion System (Action-based)
- If you are on a Windows machine and you have the Oculus PC app installed you can preview your scene by pressing play and having the Quest 2 connected. Otherwise you will need to install the app on the Quest 2 by going to
File > Build and Run
. Create a new folder calledBuild
in your project and enter a filename for the app. On the Quest 2 open Apps and click on the drop down in the top right and selectUnknown Sources
. Your app should be listed. Click on it to run it.
For an introduction to creating interactions in VR, Unity has a good tutorial on its site.
AR with iOS and Android
- In Unity Hub press
New project
in the top right. - Select
3D (URP)
template and enter a project name and location and pressCreate project
. - After Unity opens go to
Edit > Project Settings
, selectXR Plugin Management
on the left. PressInstall XR Plugin Management
button. - Select the
Android
tab and checkARCore
. Select theiOS
tab and checkARKit
. - Go to
Window > Package Manager
. SelectUnity Registry
from the drop down on the top left and typear foundation
into the search field. SelectAR Foundation
on the left and pressInstall
in the bottom right. - Close the package manager and go to
GameObject > XR > XR Session Origin
and then delete theMain Camera
. Then go toGameObject > XR > XR Session
. - Click on
AR Camera
underAR Session Origin
and in the inspector on the right setLight Estimation
to Everything. - In the Project window go to
Assets > Settings
and selectURP-Balanced-Renderer
. PressAdd Renderer Feature
in the inspector and selectAR Background Renderer Feature
. - Go to
Edit > Project Settings
and selectGraphics
on the left. UnderScriptable Render Pipeline Settings
click the circle button on the right and selectURP Balanced.
The default renderer isURP-HighFidelity
but I found it produced weird colours when you view the app on a device. - Select
Player
on the left in project settings and enter company and product name. - Select the
Android
tab. UncheckMultithreaded Rendering
. RemoveOpenGLES2 (Deprecated)
fromGraphic APIs
. Enter a Package Name. Set Minimum API Level toAndroid 7.0 'Nougat'
. - Next select the
iOS
tab. Enter theBundle Identifier
andSigning Team ID
. Signing Team ID can be got by signing into your Apple developer account. Click on Membership on the left and Team ID should be listed on that page. Back in Unity, checkAutomatically Sign
. Enter a sentence forCamera Usage Description
like "Camera is required for AR". - Just so you can see something when you run the app add an
AR Point Cloud Manager
toAR Session Origin
. Create a particle system by going toGameObject > Effects > Particle System
. Select the particle system and uncheckLooping
, set start size to 0.1 and uncheckPlay on Awake
. ClickAdd Component
and selectAR Point Cloud
. ClickAdd Component
again and selectAR Point Cloud Particle Visualizer
. Create a folder calledPrefabs
underAssets
in the Project window and drag the particle system into thePrefabs
folder. Delete the particle system from the hierarchy window. SelectAR Session Origin
and drag the particle system prefab on toPoint Cloud Prefab
underAR Point Cloud Manager
. - To build for iOS go to
File > Build Settings
. Select iOS under Platform on the left and pressSwitch Platform
in the bottom right. Make sure you have an iPhone connected to your computer via an USB cable. You can connect your iPhone via WiFi but for the initial build you will have to connect it via USB. To enable WiFi connection, go toWindow > Devices & Simulator
in Xcode. Select your device on the left and checkConnect via network
. Back in Unity pressBuild and Run
. Create a folder calledBuild
in your project and inside that folder create a sub folder callediOS
. Select it and then pressChoose
. - To build for Android go to
File > Build Settings
. SelectAndroid
underPlatform
on the left and pressSwitch Platform
in the bottom right. Make sure you have an Android phone connected to your computer via a USB cable. Next pressBuild And Run
. Select theBuild
folder you created earlier and enter a filename. If the app doesn't start automatically swipe up on the Android phone and look for your app amongst the list of apps.