What code should I create to show Admob Ads

130 views
Skip to first unread message

tom g

unread,
Sep 21, 2017, 7:09:33 PM9/21/17
to Google Mobile Ads SDK Developers
I have downloaded the Unity plugin into my game for Android.  Now what should I do?

Does anyone have the standard code that I should use to do the rest??

Thank you

Ivan Bautista (Mobile Ads SDK Team)

unread,
Sep 22, 2017, 12:19:07 AM9/22/17
to Google Mobile Ads SDK Developers
Hi,

You can refer to this Get Started guide to kick off your development and integration with the Google Mobile Ads Unity plugin. Additionally, you may use our sample app to help you get started with integration.

Regards,
Ivan Bautista
Mobile Ads SDK Team

tom g

unread,
Sep 22, 2017, 12:44:47 AM9/22/17
to Google Mobile Ads SDK Developers
Thank you I have looked at both these for too long.  Can someone post the code without all of the Unity OnGUI components so that I can see just the advertising stuff.  And then tell me how to call the Show Ad() or Show Banner Ad().
Thank you

Ivan Bautista (Mobile Ads SDK Team)

unread,
Sep 22, 2017, 1:39:40 AM9/22/17
to Google Mobile Ads SDK Developers
Hi,

You may refer to this Get Started section for creating, loading and displaying Banner ads. 

The code snippet below will instantiate and position the BannerView.
bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);
 
While the code snippet below will load and display (if successful) the Banner Ad in the BannerView.
bannerView.LoadAd(request);

Additionally, you may refer to the RequestBanner() method of the sample app for the instantiation of the BannerView until the loading of the Banner Ad.
Message has been deleted

tom g

unread,
Sep 22, 2017, 12:16:40 PM9/22/17
to Google Mobile Ads SDK Developers
Ivan,
Please explain the bannerView.LoadAd(request); components
Do I require .AddTestDevice(AdRequest.TestDeviceSimulator)??
Do I require .AddTestDevice("0123456789ABCDEF0123456789ABCDEF")??
        - where do I get the string value for my test device?
Do I require .AddKeyword("game")?
etc.

return new AdRequest.Builder()
.AddTestDevice(AdRequest.TestDeviceSimulator)
.AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
.AddKeyword("game")
.SetGender(Gender.Male)
.SetBirthday(new DateTime(1985, 1, 1))
.TagForChildDirectedTreatment(false)
.AddExtra("color_bg", "9B30FF")
.Build();

tom g

unread,
Sep 22, 2017, 12:18:25 PM9/22/17
to Google Mobile Ads SDK Developers
or should I just use
AdRequest request = new AdRequest.Builder().Build();

tom g

unread,
Sep 22, 2017, 4:24:54 PM9/22/17
to Google Mobile Ads SDK Developers
Why is the code block
#if UNITY_EDITOR
       
string adUnitId = "unused";
#elif UNITY_ANDROID
       
string adUnitId = "ca-app-pub-3940256099942544/1033173712";
#elif UNITY_IPHONE
       
string adUnitId = "ca-app-pub-3940256099942544/4411468910";
#else
       
string adUnitId = "unexpected_platform";
#endif


