ANDREW BROGDON: Hey, everybody.
I'm Andrew Brogdon.
Welcome back to the "Mobile Ads Garage."
Today, with the help of my intrepid partner, Gary,
the graphics guy, we're going to cover the AdMob
plug-in for the Unity game development platform,
and show how to use it to display rewarded video ads.
But first, I have a really important announcement to make.
[TRUMPET PLAYING]
Thank you, Gary.
If you saw our previous episode on rewarded video,
you know that when that format first
launched it was only available through mediation,
and AdMob didn't have its own ads to display.
Well, I'm happy to say that after so many of you
guys went out and implemented rewarded video
in your apps, that's changed.
You can now go to apps.admob.com,
create a rewarded video ad unit, and with no mediation required
get live ads directly from AdMob's advertisers.
So if you're not using mediation,
you don't have to miss out on rewarded.
And if you are mediating, that's one more network
competing for your impressions.
Today we're going to put these new rewarded video
ads to work in a Unity app.
So let's talk about that.
Unity has a cross-platform game engine that lets you write apps
once in C# and then compile them for a number of different
platforms, including Android and iOS.
AdMob created a plug-in to make it easy to use the mobile ads
SDK with Unity.
There's a single C# interface for publishers to code against.
And then under the hood, the plug-in
takes care of communicating with the native iOS and Android
SDKs.
So you're actually dealing with the same SDK that you normally
would, but doing it with calls into C# classes.
The plug-in offers banners, interstitials, rewarded video,
and native ads.
And it's been around for almost four years now.
That's true.
I was just a poor boy back in Ohio
when version 1.0 was launched.
So it's pretty battle tested at this point.
Another nice thing is that the whole deal is open source
under the Apache license.
So if you're curious about the codebase,
you can actually clone it.
And if you want to try tweaking how it works,
you can fork the repo and hack away.
And speaking of hacking, let's get into the editor
and take a look at how to import the Google plug-in into a Unity
project.
Here are the steps we're going to cover today.
If you've done rewarded with our native iOS or Android SDKs,
these are going to look really familiar.
The Unity plug-in sticks pretty well
toward native interfaces, which helps keep things simple.
All right, let's get right to it.
Time to get into the Unity editor and import the plug-in.
So I've got a simple app here.
It's just got two buttons, one for requesting
a video and one for showing it.
So that was my scene.
And I'm going to go up to Import Plug-in in the Assets menu,
and I'm going to go find the package that I downloaded
from GitHub.
You can get this right on our GitHub repo.
And there's all the code in it.
I'm going to import everything.
So I'll just go straight down to Import and hit the button.
And there we go.
I am now imported.
Now, I can come in here, and this
is a script editor for the scene you were just looking at.
And I can start referencing some of the stuff that's
in that name space.
So Google mobile ads, I can do it using up
here and googlemobileads.api.
There we go-- plug-in's imported.
So that's how to get the plug-in into your project.
Now let's put it to work, loading and displaying
an AdMob rewarded video ad.
So here are the five steps again.
And we're on getting the singleton instance.
So rewarded video ads is backed by a singleton object.
And within the script for my app I'm
going to go and make a private member right
here for reward-based video ad.
And in the Start method, I'm going
to grab an instance of the singleton
and store it there for later use.
There we go.
So that's two down, three to go.
Next step is loading an ad.
And so I'd like to wire up that first button, Request Rewarded
Video, to a function that will load an ad for me.
So here in the onGUI method, you can
see I've just got a simple GUI here.
It's just the two buttons.
And I've got a couple of event handlers down at the bottom.
And there's the one for request rewarded video.
So I'm just going to drop in a function call here.
And I'll call it load reward-based ad.
And I need to define that at the bottom.
So let me put that in here.
So it's just a private void member
that I can dump some code into.
There we go.
So I'm going to paste some code in here.
It's going to look like a lot, but we're
going to talk about it.
So AdMob, as you know, uses separate ad units
for iOS and Android.
But Unity can compile for both.
So what I've done here is use compiler directives
to make sure that I'm using the right ad
unit for each platform.
So if I'm building for Android, my string
ends up being the Android ad unit.
And if I'm building for iPhone, my string
ends up being the iOS ad unit.
There we go.
Now I can just drop in a load add call
on my reward-based video ad object.
And I need an ad request, which I
can build using a builder interface that
should look very familiar to anybody using Android.
It's the same kind of fluid style.
And all the same information you can
put into an ad request for the other platforms,
you can use here.
We have methods for it.
Cool.
So that's one, two, three.
Now let's try showing an ad.
So I have that second button, and I
want to wire that up to show.
So I'm going to pop down here, and I'm
going to add a new function call.
And I'll call it show reward-based ad.
And I'll define it right down here.
So another private void member--
show reward-based ad.
There we go.
So the first step here is to see if I definitely
have an ad loaded when that button gets pressed.
I can do that by calling is loaded right there.
And so if I've got an ad at the ready, I can call show,
and up pops my video ad.
If I don't, I'm just going to log a statement here
to say, hey, don't have an ad yet.
There we go.
Cool.
So four steps down.
One step left-- using events to get the reward.
So one of the nice parts about the Unity plug-in
is that it's open source.
So you can actually go in and look at what's going on.
There's the reward-based video ad class.
And I'm looking for the events in here.
There they are.
So these are the public events exposed by this class
that you can use to know what's going on
with your rewarded video ads.
So I'm going to grab those events.
And I want to make handlers for them.
So I'm just going to cut and paste these just as a shorthand
so I know which events I'm making handlers for.
There we go.
I'm just going to paste these in at the bottom.
And what I'm going to do here is create handlers
for each of these.
And I'm just going to paste this in because it's a lot of code.
There we go.
So where I used to have the events themselves
just as a shorthand to know which ones they are,
I just dropped it handlers for each one.
So I've got a handler for onAdLoaded,
onAdFailedToLoad, all the way down to onAdRewarded
and leaving application.
So this is how, like, you'd use a delegate or an ad listener
on Android and iOS--
this is how you get these events on Unity.
Cool.
So I can come in here, and I could put a reload in here
onAdFailedToLoad.
In onAdOpening, for example, I might
want to pause the action if I've got a game engine going,
because I know I've got an ad that's being displayed.
And any others, all the normal things you'd usually do,
like onAdStarted, you might want to mute your audio.
In onAdClosed, you might want to kick your game engine--
crank the party back up, get it going again.
And this is the one I'm particularly
interested for rewarded.
This is onAdRewarded, which gets called
when the user has completely viewed an ad
and deserves a reward.
So let's put some code in here just to log the reward.
You do get a reward object as the arguments,
and it has an amount and a type, just like it
does on iOS and Android.
So you get both a numerical value
for the amount of the word and a string
that represents the type.
And those are the same values that you
put into the adapps.admob.com front end.
There we go.
So I've got these methods ready.
Now it's time to get them wired in.
So let's go back up into the start method, where
I'm grabbing my instance.
I'm going to drop a block of code in here.
It's going to look like a lot, but we'll talk about it.
There we go.
So all I'm doing here is adding my methods as listeners
for these events one by one the C# way,
using the plus equals operator.
And so those are the five steps.
We've covered all of them at this point.
So I can run the app, and let's see what the log says.
Here we go.
So I'm running my Unity app.
There goes my app request.
And it finished loading.
And there's the reward in the log, one reward, which is
the default for this ad unit.
All right, so that's a working Unity implementation
for AdMob rewarded video ads.
That's going to do it for today.
But I've got some links for you on the way out.
First up is our GitHub repo for Unity, which has
the plug-in plus some samples.
I've got a link to our Get Started Guide as well.
If you're just setting out with Unity and AdMob,
these are great places to start.
We've also got guides for individual formats.
Here's Rewarded Video and Native Ads Express,
but you can find guides for banners and interstitials
too right next to those on our developer site.
And as always, you're welcome to bring your technical questions
to our support forum.
And if you've got an idea for something
you'd like us to cover in this series or a question
about what you've just seen, leave a comment below,
and Gary and I will see you next time.