Getting Started with VR in Unity

A few weeks ago I was asked to assist a university student with a Virtual Reality project in Unity 3D. As this would have been my first interaction with making a VR game, I thought this would be a good learning experience for me too.

Although there are so many excellent tutorials on the web, I couldn’t find “one size fits all” instructions and landed up spending hours taking snippets from several sites to get the smallest things working.

I am hoping this tutorial is a “one size fits all” to someone out there, but as we all know with coding, Unity and Xcode, it’s never as smooth sailing as it is in your head.

Initial Project Setup

Setup a new unity project and folder structure as your process defines.
For the VR game to run on mobile it is suggested to get low poly assets from the unity asset store. If the poly count is too high it will be resource intensive and crash on a mobile device.
Install the Google VR software. Unity has a quick start tutorial on their site in regards to the steps. This is the link https://docs.unity3d.com/Manual/googlevr_quick_start.html
Make sure that the player settings are updated as per the Unity quick start document (These settings will depend on what platform you are building for and targeting)

Unity 3D Player PreferencesUnity 3D Player Preferences

Everything is setup now and ready to be built out when you have created your masterful VR game.
Build out your controls, your actions, and scenery in the game to how you have envisioned it.
Make sure that your UI are VR friendly, I.E, how will the user start, and exit the game. Will there be clickables in the game etc ?
There are now 2 options that follow, are you building for Android or IOS

Build and Run for Android

Install Android SDK for Unity from the Unity site, there are install instructions and guidelines on the unity website.
Install DayDream if you are using the Daydream-ready phone and a Daydream View.

Day Dream Unity XR SettingsDay Dream Unity XR Settings

Make sure that the player settings are updated as per the Unity quick start document for Android, and for what Hardware you will be using to test the Android version. https://developers.google.com/vr/develop/unity/get-started-android

CardBoard Unity XR SettingsCardBoard Unity XR Settings

Connect your Android device to your computer using a USB cable. I did all my testing on Asus Nexus 7
Make sure your scene is part of build settings.
Select File > Build and Run.
Unity will build your project into an Android APK, installs it on the test device, and launches it.
The view will be 2 identical scenes in different frames on your device. This is great and you know that the android build has been a success!
Put the phone in your viewer and try out the demo.

Android VR ViewAndroid VR View

Build and Run for IOS

Requirements:

  • iOs device
  • Apple developer account
  • 2 Certificates need to be requested with your Apple ID in your developer account, one for development and one for distribution (if you go as far as publishing to the app store)
  • CocoaPods
  • Google SDK
  • XCode

The ios build has several dependencies and if these are not loaded you will spend hours trying to troubleshoot what is missing. It’s not as simple as the Android build, but if you do these pre requirements there should be fewer errors.

Some of the error codes i was receiving before knowing that I did not have all the pre requirements

  • _gvr_set_default_viewer_profile
  • No pods being created in Xcode
  • iOS test device not selected in Xcode

CocoaPods Installation

CocoaPods needs to be installed. On their site they have a very in detail, user friendly, instructional https://guides.cocoapods.org/using/getting-started.html
Hint: Due to the Google SDK package being used for this VR project, the Android SDK is still needed even if you are only building for IOS. (Or this helped my IOS build at least ;))
Your project should now be ready to build for IOS.
Update your player settings to IOS build

CardBoard Unity XR SettingsCardBoard Unity XR Settings

Make sure your IOS device is plugged into your MAC, ready to be tested
Do the necessary registrations, information, icons etc needed on your developer.apple account
Open XCode on your Mac
XCode will pick up your device automatically
Fill in all the necessary information, and link the necessary certificates that are needed. If you need assistance with what needs to be done for the developer account refer to our tutorial here : Getting Started with Apple Developer Account
Remember to make sure that at the top right corner of xcode that the Unity-iPhone drop down is selected on your iOS device and doesn’t say generic iOS device. If this is not selected, it will not display on your mobile testing device
When you are ready to do an iOS build, hold your breath, hold all thumbs and toes, and click on the “build and run” button in Xcode.
Once the build is complete the same view will be displayed in double images as it showed on your android device.

IOS VR ViewIOS VR View

OnGoing Issues

Once the iOS game was built and I was able to test on iPhone , the whole scene world is upside down
I have yet to find any resolution for this and have followed all the tips and tricks that others have done to no avail 🙁
The suggestion is that the player settings resolution field gets updated to landscape left for iOS. In my case though it did no assist. I even went as far as hardcoding it in Xcode and the relevant c# scripts.
As a quick work around i rotated the players camera view in unity to 180 degrees.
Although this was a quick resolution, all the controls are opposite. If you know of any solution to this feel free to drop us an email and i can test it my side.