Monday, October 12, 2015

Building a Google Cardboard VR App in Unity

Unity is currently one of the most popular choices when it comes to building virtual reality applications. Google Cardboard is a great first choice for getting started in the field - spending $20 on a VR headset rather than potentially hundreds is a much safer bet for getting started (and there are plenty of people with smartphones who could potentially enjoy your app with an equally smaller upfront cost!).

I've previously covered how to build VR for the web today via JS, along with demos on Visualizing a Twitter Stream in VR and JS and plenty others. For those who prefer Unity development, it's about time I evened these numbers and wrote some more on VR development in Unity. For some rather strange reason, I couldn't find any in depth guides to working with the Google Cardboard Unity SDK from scratch, so I did what any SitePoint writer would do. I put one together!

What You'll Need

To follow along, you'll need the following bits of software and hardware:

[author_more]

  • Unity - At least v4.5 is recommended. I'm using Unity v5 Pro for this guide. Unity v5 Personal apparently works a-okay too.
  • Windows or Mac - I'll be using Windows in this tutorial for a change (mainly because most Unity devs seem to be on Windows, so it's only fair I follow suit for the article!)
  • Cardboard SDK for Unity - That link will provide you with the GitHub repo. I'll explain this and setting up Java and Android in this article.
  • Java SE SDK - This will be required to install the Android SDK.
  • Android SDK - If you are downloading this for the first time, you only need the "SDK Tools Only" bits of the SDK. I'll explain this soon.
  • A Google Cardboard style headset
  • An Android device to put inside that headset - it is possible to export this to work with iOS, but I won't be covering that process as I don't have an iPhone!
  • Relatively basic knowledge of Unity - I'll cover a lot of the absolute basics for those new to it all but having a small amount of knowledge will help!

The Code

Want to jump straight into the code? My sample project is up at GitHub for you to do whatever you'd like!

Preparing the Android SDK

In order to create an Android mobile app, we'll need the Android SDK working on our system. For seasoned Android developers who've already got this running, feel free to skip to the next section!

To begin the Android SDK process, you first will need to check you've got the Java SE SDK installed on your system. If you aren't sure, try skipping to the Android SDK install step first. The installation will complain if you don't have Java!

Which Java?

If your system is Java-free, it can be a bit confusing to know which of the various Java options you'll need. Head to the Java SE download page and choose the JDK download option:

Download Java

Once you've got that downloaded, run through the very typical install process that looks a bit like so:

The Java Install Screen

Android SDK Download and Install

If you are new to Android development, you'll need to download the Android SDK and install it. You only need the "SDK Tools Only" bit of the SDK which is available at their Other Download Options page. This provides the bare minimum. You could download the whole Android Studio package if you're looking to do a lot of Android app development in future. If you are sticking with the "SDK Tools Only" option, head to that page and choose the version for your operating system:

Downloading Android SDK

Run that installation file you've downloaded. If you weren't sure if you had Java installed, the installation should confirm this for you:

Java Exists!

Follow the rest of the prompts (they are pretty standard installation prompts) and install the SDK to your system. When it is done, keep the checkbox ticked before you click Finish, that way you can load the SDK Manager:

Android SDK Success

The SDK Manager should appear and look like so:

SDK Manager

It is likely to have a bunch of checkboxes already selected for you. Let's reduce it to just what we'll need for now. Underneath the "Tools" folder, you'll want the following selected:

  • Android SDK Tools - This should be at the top of the list.
  • Android SDK Platform-tools - This will be right after the SDK Tools.
  • Android SDK Build-tools - You should only need the latest revision of this (you can see the revisions under the "Rev." column)

That should look like so:

SDK Manager Install Options

Then, within the latest Android API folder (whichever is highest, at the time of writing that's "Android 6.0 (API 23)"), choose the following:

  • SDK Platform - You'll need this!
  • A system image - You can choose either an ARM or an Intel system image to allow you to emulate that Android system on your computer. If you're looking to do all testing on your physical Android device, you can skip this.
  • Google APIs - This will let us use any Google APIs with our app. This doesn't include Google Cardboard - I've just included it for convenience, you can likely skip this one too.

That should look like the following:

SDK Manager System Images

Click "Install X packages" in the bottom right corner to begin the installation process. You'll need to accept the licence before it will let you install.

Once that is installed, you should have everything you need on the Android side of things!

Creating A Cardboard Empowered Unity Project

From the Cardboard SDK Github repo linked above, all you'll really need to download is the Cardboard SDK For Unity .unitypackage file here.

Once that has downloaded, we're ready to begin our actual Unity project. Open up Unity and do the obvious first step - create a new project.

Create a new Unity project

Choose 3D and give your project a name. Don't worry about adding asset packages on this screen, we'll be adding our custom one in the next step. Select "Create Project" once you're ready to begin:

Naming the Unity Project and Setting It to 3D

Next, go to Assets > Import Package > Custom Package... and find your Cardboard SDK .unitypackage file you downloaded earlier.

Add custom package

If you are using Unity 5, you won't need the legacy folders and can untick those:

Untick legacy for Unity 5

When that is successful, your new Cardboard SDK assets should appear in your project inside a "Cardboard" folder:

Cardboard Assets Added

Within the Cardboard SDK folder, you'll find four subfolders - "Editor", "Prefabs", "Resources" and "Scripts":

Within the Cardboard Folder

Inside the "Prefabs" folder, you'll find a Prefab called "CardboardMain.prefab". Drag that into your scene and raise it up by 2 on the Y axis (otherwise the user will feel like they're either tiny or super short!):

CardboardMain Prefab Folder

Open up the "CardboardMain" prefab and you'll find the "Main Camera" within it. This is our two stereoscopic style cameras working in unison to display the scene in wonderous VR when looking at it though a Google Cardboard headset. In our "Main Camera" object, we'll want to add a "Physics Raycaster" component. To do so, click "Add Component" whilst you've got the "Main Camera" open. I typically find it by typing in "raycaster" in the window that pops up. We'll need a Physics Raycaster to be able to interact with elements by looking at them.

Adding a Raycaster

Next, you can add in an icon to show when the user is looking at an object. I used a free icon in the Unity store from the 64 Flat Game Icons pack by Office57. You could create your own sprite for this too if you'd prefer. Get a sprite from somewhere and import it into your project.

Create an empty game object within the "Head" object in "CardboardMain". I've renamed mine as "Target". Then drag your sprite inside this object:

Dragging sprite into empty game object

Continue reading %Building a Google Cardboard VR App in Unity%


by Patrick Catanzariti via SitePoint

No comments:

Post a Comment