Show/Hide Mobile Menu

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, then Devices, scroll down to Developer 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 press Create project.
  • After Unity opens go to Edit > Project Settings, select XR Plugin Management on the left. Press Install XR Plugin Management button.
  • Click on the Android tab in XR Plugin-in Management and check Oculus.
  • 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 under Graphics APIs on the right remove OpenGLES2 (Deprecated).
  • Go to File > Build Settings, select Android 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, press Refresh on the right.
  • Press Switch Platform in the bottom right.
  • Go to Window > Package Manager and select Packages: Unity Registry from the drop down in the top left.
  • Press the gear icon and select Advanced Project Settings and check Enable 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 the XR Interaction Toolkit package should show up. Select it and press Install. For Unity 2021 press the plus button in the top left and select Add package from git URL. Then type com.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 press No Thanks. Unity will then restart.
  • Open the package manager again and select XR Interaction Toolkit on the left. Expand samples and press Import to the right of Starter Assets. In older versions of the package it's called Default Input Actions. If you want to test the project on a PC with a keyboard also import XR 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 from XRI Default Input Actions and press Add to ... button at the top of the inspector.
  • Open Edit > Project Settings and select Preset Manager on the left. Under ActionBasedController enter Right in the text field to the left of XRI Default Right Controller and enter Left in the text field to the left of XRI Default Left Controller.
  • Go to GameObject > XR > XR Origin (Action-based)
  • Select XR Origin in the hierarchy window and press Add Component in the inspector and add Input Action Manager.
  • In the Input Action Manager expand Action Assets and press the plus button and click on the circle button to the right of Element 0 and select XRI 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 called Build 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 select Unknown 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 press Create project.
  • After Unity opens go to Edit > Project Settings, select XR Plugin Management on the left. Press Install XR Plugin Management button.
  • Select the Android tab and check ARCore. Select the iOS tab and check ARKit.
  • Go to Window > Package Manager. Select Unity Registry from the drop down on the top left and type ar foundation into the search field. Select AR Foundation on the left and press Install in the bottom right.
  • Close the package manager and go to GameObject > XR > XR Session Origin and then delete the Main Camera. Then go to GameObject > XR > XR Session.
  • Click on AR Camera under AR Session Origin and in the inspector on the right set Light Estimation to Everything.
  • In the Project window go to Assets > Settings and select URP-Balanced-Renderer. Press Add Renderer Feature in the inspector and select AR Background Renderer Feature.
  • Go to Edit > Project Settings and select Graphics on the left. Under Scriptable Render Pipeline Settings click the circle button on the right and select URP Balanced. The default renderer is URP-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. Uncheck Multithreaded Rendering. Remove OpenGLES2 (Deprecated) from Graphic APIs. Enter a Package Name. Set Minimum API Level to Android 7.0 'Nougat'.
  • Next select the iOS tab. Enter the Bundle Identifier and Signing 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, check Automatically Sign. Enter a sentence for Camera 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 to AR Session Origin. Create a particle system by going to GameObject > Effects > Particle System. Select the particle system and uncheck Looping, set start size to 0.1 and uncheck Play on Awake. Click Add Component and select AR Point Cloud. Click Add Component again and select AR Point Cloud Particle Visualizer. Create a folder called Prefabs under Assets in the Project window and drag the particle system into the Prefabs folder. Delete the particle system from the hierarchy window. Select AR Session Origin and drag the particle system prefab on to Point Cloud Prefab under AR Point Cloud Manager.
  • To build for iOS go to File > Build Settings. Select iOS under Platform on the left and press Switch 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 to Window > Devices & Simulator in Xcode. Select your device on the left and check Connect via network. Back in Unity press Build and Run. Create a folder called Build in your project and inside that folder create a sub folder called iOS. Select it and then press Choose.
  • To build for Android go to File > Build Settings. Select Android under Platform on the left and press Switch Platform in the bottom right. Make sure you have an Android phone connected to your computer via a USB cable. Next press Build And Run. Select the Build 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.