Yuma 4×4

Media and Communications

Unity and AdMob Rewarded Video – Mobile Ads Garage #10

Unity and AdMob Rewarded Video – Mobile Ads Garage #10

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.

80 thoughts on “Unity and AdMob Rewarded Video – Mobile Ads Garage #10

  1. When I needed rewarded videos admob didn't hvae them… Now I will not spend time changing my game code…

  2. Hi, i created two ad units, a banner n rewarded ad, implemented both, but only banner is shown and rewarded ad is not loaded not even the test ad as see in this video. Googled what went wrong but can't find a solution. Thanks

  3. Hey, i used the basic rewarded video code, but there is always this problem:
    Project GPM data does not exist. Creating new object.

    can you please help me? it's important 🙂

  4. This was a really great and helpful video you guys should cover in dept how to set up and use the firebase realtime database in unity

  5. Hi Andrew, I have tried to implement it, but my rewarded video won't show in any case. Can you please provide me a Unity Project with rewarded video setup so that i Only Have to change the rewarded ad unit id for my unity project?

  6. All the script gives me errors. I added GoogleMobileAds and GoogleMobileAds.Api libraries. What am I missing?

  7. Hi Andrew, Thanks for your such a nice explaining video. i have implemented exact with the same way as you described, my interstitial ads are being displayed properly but unfortunately reward based video ads says "No Fill" each time. they are not displayed even a single time and even i didn't add the test device.
    Kindly give me a path to walk on so i can implement reward based video ads successfully in my project.

  8. i am trying but reward video ads aren't showing
    i did a debug in my build to display message and i receive the message in the method

    i get the message no fill

  9. Admob Rewarded Video Not Showing getting the error "Add Failed To Load", Not Using any Mediation Just Wanted Simple AdMob Rewarded Video To Popup, I Have Tried Your Sample Project And Your Admob Test Id But The Result Is The Same.

  10. Hi Andrew, Thank you for all your efforts, I have a issue with event handlers, I followed all your instructions in the video, only thing i made different is I am calling LoadRewardedAdVdeo() from Start() method, after i built the game, i test it and rewarded video is showing correctly, but when it is finishes or user closes the add, event handlers are not getting called, I tested it also by adding bool iswatched; and set in HandleRewardBasedVideoRewarded(…) {iswatched = true} and HandleRewardBasedVideoClosed(…) {iswatched = false} and in Update() method checked iswatched true or false and debug.log something based on that but, it is not doing anything. iswatched remained in initialized state and not getting true or false condition, Thank you.

  11. Even the video is interesting it missed one important part: 'let's see what the log says'. Log? How could you test it under Unity? It is the log built from the example code? Could you explain that?

  12. Help
    please, everything works, but only the first time, after restarting the
    scene, the show is launched but public void HandleRevardBasedeVideo
    Relay does not work and the reward does not come

  13. at 6:12, where you copied the eventhandlers, I followed your exact steps but my eventhandlers are giving error saying that 'EventHandler' does not exist in the current context
    And can you plz tell me from where did you copied the handlers for event handlers at 6:22

  14. you should add c# as android supported language, instead of bizzare language such as kotlin, microsoft is good guy now..

  15. Can anyone Tell me the difference between these 2 Ad APIs (Both APIs are for Unity but what is the difference?)

    1) https://github.com/googleads/googleads-mobile-unity/releases (Find in another Video)
    2) https://github.com/unity-plugins/Unity-Admob (Given In this Video)

  16. Hi Andrew, Thanks for such a nice tutorial for reward based ads. I have implemented it as you explained in the tutorial, the video ad are being displayed but i am facing problem in event handlers. No event got triggered when the ad displays. Kindly give me a solution so that i can implement reward based video ads in my project.
    Thank You 🙂

  17. My project uses Google Play Services and AdMob. Everything works fine, but when displayed full-screen ads and close it, not showing leaderboard.What could be the problem?

  18. I have read a lot of complaints about not getting admob working with unity here and on the unity forum…
    If you want to add admob to your project in a matter of minutes, check out how Enhance can help you!
    Implement & update multiple SDKs in a few simple steps with Enhance : https://goo.gl/vv56Qm

  19. I have mine working – sort of – I have a rewarded ad unit set up, which is configured to give an amount of 1. It rewards an amount of 10 instead :/

  20. hi Andrew, thank you for this awesome video, when i use admob plugin and i try to build for android i get this error: IOException: Failed to Move File / Directory from, please if you have any suggestion for resolving this issue it would be nice from you , thank you

  21. Can anyone help I connect firebase to unity and then implemented this video and attach my project id Why my ads are not showing ? 🙁
    Thanks in advance 🙂

  22. hi i implement the admobe rewarded video and use ur id for test and it is work perfectly and i have the reward 10 from the test id put when i use my app id and adunitid nothing happend

  23. Hi.Thanks for this good tutorial.but i have a little problem. My game just closes when it runs this line of code "rew = RewardBasedVideoAd.Instance;".Anyone please can help me with this??

  24. Hi Andrew
    Here is a method which could increase your ad revenue and make the playing experience more user friendly.

    As Google has it now with any ad clicked they get taken out of the game and sent to the where the link for the ad sends them. This is not good for both the player because they may want to continue the game and also they may want. to see the ad info.
    A better solution is they click he ad and it stores the info which they access immediately after they quit the game.
    For sure this will put you ahead of the competition but I am sure they will be quick to follow

  25. hi, btw do you have any tutorial to integrating for ios? because i still getting error that ive post right here https://forum.unity.com/threads/admob-implementation-apple-mach-o-linker-id-error.515398/

  26. So 2 things that weren't mentioned in the video:
    1: actually assigning the handlers;
    rewardBasedVideoAd.OnAdLoaded += HandleOnAdLoaded;
    … Etc

    And 2:
    That the handlers are pretty inconsistent, even with test ads. I just use a late update to check the status on the isLoaded

    Don't forget that if you're using your own adId, it'll be some time before it regularly has ads. It'll only send out one in every 1000 or so calls for me, but after a week it'll be normal.

  27. In my case "test rewarded video" are never shown and I get "No fill" error all the times. My app is published in alpha and enrolled to the "Designed for families" program. I changed the package name with one that is not even published and I'm able to get "test reward video". This means that reward video are not available for "Designed for families" apps, right?

  28. hello sir, If i want different banner ads in a banner view so did i need to destroy that banner view and request again for banner view in unity, or in a single banner view different ads can load (i hope you are understanding what i want to say)plzz answer.

  29. Hi, I have followed this tutorial and the video reward ads are appearing as expected, but they are loading very slow, they take too much to appear. Do you know a way to load the reward videos faster? Right now I am loading these when the user clicks on show reward video.

  30. I use a plugin leaderBoard and an admob plugin, but when I do the build, an error shows me: Unable to convert classes into dex format ? help me

  31. Interstitial Ads:
    If the back button is pressed before the ad expires, the ad closes. Is there any way to disable back button? (Not unity.. activity back button)

  32. Hi Andrew, I have copied this code verbatim into an identical project with the most recent package and Unity 2017.4.3f1 (most recent 2017.X version (May 11,2018)), but I get the following response in logcat after about 10 seconds or so when i press the Request button :
    HandleRewardBasedVideoFailedToLoad event received with message: Internal error

  33. i have integrated the banner ads in unity its working in console i use onload onlading event and print some thing on console itss working but when i use it in mobile no ads are displaying

  34. @Andrew my handlers are working fine every thing is fine in console. in cosnole (dummy load ad)( dummy rewarded based video show) but on mobile am not getting ads..

  35. so ,if we have 5 rewards ads need to add in the app, the AdMob can only let all these different rewards ads share one unit id ,Cant let them get their own different unit id?

  36. andrew i am my reward video are not showing i get a msg in the console but when i run the app in mobile ad is not showing

  37. Hi Andrew. My testing device was blocked in admob after my learning experiments with admob. Now I can't receive even test blocks with testing appID.. Why admob so paranoid?

  38. Class not found exception when create instance "this.rewardBasedVideo = RewardBasedVideoAd.Instance;" here plz help

  39. It is laughable how he takes time to fix all the unwanted spaces c# leaves. Coming from the Java world, this is annoying for me as well.

  40. Hy Bro My Thanks soo much for that kinda videos your videos such a awesome but now from start of this month my ads are not showing but the test ads are working perfectly butt my ads are not showing why can you help me about that Thanks broo

  41. Dear Sir . I have a error "GoogleMobileAds.Common.DummyClient:.ctor() (at Assets/GoogleMobileAds/Common/DummyClient.cs:28)" at rewardBasedVideo = RewardBasedVideoAd.Instance. Please help me

Leave comment

Your email address will not be published. Required fields are marked with *.