Build a Simple Gaze Interaction for Google Cardboard VR in Unity
This step‑by‑step guide shows how to set up Unity with Google’s Cardboard SDK, import the required assets, add gaze‑based interaction components, write a C# script to change object colors on focus, link events, and finally package the project for Android devices.
Introduction
The article focuses on developing a VR experience for Google Cardboard using Unity. It compares Cardboard with Daydream and explains that the tutorial will cover creating a simple gaze‑based interaction in a Unity scene.
Software Preparation
Unity Editor (version 5.4.2f2 personal)
Visual Studio 2015 (C# editor)
Google VR SDK for Unity (GoogleVRForUnity.unitypackage)
Android SDK (recommended with Android Studio)
JDK (environment variables required)
Cardboard headset (available commercially or DIY)
Development Workflow
The process is divided into five main steps: import resources, add interactive objects, write code, link objects‑code‑events, and build for Android.
Import Resources and Set Up Binocular View
Create a new Unity project and import the downloaded GoogleVR.unitypackage via Assets → Import Package . Two folders, GoogleVR and Plugins, appear in the Assets directory.
Drag Assets → GoogleVR → Prefabs → GvrViewerMain.prefab into the hierarchy, select it, and set Screen Size to Nexus 5 and Viewer Type to Cardboard May 2015. This configures the scene for a stereoscopic (dual‑eye) view.
Run the scene to verify the split‑screen effect.
Add Interactive Object
Select the Main Camera , click Add Component in the Inspector, and add GvrPointerPhysicsRaycaster. This component enables the camera to emit a ray that can interact with colliders.
After adding the component, a white reticle appears in the center of each eye view, indicating the gaze point.
Create a Sphere (Hierarchy → 3D Object → Sphere) and place it within the camera’s view range.
Write the Gaze Script
In the Assets folder, create a Scripts directory, then add a new C# script named GazeToSphere. The core code is:
using UnityEngine;
using System.Collections;
public class GazeToSphere : MonoBehaviour {
void Start () {
ChangeColor(false);
}
void Update () {}
public void GazeEnter() {
ChangeColor(true);
}
public void GazeExit() {
ChangeColor(false);
}
public void ChangeColor(bool gazedAt) {
GetComponent<Renderer>().material.color = gazedAt ? Color.black : Color.white;
}
}The script inherits from MonoBehaviour. Start() initializes the sphere’s color, GazeEnter() and GazeExit() are called by the gaze system, and ChangeColor() switches the material between black (focused) and white (unfocused).
Start() : called once when the object is created.
Update() : called every frame (unused here).
GazeEnter() : invoked when the gaze ray hits the object.
GazeExit() : invoked when the gaze leaves the object.
Remember to save the script in Visual Studio; otherwise Unity cannot reference the methods.
Link Object, Script, and Events
Select the Sphere , click Add Component , and attach the GazeToSphere script.
Add an Event System (Hierarchy → UI → Event System). With the Event System selected, add the component GvrPointerInputModule.
On the Sphere , add an Event Trigger component. Create a new event type Pointer Enter and assign it to GazeToSphere → GazeEnter. Then add another event type Pointer Exit and link it to GazeToSphere → GazeExit. Ensure the script file is saved so Unity can locate the methods.
When the gaze ray hits the sphere, the reticle changes and the sphere turns black; when the gaze leaves, it returns to white.
Package for Android
Save the scene ( .unity file), then open File → Build Settings . Select Android as the target platform and click Open Download Page to install the Unity Android Support module (UnitySetup‑Android‑Support‑for‑Editor). After installation, restart Unity.
Configure SDK, JDK, and (optionally) NDK paths via Edit → Preferences → External Tools . Ensure Visual Studio is set as the external script editor.
In Build Settings, click Add Open Scenes to include the saved scene, then open Player Settings . Set a unique package name (company name + product name, no digits). Under Resolution and Presentation , select Landscape Left (or Right) because Cardboard expects a horizontal orientation.
Finally, click Build And Run , choose a destination folder, and connect an Android phone via USB. Unity compiles the APK, installs it, and launches the app. Place the phone into a Cardboard headset to experience the gaze‑controlled sphere.
Tencent Music Tech Team
Public account of Tencent Music's development team, focusing on technology sharing and communication.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
