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.
- Download the Unity download assistant from: https://unity3d.com/get-unity
- Make sure to install the Android Build Support component during the installation.
This will install the Android SDK, which we need to deploy application in an Android device.
Download the installer from:
http://developer.android.com/sdk/index.html
Download the latest version from:
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Download the SDK Unity package from:
https://github.com/googlesamples/cardboard-unity/blob/master/CardboardSDKForUnity.unitypackage?raw=true
Nature Starter Kit 2:
https://www.assetstore.unity3d.com/en/#!/content/52977
"Unity-chan!" Model:
https://www.assetstore.unity3d.com/en/#!/content/18705
- Android 4.1 or up.
- Gyro sensor.
- NFC support (Optional).
Create a 3D project scene:
In this case I have the following elements in my scene:
Directional Light: Default light created by Unity.
Main Camera: Default camera created by Unity.
Terrain: I've created the environment using a terrain object from the Nature Starter Kit.
Character: I've created a Unity Chan character object from the "Unity-chan!" Model.
Once your scene is ready, you can start the integration for the Google Cardboard SDK for Unity and import the Cardboard SDK package.
Select the Import Package option:
Select the downloaded Cardboard SDK CardboardSDKForUnity.unitypackage.
Select the elements from the package (we omitted the iOS components because we don't need them):
Select "Import". The files will be created in the Cardboard folder in your project.
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.
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.
Adjust the position of the camera to the appropriate height.
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.
Create a new script: This script will have the behavior from Cardboard device.
Create an empty object and add the created script:
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:
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.
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.
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.
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.
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.
Create a new script for the gaze behavior. This script should inherit from ICardboardGazeResponder. Set your desired behavior in the trigger events. For example:
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.
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:
Now you're ready to install your application and test it on your own device.
Open the project build settings.
Select the Android platform and open the "Player Settings..." option.
The Cardboard device require the application to be in landscape mode. In Resolution and Presentation, change the default orientation to "Landscape Left".
In Other Settings, setup your application Identification in the Bundle Identifier field. For example edu.innovrtors.GoogleCardboardDemo.
Enable your Android device to allow USB debugging in Settings -> Developer options>.
Select the folder to build the application and click in Build.
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.
For example:
- SDK: C:/Users/MyName/AppData/Local/Android/sdk
- JDK: C:/Program Files/Java/jdk1.8.0_60
Your application should appear now in your device. Enjoy it!
Created by:
Ángela M. Benavides