In this codelab, we will walk you through the various ways of displaying video in Daydream. You will also create a simple UI that will allow you to control the playing video.
What you'll learn
Create a new Unity project that uses the Google VR Unity SDK.
How to import the Google VR Unity SDK package.
How to adjust the build settings to target Daydream devices.
Deploy a Daydream app onto a device.
Display flat and 360° videos.
Play videos from local files and streaming services.
Create a world space UI that interacts with the Daydream controller.
Tell us a little about yourself
How will you use this codelab?
Only read through itRead it and complete the exercises
How would you rate your experience with building Unity applications?
Never triedNoviceIntermediateProficient
How would you rate your experience with Google Daydream?
Never triedNoviceIntermediateProficient
How would you rate your experience writing code?
Never triedNoviceIntermediateProficient
Hardware you'll need
A development machine running Windows 7 or higher, or Mac OS.
Unity 5.6 or greater. You can download and install the latest version of Unity here. When installing Unity, make sure you install Android Build Support as well.
Download and install the latest version of Android Studio. This sets up Android tools like adb and downloads the Android SDK for you.
In order to run samples, you'll need to enable debugging over USB on your device. To do so:
Go to Settings
Go to About phone (usually at the bottom)
Tap Build number (usually at the bottom) seven times. You should see a toast saying you are now a developer.
Return to the previous screen to find Developer options at the bottom.
From the Developer options menu, find USB debugging and turn it on.
When you connect your phone to your development platform, you will need to authenticate debugging from the phone. Allowing this will enable your machine to control your device.
Download The Google VR SDK for Unity
You will need this SDK to enable Daydream development in Unity. To do so, download it as a .unitypackage to your computer.
To get started, we'll need to create a new project from scratch. Go ahead a launch Unity.
After unity boots up, you will be greeted with a welcome dialog. Click the New button at the top to setup a new project.
Set the Project name to "daydream-video-ui"
You can set the Location to whatever you prefer.
If this is your first time using Unity, or you are still a beginner, let's walk through some important parts of Unity.
Using the Default Layout
First off let's use the default layout for the editor. At the top right of the editor, you will see several drop downs. Click the Layout dropdown and select Default. This will arrange the editor windows for you.
Unity's Windows
In Unity, there's several windows you should be aware of. Feel free to explore the provided links, but don't worry if it feels like a lot, we'll walk you through some of the first time steps.
The Project window: this is where you can access and manage the assets that belong to your project. Assets are files on disk that represent the various aspects of your project. We'll spend a lot of time here finding assets and creating/using prefabs
The Hierarchy window: Scenes are made up of various GameObjects, this is where you will see the hierarchy of GameObjects in the current scene. You can set up parent-child relationships here.
The Inspector window: This is the window that will list the various Components of a given GameObject. We'll spend a lot of time here adding Components and editing properties. Inspector window also lets you edit properties of scene GameObjects, Project prefabs, and other various assets.
The Scene View: This is a visual representation of your scene's hierarchy.
The Game View: This is the view of the game when you are running in the editor.
The Console window: this is where you can view the errors, warnings and other messages generated by Unity. Unity displays the most recent message on the status bar at the bottom of the IDE, but you can switch to the Console tab on the bottom pane to see all messages.
Building for Android
First thing we need to do is configure Unity to build the project for Android
Click on File -> Build Settings
Change the target platform by selecting Android under the Platform menu.
Click on Switch Platform.
If everything was setup correctly, it will look like this (note the Unity symbol to the right of Android).
Configuring the Player
Click on Player Settings... at the bottom of the Build Settings window. It will update the Inspector window in the right panel with the PlayerSettings.
In the Inspector window, expand the Other Settings.
Check the box for Virtual Reality Supported.
Click on the + button and select Daydream SDK.
Set the Package Name attribute to "com.example.daydream.video.ui"
Finally set the Minimum API Level to 24 or greater.
Importing the Google VR SDK
If you haven't done so already, grab the the latest version of the Google VR SDK for unity. The latest .unitypackage is available to download from GitHub here.
Open the .unitypackage from Windows Explorer or MacOS Finder.
Unity will extract the package and prepare it's files for import. You'll get a confirmation dialog first that list all the files. Click Import to proceed.
After the import operation, you will now see a GoogleVR option appear in the system Menu.
Importing the GVRVideoPlayer package
Within the Google VR SDK, there will be another .unitypackage used just for video.
In the Project window, type "GVRVideoPlayer" in the search bar.
Double click the .unitypackage file to start the import process.
You'll again see a list of files, Click Import to proceed.
Importing the Codelab Project
Now we'll repeat the process for the provided Codelab project. Importing this project into Unity will set up the editor with the proper setting and include some assets we'll use later on in the Codelab. If you haven't done so already, grab the .unitypackage from GitHub here.
Open the .unitypackage from Windows Explorer or MacOS Finder.
Click Import to proceed.
Open the starter scene
Now that we've imported all the assets, we'll setup our main scene. In the Project Window, select the Scene folder
Double click the Starter scene to open it. You can use the zoom slider at the bottom right to switch between icons and a list view of the assets in the folder.
In the Hierarchy window, you should just have the Main Scene with an Environment Object and a Main Camera.
The scene provided places the player on an icy island. The Game view should look like this, which will be the perspective of the player when it is run on the headset.
The Arm Model
When using Daydream, it's best to set up an arm model to mimic where the user's hand is. An arm model is a mathematical model for approximating the location of the user's hand in virtual space. The Daydream controller only has rotational values, but we can use that in combination with the headset's information to create an accurate estimate of where the user's hand is.
Working with Prefabs
In Unity, a Prefab is a form of a template. You can create prefabs and then re-use them multiple times in a scene without having to create them over and over again. Through the course of this Codelab, we will provide some prefabs already made for you to use, and walk through the construction of one later on.
Using the SDK Prefabs
In order to do this, there are some foundational prefabs that are needed in order to set up a Daydream Arm Model.
GvrEventSystem: this is the main event system prefab that the various Unity components use to send events with. It acts as the glue between Unity's Event System, Input Module, UI and GameObjects to allow interactions with the Daydream controller.
GvrControllerMain: Used to setup the parameters of the arm model.
GvrControllerPointer: This takes the Arm Model information from the GvrControllerMain and creates a GameObject hierarchy out of it.
GvrEditorEmulator: If you are running from the editor, this prefab will make the editor mimic the VR environment, so you can get a sense of what the scene would look like when it is running through the headset.
You can read more about the Daydream controller and the provided prefabs here and here. We'll add them to the scene now.
Adding the Prefabs to the Scene
In the Project window, type "t:prefab GVR" in the search bar, which will find all prefabs with the phrase "GVR".
If you see a lot of blue cubes and can't see the names, use the zoom slider to make them larger, or shrink them down to a list.
Select the four prefabs we are using.
Drag and drop them on the Hierarchy window. Don't worry about order, it is not important in this context. In the end your hierarchy will look something like this.
Setup the Player
When creating Daydream applications, you'll want to have a Player GameObject in every scene. The Player will consist of the two elements that the user will use to interact with the scene: the camera and the controller. The camera will be used to position the viewpoint of the headset, ie- the root/head. The controller will be a hierarchy that represents the user's arm in the scene.
From the Menu, Choose GameObject -> Create Empty. You will see a new GameObject in the Hierarchy window.
In the Inspector window, rename the object "Player".
Set Position to (0, 1, 0)
In the Hierarchy window, find the Main Camera GameObject
Make the Main Camera GameObject a child of the Player GameObject by dragging onto the Player GameObject. The Player GameObject will be highlighted when it is properly dragged.
In the Hierarchy window, select the Main Camera GameObject
In the Inspector window, set Position to (0, 0, 0)
Set Clipping Planes/Near to 0.01
We also want to make the GvrControllerPointer GameObject a child of player. Drag it onto player as well.
Set it's Position to (0, 0, 0)
Your hierarchy will look like this when you are done:
Now we are ready to build and run our first Daydream APK!
Building
Connect your phone to your computer via USB.
From the menu, select File > Build Settings. In the Build Settings window, click On the Build and Run button at the bottom right.
In the file selection dialog, specify your desired location for the resulting APK file. Unity will build, install, and run the apk on your device.
Running
If you have not yet put your phone in your headset, do so now. Ensure the USB cable stays connected, and that the volume/power buttons are on the top. If you just see a black screen, double check to make sure the device is on!
Controllers
Controllers are a required component of Daydream apps, so if your device does not have a controller paired to it, you will see something like this:
Every Daydream View comes with a remote included. If you don't have access to a Daydream view, you can emulate the remote using the Controller Emulator APK.
Initial Orientation
If everything is going smoothly, you should see a landscape with a controller floating in front of you. Follow the instructions and you should then see your application.
If you don't see anything try pressing and holding the home button at the bottom of the controller. It will recenter your view based on your current orientation. You can hold the home button at anytime to recenter.
Check your Work
After you have calibrated your controller, you will see the Daydream controller being held in your hand.
Open the main scene
If you have ran into any issues, don't fret! We've supplied another scene for this point in the codelab. Even if everything is running smoothly, it's good to use at this point as we've organized the scene a little more.
Now that we've imported all the assets, we'll setup our main scene. In the Project Window, select the Scene folder
Double click the Main scene to open it. You can use the zoom slider at the bottom right to switch between icons and a list view of the assets in the folder.
In the Hierarchy window, your hierarchy should look like this:
First up we'll add a flat video screen object and configure video playback.
Creating the Screen GameObject
First we'll create the geometry for a video screen.
From the Menu, Choose GameObject -> 3D Object -> Quad. There will now be a new Quad GameObject in the Hierarchy window.
In the Inspector window, rename the object "Flat Screen"
Set Position to (0, 5, 12)
Set Scale to (16, 9, 1)
Assigning the Video Material
Next we are going to assign a material to the screen. This is what will be shown while the video is loading.
Under the Mesh Renderer component, set Materials/Element 0 to VideoMono. We are going to assign this field using the Object Picker. To open it, click the small circle next to the field.
You will now see the Object Picker. Similar to the Add Component menu, you can find the object you are looking for by typing it in. Ensure you are in the Assets tab.
Type "Video" to find our material. Hit enter or click it to select it.
Note the updated value of the field.
Adding the GvrVideoPlayerTexture Component
Now that we have a screen object and a loading material, we can add the component that handles the loading/playing of the video, GvrVideoPlayerTexture.
At the bottom of the Inspector window, click the Add Component button
Type in "GvrVideoPlayerTexture" and hit enter or click it to add it
Adding the Auto Play Video Component
Finally, we want to autoplay the video when the application starts.
At the bottom of the Inspector window, click the Add Component button
Type in "Auto Play Video" and hit enter or click it to add it
At this point we have a flat screen ready for playback. All we have to do is setup the video. We'll go through the different types of playback that is offered now.
Now that we have our scene setup, we will start playing some videos. First up are streaming Dash videos. We're going to setup the GvrVideoPlayerTexture component to auto play a dash video over the internet. Generally, GvrVideoPlayerTexture will handle playing most video types, it's just a matter of configuration.
In the Hierarchy window, select the Flat Screen GameObject.
Now build and run the application. You should see the streaming video playing automatically on the screen.
If your video is a large one and causes your APK size to be larger than 100MB, you'll need to use Expansion Files to package it. Thankfully, Unity does a lot of the work for us! To work around this limit, the larger video resources can be stored in another file, called an .OBB file. We'll now setup playback for this type of set up.
In the Project window, select the Assets/StreamingAssets folder
Note the file Assets/StreamingAssets/vid_bigbuckbunny.mp4. Unity treats this any directory called StreamingAssets differently when it packages the application.
Set up the player
In the Hierarchy window, select the Flat Screen GameObject.
In the Inspector window, under the GvrVideoPlayerTexture component, set Video Type to Other
Set Video URL to "jar:file://${Application.dataPath}!/assets/vid_bigbuckbunny.mp4" Note that at runtime the ${Application.dataPath} will be replaced with the actual value of Application.dataPath. This video is a stereo 3D video, so we'll need to change the Flag Screen's rendering material from VideoMono to VideoStereo too.
Clear Video Content ID.
Updating Publishing Settings
Finally, we must update how Unity packages the application.
First thing we need to do is configure Unity to build the project for Android
Click on File -> Build Settings
Click on Player Settings...
In the Inspector window, expand the Publishing Settings menu at the bottom
Open the player settings and in the "Publishing Settings" Section, check the "Split Application Binary" checkbox. This will create the OBB file when the application is built.
Now build and run the application. When deploying the application locally using Build & Run, the obb file is copied to the correct location automatically. More information about how Unity handles .obb files via the Play Store is available here. You should see the local video playing automatically on the screen.
Sometimes you may need to always have a smaller video present, perhaps for loading screens or logo displays. If you stay under the Play store limit, you can store them in a different location and circumvent the need for OBB files altogether.
Setup the Player
In the Hierarchy window, select the Flat Screen GameObject.
In the Inspector window, under the GvrVideoPlayerTexture component, ensure Video Type is Other
Set Video URL to "asset:///raw/vid_bigbuckbunny.mp4"
Ensure Video Content ID is clear
Move the source video
Now that the component is set up, we need to move the source video to it's new location so it will be accessibly via the "asset:///" path. The easiest location to place mp4 files for playback is in Assets/Plugins/Android/assets. All content in this folder is accessible via the AssetManager in the Android SDK.
In the Project window, right click the Assets/ folder -> Create -> Folder, name the folder "Plugins"
In the Project window, right click the Assets/Plugins/ folder -> Create -> Folder, name the folder "Android"
In the Project window, right click the Assets/Plugins/Android folder -> Create -> Folder.
Name the folder "assets"
Add another folder in the newly created one. Right click the Assets/Plugins/Android/assets folder -> Create -> Folder.
Name the folder "raw"
Now we will move the video. Select the Assets/StreamingAssets folder
Begin to drag the vid_bigbuckbunny asset.
Drop the asset in the Assets/Plugins/Android/assets/raw folder
Now build and run the application. It will seem like it is the exact same video, but how it is treated behind the scenes is completely different!
Now we will illustrates how to playback 360° stereoscopic video. This is done by creating a separate camera for each eye and then using the per-eye video rendering from the video SDK.
Creating the 360 Screen
From the Menu, Choose GameObject -> 3D Object -> Sphere. There will now be a new Sphere GameObject in the Hierarchy window.
In the Inspector window, rename the object "360 Screen"
Set Scale to (12, 12, 12)
In the Mesh Renderer component, set Materials/Element 0 to VideoStereoInside using the Object picker like you did before for the flat screen.
Adding the GvrVideoPlayerTexture Component
Once again, we will add a GvrVideoPlayerTexture component.
At the bottom of the Inspector window, click the Add Component button
Type in "GvrVideoPlayerTexture" and hit enter or click it to add it
At the bottom of the Inspector window, click the Add Component button
Type in "Auto Play Video" and hit enter or click it to add it
Hiding the other objects
Since it's a 360° video, we want to hide the flat screen and the environment so they don't get in the way of viewing the sphere.
In the Hierarchy window, select the Flat Screen GameObject.
In the Inspector window, disable the Flat Screen GameObject by ensuring the checkbox by it's name is cleared.
Repeat the steps and hide the Environment Object. In the end your Hierarchy should look like this (note the grayed out font for Environment and Flat Screen)
Now build and run the application. You will see a 360° video playing now!
Now we'll add a simple control to play and pause the 360° video. First up when need to add the Unity objects for the UI.
Adding the Canvas
From the Menu, Choose GameObject -> UI -> Canvas. There will now be a new Canvas GameObject in the Hierarchy window. Canvas is the top level class that any UI in Unity is needed.
In the Inspector window, rename the GameObject "Main Menu"
Under the Canvas component, change Render Mode to World Space.
Under the Rect Transform, Set (Pos X, Pos Y, Pos Z) to (0, 0.5, 5)
Set (Width, Height) to (200, 75)
Set Scale to (0.01, 0.01, 1)
Adding a Layout
Next we'll add a layout, which will automatically position the children of the UI. We'll add buttons later, and this component will arrange them when they are added.
At the bottom of the Inspector window, click the Add Component button
Type in "Horizontal Layout Group" and hit enter or click it to add it
Under the new added component, set Spacing to 15
Set Child Alignment to "Middle Center"
Setting up the Raycaster
We need to remove the default mouse based raycaster from the component and add the Daydream based one, so the UI will respond to the controller events.
Remove the Graphics Raycaster component by clicking the Gear Icon -> Remove Component.
At the bottom of the Inspector window, click the Add Component button
Type in "GVRPointerGraphicRaycaster" and hit enter or click it to add it
Creating a Play Button
Now that we have a canvas, we need to add some buttons to it.
In the Hierarchy window, add a child button object by right clicking on the GameObject Main Menu -> UI -> Button. There will now be a new Button GameObject added as a child to Main Menu.
In the Inspector window, rename the object "Play Button"
Set (Width, Height) to (50, 50)
Replace Label with an Image
We're going to replace the text with an image. In the Hierarchy window, remove the text by right clicking on the GameObject Main Menu/Play Button/Text -> Delete
Add a child RawImage object by right clicking on the GameObject Main Menu/Play Button -> UI -> Raw Image. There will now be a new RawImage GameObject added as a child to Main Menu/Play Button.
In the Inspector window, under the Rect Transform, set (Width, Height) to (50, 50)
Under the Raw Image component, set Material to play using the Object picker.
Creating a Pause Button
We'll now make a second button, but we'll do so by duplicating the play button and changing the image.
In the Hierarchy window, right click and choose Main Menu/Play Button -> Duplicate. There will now be a new Play Button (1) GameObject added as a child to Main Menu.
In the Inspector window, rename the object "Pause Button"
In the Hierarchy window, select the Main Menu/Pause Button/RawImage
In the Inspector window, under the Raw Image component, set Material to pause using the Object picker.
Creating the Main Menu Script
Creating our scripts
Next we need to make some scripts which will allow us to create custom components that will change the behavior of our objects. We'll flesh out the code in these scripts later on.
In the Project window, right click the Assets folder -> Create -> Folder.
Name the folder "Scripts"
Right click again on the Scripts folder -> Create -> C# Script
Name the script "MainMenu"
In the Inspector window, verify your script was created
Attaching the Script to the Main Menu
In the Hierarchy window, select the Main Menu GameObject
At the bottom of the Inspector window, click the Add Component button
Type in "MainMenu" and hit enter or click it to add it
In the MainMenu component, double click the Script field to open up the scripting editor.
Now that you are in the coding editor, you will see an initial class template like so. Next let's flesh out the code!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainMenu : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Fleshing out the Script
First thing we need in our script is a reference to our video and our buttons so we can control them from within the component.
public GvrVideoPlayerTexture video;
public GameObject playButton;
public GameObject pauseButton;
Add two handlers for when the buttons are clicked, OnPlayClicked and OnPauseClicked. These methods will enable/disable buttons and play/pause the video.
public void OnPlayClicked() {
playButton.SetActive(false);
pauseButton.SetActive(true);
video.Play();
}
public void OnPauseClicked() {
playButton.SetActive(true);
pauseButton.SetActive(false);
video.Pause();
}
Hooking up the Buttons to Main Menu
Back in Unity, we'll need to setup the component we just scripted.
In the Hierarchy window, select the Main Menu/Play Button GameObject
In Inspector window, we'll add a listener by clicking on the plus button under the On Click () event.
This will add a listener to the event. Next we need to assign the callback object by clicking on the small circle and to open the Object Picker.
This time we are picking an object from the Scene, so ensure the Scene tab is selected.
Type "Main Menu" to find our object in the scene. Hit enter or click it to select it.
Note the updated field in the Inspector window. Add the callback by clicking No Function -> MainMenu -> OnPlayClicked (). In the end your component should look like this.
Repeat these steps to connect the Main Menu/Pause Button to the MainMenu's OnPauseClicked () method.
Setting the Main Menu References
In the Hierarchy window, select the Main Menu GameObject
In Inspector window, under the Main Menu component, set the Video field to 360 Screen using the Object Picker.
Similarly, set the Play Button field to Main Menu/Play Button using the Object Picker. Make sure you use the Scene tab when searching.
Set the Pause Button field to Main Menu/Pause Button using the Object Picker.
Disable the Play Button
Finally, since the video will autoplay when the application starts, we can disable the Play Button
In the Hierarchy window, select the Main Menu/Play Button GameObject.
In the Inspector window, disable the Play Button GameObject by ensuring the checkbox by it's name is cleared.
Note that when the Play Button is disabled, the position of the Pause button changes. This is because of the Horizontal Layout component we added to the Main Menu earlier.
Now build and run the application. Now you will see a button hovering around the 360° video which will pause and play the video accordingly.
You did it! You've finished the codelab and successfully display various types of videos and added a simple UI controller.
Beyond
If you are looking for some more interesting things to do try:
Adding more complex controls, like positional and volume controls.
Adding a gallery to browse videos.
Check out the example scene Assets/GoogleVR/Demos/Scenes/VideoDemo