cardboard

This is part 2 of our series introducing Makers to Virtual reality. If you haven’t already seen it, you should go check out: An Introduction To Unity.

The Google Cardboard headset is the perfect and inexpensive way to get started with virtual reality for anyone who owns a smartphone. Mainstream companies are already helping get headsets into the hands of the public like wildfire. The New York Times recently sent out more than a million Google Cardboard headsets to its subscribers, bringing VR to a whole new audience. Children are also poised to have their own Google Cardboard headsets in droves as companies like View-Master produce Google Cardboard headsets targeted at children. The market is growing and soon the public will be eager for new VR experiences. This is where you, the humble Maker, come into the picture.

I’m looking to share the basics of how people who are used to building physical things can get involved in helping build these VR experiences in Unity. In the first article of this three part series, we looked at how to get started with Unity. If you are completely new to Unity, I’d recommend reading that one before continuing on with this guide.

In this article, we will look at how you can bring the Google Cardboard camera into a Unity project so we can view a scene in virtual reality. I’ll be using the Maker Faire robot scene I imported in our first article on Unity — you can use any Unity scene you’d like. I’ll be sticking with a scene that has only a few objects to keep things simple.

Project Steps

IMPORTING THE GOOGLE CARDBOARD SDK

To bring in the capabilities of a Google Cardboard headset into Unity, you’ll need to download the Cardboard SDK for Unity. The main GitHub page for the SDK has both SDK files for you to use, as well as a sample demo. The file you’ll want is CardboardSDKForUnity.unitypackage. Download that to the computer you are running Unity on.

Open up the scene that you’d like to bring into virtual reality and go to Assets > Import Package > Custom Package…. Find the CardboardSDKForUnity.unitypackage file you just downloaded.

importingcustompackage

An “Importing package” window will appear, allowing you to set which parts of the package we want in our project. The only thing we’ll want to remove is the “Legacy” folder, unless you have an older version of Unity than Unity 5. To remove it, untick the box next to it. Then click “Import”.

unticklegacy

Once you’ve clicked “Import”, the window will close and you should find a new Cardboard folder in your project’s assets. This is where everything for the Google Cardboard SDK lives ready to be used!

importedpackage

You’ll see five folders inside the Cardboard folder you just imported in: Distortion, Editor, Prefabs, Resources, and Scripts. We will only be using one of the Prefabs from the Prefabs folder.

SETTING UP THE GOOGLE CARDBOARD CAMERA

Everything in experiences built with Unity is viewed by the user through a camera. In most simple Unity projects, this camera is called “Main Camera”. We want to change this, so that we look through a Google Cardboard enabled camera instead.

This should bring in a new camera to your scene like so:

Open the Prefabs folder within the Cardboard folder you’ve just imported into your project. You will see a Prefab called “CardboardMain”; drag that into your project’s hierarchy.

dragincardboardmain

This should bring in a new camera to your scene like so:

ournewcamera

If this is a Unity project you’ve already spent lots of time and effort into and you want to keep the view very similar, take note of where your “Main Camera” object is in the scene. Move “CardboardMain” to where your “Main Camera” is. In my own project, I moved it to {x: 0, y: 1, z: –10} . Once your CardboardMain is in the right spot, delete the “Main Camera”, or hide it by clicking the checkbox next to its name in the Inspector:

removingmaincamera

The easiest way to test how this camera is positioned is to test out our Unity application and see how it looks. Let’s do that now.

TESTING OUR UNITY VR EXPERIENCE

To test out the new Google Cardboard camera, we will run our scene just like we did in the first part of this series. Click the Play button to test your the scene through the new Google Cardboard camera in CardboardMain. It will appear within the Game tab:

testingthescene

Of course, as you are testing this within Unity, you cannot physically move our head around as you would whilst wearing a Google Cardboard headset. Instead, you can use keyboard controls to move the camera instead:

  • Hold down the Alt key and move your mouse to look around the scene.
  • Hold down the Shift key and move your mouse to tilt your head.

ADJUSTING THE VR CAMERA

The camera I’d positioned for the previous article demo is a bit too far away for a VR experience. You may find this is the case for your own camera positioning too. Within Unity, we can move the camera around whilst the scene is running in our Game tab.

To move it around, select CardboardMain from the hierarchy and adjust the transform values as you would when positioning it in the scene. For example, I change CardboardMain’s Z position in my scene whilst it is running to see how it looks. Any changes made whilst the scene is running are temporary.

adjustingcamerawhiletesting

When you are happy with the camera position, take note of what the position values are for CardboardMain and then click the Play button to stop the scene test running. Re-enter those position values into CardboardMain to have them made permanent.

RUNNING ON A GOOGLE CARDBOARD HEADSET

To test this on an actual headset, you’ll need to build it for either iOS or Android within Unity. Keep in mind for iOS, the Cardboard SDK only works on iPhone 5 and later with iOS 8.

To build iOS apps, you will need to have an Apple account and understand XCode. This is a bit beyond the scope of this article, so follow that link for further info on what you will need to know to get an Apple account and set up XCode for Unity.

Building for Android is a bit simpler, you just need to install the Android SDK from here first before completing the following steps.

To begin the build process, go to the Build Settings via File > Build Settings:

openbuildsettings

Within Build Settings, choose your smartphone operating system, either iOS or Android. Then click “Player Settings” to open further options to the right. Within these options, expand the section titled “Other Settings”. In here, there is a field called “Bundle Identifier”. This is in the form of a standard method of identifying apps — com.company.appname. Change yours to something that is custom to you, for example, I set up mine to be  com.makermedia.cardboarddemo.

buildsettings1

Then expand the section called “Resolution and Presentation” and set the Default Orientation field to “Landscape Left”.

buildsettings2

Finally, check that your Unity scene is in the list (notice below there is no scene underneath “Scenes in Build”, your scene should be there!), then connect your device to your computer (in my case, I’ve connected an Android device) and click “Build and Run” to create an app and have it run automatically on your device.
buildandrun

That covers how to get a simple Google Cardboard VR experience up and running. In the final part of this series, we will look at how to add Google Cardboard interaction into our scenes so we can interact with our scene.