Google Daydream Hello World Tutorial
By Ruiqi Mao
This tutorial assumes that you have completed the Google Cardboard Interactivity tutorial.
You will also need a Google Daydream-capable device, along with a Daydream headset and Instant Preview installed on the device (https://developers.google.com/vr/develop/unity/instant-preview).
1. Create a new 3D project in Unity.
2. Under File > Build Settings, change the target platform to Android and open the Player Settings.
3. Under "Other Settings", give your application a package name and change the minimum target API to Android Nougat 7.0.
4. Under "XR Settings", add Daydream as a supported platform.
5. Import the GoogleVR asset package the same way you did in the Cardboard Hello World tutorial.
6. Add the GvrControllerMain, GvrEditorEmulator, GvrEventSystem, and GvrInstantPreviewMain prefabs to the top level of your scene hierarchy.
GvrControllerMain: This lets Unity recognize the Google Daydream controller.
GvrEditorEmulator: This lets Unity run our Google Daydream application in the player.
GvrEventSystem: This will be used to trigger events with our Daydream controller.
GvrInstantPreviewMain: This will be used in sync with the Instant Preview application on your Daydream-capable device. It will allow us to either stream the entire VR experience to your device or just stream the controller data to the Unity editor when testing.
7. Create an empty object in the scene hierarchy and name it "Player" (Right click in the hierarchy > Create Empty). Drag the Main Camera into the new object, and drag a GvrControllerPointer prefab into the Player object as well. This allows the player to visualize where the Daydream controller is pointing.
8. Make sure the Player object and the Camera object are both at (0, 0, 0).
9. Create a cube (Right click in the hierarchy > 3D Object > Cube) and place it at (0, 0, 5).
10. Now we're going to make it so that we can interact with the cube and drag it around with our controller! Start by attaching a GvrPointerPhysicsRaycaster to our Main Camera like we did in the Cardboard Hello World tutorial.
11. Now, attach an EventTrigger component to the cube. Then, create a new C# script (we'll call it "CubeBehavior"), and attach it to the cube as well.
12. Open up the CubeBehavior script and place the following in:
The EventExtensions class is a helper class that makes it easier to add events to the EventTrigger object. It is the same code as the code seen in the Cardboard Interactivity tutorial.
We add two listeners to the cube: PointerDown and PointerUp. PointerDown is triggered when the controller's main button is pressed down while the cube is being pointed at. PointerUp is triggered when the controller's main button is let go while the cube is being pointed at.
When we press the button down, we attach the controller's transform to the cube as its parent. This lets us fix the cube's relative transformation to our controller, essentially allowing us to drag the cube around.
When we let the button go, we want to release the cube. This is simply done by removing the transform's parent.
13. Now we can test! Attach your Daydream-capable device to your computer, open up the Instant Preview application, and hit Play in the Unity editor. After syncing the controller, you should be able to move the cube around with your controller!