[MUSIC PLAYING]
If you build a game for iOS or Android using Unity,
I'd love to see it on my TV, and you
can do so by using the Google Cast Display plug-in for Unity.
We've put together an Android code lab
showing you how to do so.
So let's take a look.
Let's get started with adding Cast's support
to an existing game in Unity.
First thing we do is we open an existing project.
We'll open this project that we've called Begin Project,
and then we'll fire up inside the Unity IDE.
Now that we're actually inside the IDE,
we could try playing this project
as is by pressing the Play button.
Now we see the game running, and we
can explore our world, fire, do all sorts of stuff,
and hopefully not get killed.
Now that we know the game is working,
we can start by adding support for the Android
SDK inside Unity.
To set up the Android SDK inside Unity,
first you need to tell it where it is.
So you go to Preferences, External Tools,
look for the Android SDK line, choose Browse,
and locate the position on your hard disk,
where you've actually installed the Android SDK.
In this case, it's right here.
So I click Choose, and now Unity is
ready to work with the Android SDK.
Next we'll need to configure the Android player so that Unity
will build for Android.
What we do is we click the File Menu, choose Build Settings,
then look for Android in the list,
select it, if it's not already selected,
and click Switch Platform to make it
the default build target.
While that happens, it will start importing the assets
and getting ready to build an Android application.
After we've imported everything for Android,
we now need to configure the bundle identifier.
Come over here to the Player Settings button.
If you click that, you'll see over here in the Inspector
we have the Play Settings.
We click on Other Settings.
It brings up a whole lot of options.
First thing we do is we change the bundle identifier.
So for our example, we'll just set it
to something like example.game.
We also need to change the minimum API version
because the Cast plug-in requires
Android KitKat 4.4 or higher, so we choose 4.4 KitKat.
Now that we've configured everything,
we can just close the Build Settings dialog.
And if we save our project by going File, Save Project,
we can then go File, Build & Run.
What we should do at this point in time
is actually connect an Android device.
So we'll plug it in, click Build & Run, and hopefully,
build our application to run on our phone,
and we'll call the project game.
Now, while we wait for that to build,
we'll get on to the next thing, which is actually playing
the game to see how it works.
Now we have our game running on the phone, so swipe
from the top to the home screen and quit the game.
Next, we'll get on the Cast, enabling the game itself.
To begin development of your Cast-enabled game,
you need to go to the Cast Developer Console.
On the console, you can add new applications
that will run on the Cast device and register devices
for debugging.
So we'll click Add New Application
and choose the Remote Display Receiver type.
This will receive the content from your phone.
So we'll call it My Game.
Once you click Save, you will be issued an application ID
that you can see here.
You'll use that in your project.
The other thing we need to do is register a debugging device.
If I click the Add New Device button,
whilst this dialogue is up, I can click the Cast extension
in Chrome, which will show me Cast device that are available.
If you don't have the extension, you
can get it from the Chrome Web Store.
If I click on that device, it will show the serial number
and read it out to me, so I can enter the serial number
into my dialog box here.
So the serial number that it's read to me
for this particular example I'll just
type in and give it a name.
And we'll call it PlayRoom.
Now, when we click OK, what you'll see
is a registering dialogue.
Now this status here registering takes approximately 15 minutes
to complete.
So this is good time go and get a cup of coffee and come back.
After your device is registered for development,
you'll see these Ready For Testing
status in the Cast Developer Console.
At this point, you should reboot your Cast device,
and we'll get ready to set up the Google Cast plug-in.
Now that we've set up our device for testing,
we need to download the Google Cast Remote Display plug-in
from the Unity Asset Store.
Once we've downloaded it, we can go back into Unity,
choose Assets, Import Package, Custom Package,
and then select the plug-in and click Open.
This will bring up an input package dialogue.
Here we click the Import button to all the assets
into our project.
After the plug-in has finished loading all the assets,
you should be able to go down here to the Plug-ins folder
and check you have all the folders for the Google Cast
Remote Display plug-in available to you.
Note, at that this point, we need
to copy three of the Android SDK libraries
into the Plug-ins folder.
That's the google-play-services library, the appcompat library,
and the mediarouter library.
Now that we've set up the plug-in,
we need to go to the Android directory
and open the Android.manifest.xml file.
You'll see it over here in the Inspector.
Now, if we bring it up in our editor,
we need to change the package name
to be the same as what we set up in Unity.
So we set it as example.game save the manifest file.
The next thing we need to do is add an audio listener
to our project so that we can send audio the Cast device.
We go up to the Assets menu, choose Assets, Create,
C# Script, and then we'll rename that to be
AudioListenerSingleton.
Now we open that file up in a text editor,
and we can actually change the code
to implement the audio listener for us.
The example is in the code lab referenced from this video.
Next we need to add the audio listener to the scene.
But to do that, we go to Game Object,
Create an Empty Game Object.
We can rename that to the be our AudioListenerSingleton.
And if we navigate down into our Plug-ins folder,
where the Google Cast Remote Display plug-in is,
you'll see the audio listener inside there.
So we select it, and we drag it and drop it
onto our AudioListenerSingleton.
You'll notice that the display on the right here
in the Inspector has changed to show the script we just added.
Meanwhile, we need to add the script we
created in the previous step.
So we go back into the Android folder, where
we had created that script, find that AudioListenerSingleton,
and drag it on as well.
You'll see the second script up here
in the right-hand inspector.
Now what we need to do is actually add the remote display
to the scene, and we'll be all done.
Now let's add remote display to the Unity scene.
What we need to do is go to the Google Cast Remote Display
folder here and find this prefab called Google Remote Display
Manager and drag it into our scene hierarchy
at the top level.
If we then select it and go over to the right,
we need to set a few things.
First thing we set is the Audio Manager.
We want to set it to the AudioListenerSingleton
we created in the previous step so that we
can send audio to the device.
Next, we need to create a camera.
After you've added the Cast Remote Display Manager
to your project, you need to set the app ID
so that the Cast device knows which app to load, which
is over here in the Inspector.
So what we do is we go back to our Cast SDK Developer Console,
grab that ID, and copy it.
Once we've copied the ID, we can put it into Unity,
so now we know-- the Cast device knows
which application to load.
Next, we'll add the mobile camera.
To create the camera, we choose Game Object, Camera.
This is the camera that will show
the image on the mobile phone when casting,
so we'll call it the mobile camera.
Now, if we select the mobile camera,
we need to remove the Audio Listener over here
because it can interfere with our audio routing
to the Cast device, so we remove the component.
We also need to remove it from the main camera.
So we go across here to the Audio Listener
and get rid of it.
Now that we've created the cameras,
we need to wire in a script that will control them.
To prepare the script to control the camera,
we go to the File System and go to end_project, Assets,
Scripts, Camera, and find the RemoteCameraManager.cs script
file.
We take that, and we'll copy it over into our project.
So if we drop it in there, we have
the script for the mobile camera, which
provides the view for the mobile device during casting,
and the main camera, which is shown on the Cast device.
Now that we've copied the script, we get back to Unity
and set it up for use.
If we go into the Scripts, Camera folder,
you'll see the RemoteCameraManager there.
We just grab it and drag it onto our Mobile Camera.
Then if we look at the Mobile Camera in our Inspector,
we can see the script enabled.
We need to change a few settings here.
Change the Display Manager to be the CastRemoteDisplayManager.
We'll also set the Remote Display Camera to be the Mobile
Camera that we've just built. Then, of course, the Main
Camera will be the Main Camera.
So now we've set up all our cameras.
We're ready to go.
What we need to do is add some kind of UI
to allow casting to happen.
To set up a simple UI to start casting from our phone
to the display, we go back into the Google Cast Remote Display
Plug-ins folder and find the Cast Default
UI, which is a prefab that provides a Cast UI.
We drag that into the top level of our hierarchy,
and that will give us a nice UI.
And we're ready to go.
All we need to do now is choose File, Build And Run,
and watch it run on our iPhone.
So we've built our game, and, lo and behold,
it has a Cast icon, which lets us
send the game play to any Google Cast enabled device.
For more details, go to the code lab, which
explains how to make the Cast icon transparent,
change colors, and add controls to move your player
around the game so it actually does something.
I hope you enjoyed a quick tour through how to make
your game Google Cast enabled.