repeated so many time (once for banner ads, once for interstitial ads, once for native ads and again for rewarded ads.

Can I place this code block in the Start() method.?
Do I need to have a different adUnitId for each of the Ad Types(banner, rewarded, interstitial and native)?

Thank you,

Ivan Bautista (Mobile Ads SDK Team)

unread,
Sep 25, 2017, 12:17:46 AM9/25/17
to Google Mobile Ads SDK Developers
Hi, 

I just want to note that the sample app provided is mainly for the purpose of showing the user the basic way of integrating the different features of the Mobile Ads SDK into your app. That said, below are my answers to your questions.

What is the difference between an appId and adUnitId?
As per this Help Center article, the app ID is a unique ID number assigned to your apps when they're added to AdMob. The app ID is used to identify your apps. On the other hand, the ad unit ID is a unique ID number assigned to each of your ad units when they're created in AdMob. The ad unit ID is added to your app's code and used to identify ad requests from the ad unit which also defines the type of ad format you wish to request for. 

Please explain the bannerView.LoadAd(request);
This line of code basically sends the Ad Request, as defined in the request object, and displays it to the BannerView once it returns successfully the corresponding Ad.

Do I require .AddTestDevice(AdRequest.TestDeviceSimulator)??
Do I require .AddTestDevice("0123456789ABCDEF0123456789ABCDEF"
)??
No, these are not required. You only need to add test device if you wish to request and display test ads to your actual device to test out if your code implementation is correct.
 
where do I get the string value for my test device?
 The string value can only be found in the logs on both Android and iOS when running your apps and making a live ad request only. For Android, check the logcat output for a message that looks like the one below.
I/Ads: Use AdRequest.Builder.addTestDevice("33BE2250B43518CCDA7DE426D04EE232")
to get test ads on this device.
 
Do I require .AddKeyword("game")?
No, it is not required. It is only one of the targeting options you can add to filter your Ad Requests. The line of code below will suffice to request live ads.
AdRequest request = new AdRequest.Builder().Build();
Why is the code block repeated so many time (once for banner ads, once for interstitial ads, once for native ads and again for rewarded ads.
Can I place this code block in the Start() method.? 
As mentioned earlier, the code in the sample app are mainly used as reference on how to integrate the different features of the Mobile Ads SDK into your app. Thus, you can integrate them into your app accordingly. In the sample app, the same code block was used for the different Ad Units with different Ad formats just to show how they will be implemented for each Ad format/type.

Do I need to have a different adUnitId for each of the Ad Types(banner, rewarded, interstitial and native)?
Yes. One Ad Unit Id can only correspond to 1 Ad Unit and 1 Ad format/type (Banner, Rewarded, Interstitial and Native).  

Let me know if this helps or if you have any further questions.

TomPV G

unread,
Sep 25, 2017, 5:26:48 PM9/25/17
to Google Mobile Ads SDK Developers
Ivan,
Thank you for all these answers.

1.  To get the string value of my Android test device.  Where do I find the logCat output?

2.  For a banner Ad.  What would be the typical actions to take for the various callbacks (HandleAdLoaded, HandleAdFailedToLoad, HandleAdOpened, HandleAdClosed, HandleAdLeftApplication)
I am inclined to do nothing for all of these.  In fact I can't think of anything that I should do as the game developer / publisher for any of these.  However please advise of what is typically done so that I can learn more about banner Ads and learn more about what other people may be doing

Thank you

TomPV G

unread,
Sep 25, 2017, 6:13:38 PM9/25/17
to Google Mobile Ads SDK Developers
For Android banner Ads

When I call "
this.bannerView.LoadAd(this.CreateAdRequest())
Does this just load the Ad in the background or does it also Show the Ad?

I seem to be getting the test Ads to Show even though I have not called 
this.bannerView.Show();

Why would that be happening?

Ivan Bautista (Mobile Ads SDK Team)

unread,
Sep 25, 2017, 11:54:02 PM9/25/17
to Google Mobile Ads SDK Developers
Hi,

I'm glad that the previous responses helped. Below are my responses to your recent queries.

1.  To get the string value of my Android test device.  Where do I find the logCat output?
This only applies if you are testing your Unity app in an Android device. To generate the logcat output, you may run the logcat via command-line tool or view the logs in the Android Studio's Logcat Monitor.

2.  For a banner Ad.  What would be the typical actions to take for the various callbacks (HandleAdLoaded, HandleAdFailedToLoad, HandleAdOpened, HandleAdClosed, HandleAdLeftApplication)
I am inclined to do nothing for all of these.  In fact I can't think of anything that I should do as the game developer / publisher for any of these.  However please advise of what is typically done so that I can learn more about banner Ads and learn more about what other people may be doing
 It's not required to register for all these events so just register to those that you think you need to keep track of. You may refer to this section for a brief description of each events and when they are being called so that you can take certain actions accordingly if needed on your side.

 
When I call "
this.bannerView.LoadAd(this.CreateAdRequest())
Does this just load the Ad in the background or does it also Show the Ad?
This will load the Ad and show the ad if the requested Ad was successfully returned. If it failed to load Ad due to other factors such as network connectivity or available Ad inventory, then no ad will be shown and HandleAdFailedToLoad event will be called with corresponding error message and code.

I seem to be getting the test Ads to Show even though I have not called 
this.bannerView.Show();
Why would that be happening? 
You don't need to call Show() method as LoadAd() would suffice to show banner ad. The only possible reason why your Ad request returned a test Ad is because you enabled a test device or you are using one of our sample Ad Units or the available Ad Units in the sample app.

Let me know if this helps or if you have any other technical questions about the Mobile Ads SDK.

tom g

unread,
Sep 27, 2017, 2:27:58 PM9/27/17
to Google Mobile Ads SDK Developers
Thank you Ivan.

Good to know that LoadAd() ALSO will show the Ad if it loads successfully.  (That explains why the test Ads were showing)

1.  Do I have to call 
bannerView.Destroy();
when I exit my game?

2. If the banner Ad fails to load, does anything get shown at the spot on the screen where the banner Ad
was to be shown? (ie does any kind of black background or anything like that show up?) or will my entire
game screen be shown? Just asking in case I need to do anything in the "HandleAdFailedToLoad" call back.

3. If the banner Ad fails to load, does it keep trying to load an Ad automatically?

Thank you,

tom g

unread,
Sep 27, 2017, 5:24:15 PM9/27/17
to Google Mobile Ads SDK Developers
Ivan,
Will the production Banner Ads have an "x" so that a player can close the banner Ad?

Ivan Bautista (Mobile Ads SDK Team)

unread,
Sep 28, 2017, 2:03:11 AM9/28/17
to Google Mobile Ads SDK Developers
Hi,

I'm glad that the previous questions were cleared out. We appreciate your enthusiasm to learn in detail, the different features of the Mobile Ads SDK. Moving forward, to keep a consistent flow of discussion in every thread, I suggest that you create a new thread if you have a new set of questions. That way, if other users have similar questions as yours, they can refer to the corresponding relevant thread.

As for your recent queries, please refer below for my responses.

1.  Do I have to call 
bannerView.Destroy();
when I exit my game?
It is not required but as best practice, it is good to implement it if the object is no longer used to prevent memory leaks.

2. If the banner Ad fails to load, does anything get shown at the spot on the screen where the banner Ad
was to be shown? (ie does any kind of black background or anything like that show up?) or will my entire
game screen be shown? Just asking in case I need to do anything in the "HandleAdFailedToLoad" call back.
If the Ad failed to load, nothing will be shown in the area where the Banner Ad was suppose to be displayed. It will be up to you to make dynamic adjustments or not to your layout if the Ad failed to load.

3. If the banner Ad fails to load, does it keep trying to load an Ad automatically?
You can actually set your Banner Ad Units to automatically refresh or not in the AdMob dashboard. If automatic refresh rate is enabled, then it will keep trying to load an Ad automatically based on the set refresh rate.

Will the production Banner Ads have an "x" so that a player can close the banner Ad?
There's no "x" or close button in the Banner Ad View
Reply all
Reply to author
Forward
0 new messages