<< Back to tutorials

Unity and Google Cardboard Tutorial
 Live on: May 5 2016 at EN 3026

The Google Cardboard puts virtual reality on your smartphone. This tutorial is a guide to create your first Google Cardboard application and deploy it on your phone.


Unity 5

- Download the Unity download assistant from: https://unity3d.com/get-unity

- Make sure to install the Android Build Support component during the installation.

Unity Installer

Android Studio:

This will install the Android SDK, which we need to deploy application in an Android device.

Download the installer from:

Java Development Kit (JDK)

Download the latest version from:

Cardboard SDK for Unity

Download the SDK Unity package from:

Additional assets (Optional)

Nature Starter Kit 2:

"Unity-chan!" Model:

Android Mobile Device

- Android 4.1 or up.
- Gyro sensor.
- NFC support (Optional).

Google Cardboard device

Create your project

Create a 3D project scene:

In this case I have the following elements in my scene:

  1. Directional Light: Default light created by Unity.

  2. Main Camera: Default camera created by Unity.

  3. Terrain: I've created the environment using a terrain object from the Nature Starter Kit.

  4. Character: I've created a Unity Chan character object from the "Unity-chan!" Model.

Import the Cardboard SDK for Unity

Once your scene is ready, you can start the integration for the Google Cardboard SDK for Unity and import the Cardboard SDK package.

  1. Select the Import Package option:

  2. Select the downloaded Cardboard SDK CardboardSDKForUnity.unitypackage.

  3. Select the elements from the package (we omitted the iOS components because we don't need them):

    Package elements

  4. Select "Import". The files will be created in the Cardboard folder in your project.

    Package elements

Using the Cardboard SDK for Unity

Now you're ready to setup the camera for the Cardboard application.

Basically, what we need is to change our project to enable VR support.

  1. Create a new camera with a CardboardMain prefab. This prefab contains a full stereo rig, with a camera tagged MainCamera, and an instance of the Cardboard script for controlling VR Mode. You can delete the Main Camera that was created by default and replace it with this.

    Package elements

  2. Adjust the position of the camera to the appropriate height.

Once this is done, your application is ready to support VR and can be deployed on your device. You will be able to look around the scene.

Setup a trigger event

Now you're ready to add a trigger event and be able to execute an action every time the magnetic button is used in the cardboard. For this example, we'll use the button in the Cardboard device to walk in the direction that the device is gazing at.

  1. Create a new script: This script will have the behavior from Cardboard device.

  2. Create an empty object and add the created script:

    Package elements

  3. Set the behavior for the button in the Cardboard device. Use the Cardboard.SDK.Triggered event to trigger the action desired every time the button is pulled. For example:

    using UnityEngine; public class CardboardBehaviour : MonoBehaviour { public GameObject player; public GameObject _camera; private bool walking = false; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Cardboard.SDK.Triggered) { walking = !walking; } if (Cardboard.SDK.BackButtonPressed) { Application.Quit(); } if (walking) { Vector3 direction = _camera.transform.forward; direction.y = 0; direction.Normalize(); player.transform.Translate(direction * Time.deltaTime, Space.Self); } } }

    Once this is done, your application is able to trigger events from the Cardboard device, so every time you pull the button, the camera will move in the direction your looking at, like walking in that direction.

Select a target at gaze

You can add a target behavior to your application to be able to select target objects when you're looking at them. For this example, we'll interact with the Unity Chan character and change her animation every time we look at her.

  1. Create an EventSystem object. This object will have the GazeInputModule, which allows the user to select elements by looking at them. Initially add an EventSystem component.

    Event system

    Note: Usually, the object is created automatically after using some prefabs from the Cardboard SDK package, so use this one instead of creating a new one.

  2. Add the GazeInputModule script to the EvenSystems object. Make sure this component is on top of the TouchInputModule and StandaloneInputModule scripts, if it's created below this, move it up with the "Move Up" menu.

    Gaze Input Module

  3. Create a new script for the gaze behavior. This script should inherit from ICardboardGazeResponder. Set your desired behavior in the trigger events. For example:

    using UnityEngine; public class CardboardTargetBehaviour : MonoBehaviour, ICardboardGazeResponder { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } public void OnGazeEnter() { SetGazeAt(true); } public void OnGazeExit() { SetGazeAt(false); } public void OnGazeTrigger() { } public void SetGazeAt(bool gazeAt) { if(gazeAt) GetComponent<Animator>().SetBool("Next", true); else GetComponent<Animator>().SetBool("Back", true); } }
  4. Add the target behavior script as a component to your target object. In this case, we'll use the Unity Chan character as our target.

  5. Add and EventTrigger to your target object and set up the events to the method in your target behavior script. Also add a Box Collider to it. Your target object should look like this:

    Event trigger

Once this is done, your application is ready to interact with objects in your environment, in this case with the character on it.

Build and deploy your application

Now you're ready to install your application and test it on your own device.

  1. Open the project build settings.

    Event trigger

  2. Select the Android platform and open the "Player Settings..." option.

  3. The Cardboard device require the application to be in landscape mode. In Resolution and Presentation, change the default orientation to "Landscape Left".

    Event trigger

  4. In Other Settings, setup your application Identification in the Bundle Identifier field. For example edu.innovrtors.GoogleCardboardDemo.

  5. Enable your Android device to allow USB debugging in Settings -> Developer options.

  6. Select the folder to build the application and click in Build.

  7. The first time you build your application, Unity will ask you for the Android SDK and the JDK installation folders. This information can be updated in External Tools.

    External Tools

    For example:
    - SDK: C:/Users/MyName/AppData/Local/Android/sdk
    - JDK: C:/Program Files/Java/jdk1.8.0_60

  8. Your application should appear now in your device. Enjoy it!

Created by:
Ángela M. Benavides


<< Back to tutorials