Preload multiple interstitial ad units (Unity/Android)

1,344 views
Skip to first unread message

Puneet

unread,
Jan 26, 2015, 1:59:03 AM1/26/15
to google-adm...@googlegroups.com
Hello,

I am trying to integrate AdMob in my upcoming game made in Unity.

Currently, I have two Ad Units, i.e. Main Menu and Game Over, and both are configured to show interstitial ads.

1) Am I allowed/Is it okay to cache two interstitials for for different ad units? They are for different locations. I just want to make sure that its not a policy violation.
2) If 1 is yes, is it possible to do so with the Unity plugin?

Cheers,
Puneet

Eric Leichtenschlag (Mobile Ads SDK Team)

unread,
Jan 27, 2015, 8:04:54 PM1/27/15
to google-adm...@googlegroups.com
Hi Punset,

1) Caching interstitials is fine, since an impression won't be recorded until you show it. For banners, the expectation is that you'll show them immediately.
2) To do this, you just simply create two different instances of Interstitial and load them both with ad requests. An example of how to do this once is shown here. You just do that twice, and assign the second interstitial to a different variable.

Cheers,
Eric

PAHeartBeat

unread,
Jan 28, 2015, 5:22:15 AM1/28/15
to google-adm...@googlegroups.com
Hi,

We have done it but we are facing issue, at time of build game in XCode, it show some error, I will report it separte, bellow I posted code but It yet not tested due to xcode error,



using System;
 
using System.Collections.Generic;
 
using UnityEngine;
 
using GoogleMobileAds;
 
using GoogleMobileAds.Api;
 
 
// Example script showing how to invoke the Google Mobile Ads Unity plugin.
 
// Orignal code writen by Google Mobile Ad SDK Team,
 
// Modified By: Ankur Ranpariya aka PAHeartBeat on 28th Jan 2015
 
 
//Here I added few test adUnitIds for iOS Test
 
// ca-app-pub-6569756320106809/4839131600    Banner Ad iOS
 
//
 
// ca-app-pub-6569756320106809/7792598009    Interstitial Ad - Text
 
// ca-app-pub-6569756320106809/9269331204    Interstitial Ad - Image
 
// ca-app-pub-6569756320106809/1746064404    Interstitial Ad - Video
 
 
public class GoogleMobileAdsScript : MonoBehaviour {
     
private Dictionary<string,BannerView> bannerViews = new Dictionary<string, BannerView>();
     
private Dictionary<string,InterstitialAd> interstitialAdViews = new Dictionary<string, InterstitialAd>();
 
     
private BannerView banner;
     
private InterstitialAd interstitial;
     
void Awake() {
         
DontDestroyOnLoad(this.gameObject);
         name
= "Google Ad Manager";
     
}
     
void OnDestroy() {
         
foreach(KeyValuePair<string,BannerView> kvp in bannerViews) {
             
if(kvp.Value != null) {
                 kvp
.Value.Destroy();
             
}
         
}
         
foreach(KeyValuePair<string,InterstitialAd> kvp in interstitialAdViews) {
             
if(kvp.Value != null) {
                 kvp
.Value.Destroy();
             
}
         
}
     
     
}
 
     
void OnGUI() {
         
// Puts some basic buttons onto the screen.
         GUI
.skin.button.fontSize = (int)(0.04f * Screen.height);
 
         
Rect requestBannerRect = new Rect(0.1f * Screen.width, 0.05f * Screen.height,
                                     
0.8f * Screen.width, 0.075f * Screen.height);
         
if(GUI.Button(requestBannerRect, "Request Banner")) {
             
RequestBanner("ca-app-pub-6569756320106809/4839131600");
         
}
 
         
Rect showBannerRect = new Rect(0.1f * Screen.width, 0.15f * Screen.height,
                                   
0.8f * Screen.width, 0.075f * Screen.height);
         
if(GUI.Button(showBannerRect, "Show Banner")) {
             banner
.Show();
         
}
 
         
Rect hideBannerRect = new Rect(0.1f * Screen.width, 0.25f * Screen.height,
                                   
0.8f * Screen.width, 0.075f * Screen.height);
         
if(GUI.Button(hideBannerRect, "Hide Banner")) {
             banner
.Hide();
         
}
 
 
 
         
Rect requestInterstitialRect = new Rect(0.1f * Screen.width, 0.40f * Screen.height,
                                           
0.8f * Screen.width, 0.075f * Screen.height);
         
if(GUI.Button(requestInterstitialRect, "Request Interstitial 1 - Text")) {
             
RequestInterstitial("ca-app-pub-6569756320106809/7792598009");
         
}
 
         
Rect showInterstitialRect = new Rect(0.1f * Screen.width, 0.49f * Screen.height,
                                         
0.8f * Screen.width, 0.075f * Screen.height);
         
if(GUI.Button(showInterstitialRect, "Show Interstitial 1 - Text")) {
             
ShowInterstitial("ca-app-pub-6569756320106809/7792598009");
         
}
 
         
Rect requestInterstitialRect1 = new Rect(0.1f * Screen.width, 0.58f * Screen.height,
                                             
0.8f * Screen.width, 0.075f * Screen.height);
         
if(GUI.Button(requestInterstitialRect1, "Request Interstitial 2 - Image")) {
             
RequestInterstitial("ca-app-pub-6569756320106809/9269331204");
         
}
 
         
Rect showInterstitialRect1 = new Rect(0.1f * Screen.width, 0.67f * Screen.height,
                                         
0.8f * Screen.width, 0.075f * Screen.height);
         
if(GUI.Button(showInterstitialRect1, "Show Interstitial 2 - Image")) {
             
ShowInterstitial("ca-app-pub-6569756320106809/9269331204");
         
}
 
         
Rect requestInterstitialRect2 = new Rect(0.1f * Screen.width, 0.76f * Screen.height,
                                             
0.8f * Screen.width, 0.075f * Screen.height);
         
if(GUI.Button(requestInterstitialRect2, "Request Interstitial 2 - Video")) {
             
RequestInterstitial("ca-app-pub-6569756320106809/1746064404");
         
}
 
         
Rect showInterstitialRect2 = new Rect(0.1f * Screen.width, 0.85f * Screen.height,
                                         
0.8f * Screen.width, 0.075f * Screen.height);
         
if(GUI.Button(showInterstitialRect2, "Show Interstitial 2 - Video")) {
             
ShowInterstitial("ca-app-pub-6569756320106809/1746064404");
         
}
     
}
 
 
     
public void RequestBanner(string adUnitId) {
         
// Create a 320x50 banner at the top of the screen.
         
if(bannerViews.ContainsKey(adUnitId)) {
             banner
= bannerViews[adUnitId];
         
} else {
             banner
= new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);
 
             
// Register for ad events.
             banner
.AdLoaded += HandleAdLoaded;
             banner
.AdFailedToLoad += HandleAdFailedToLoad;
             banner
.AdOpened += HandleAdOpened;
             banner
.AdClosing += HandleAdClosing;
             banner
.AdClosed += HandleAdClosed;
             banner
.AdLeftApplication += HandleAdLeftApplication;
 
             bannerViews
.Add(adUnitId, banner);
         
}
         
// Load a banner ad.
         banner
.LoadAd(createAdRequest());
     
}
     
public void RequestInterstitial(string adUnitId) {
         
// Create an interstitial.
 
         
if(bannerViews.ContainsKey(adUnitId)) {
             interstitial
= interstitialAdViews[adUnitId];
         
} else {
             interstitial
= new InterstitialAd(adUnitId);
 
             
// Register for ad events.
             interstitial
.AdLoaded += HandleInterstitialLoaded;
             interstitial
.AdFailedToLoad += HandleInterstitialFailedToLoad;
             interstitial
.AdOpened += HandleInterstitialOpened;
             interstitial
.AdClosing += HandleInterstitialClosing;
             interstitial
.AdClosed += HandleInterstitialClosed;
             interstitial
.AdLeftApplication += HandleInterstitialLeftApplication;
 
             interstitialAdViews
.Add(adUnitId, interstitial);
         
}
         
// Load an interstitial ad.
         interstitial
.LoadAd(createAdRequest());
     
}
 
     
// Returns an ad request with custom ad targeting.
     
AdRequest ar;
     
private AdRequest createAdRequest() {
         
//.AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
         
//.AddExtra("color_bg", "9B30FF")
         
if(ar == null) {
             ar
= new AdRequest.Builder()
             
.AddTestDevice(AdRequest.TestDeviceSimulator)
             
.AddKeyword("game")
             
.SetGender(Gender.Male)
             
.SetBirthday(new DateTime(1985, 1, 1))
             
.TagForChildDirectedTreatment(false)
             
.Build();
         
}
         
return ar;
     
}
     
private void ShowInterstitial(string adUnitId) {
         
if(interstitialAdViews.ContainsKey(adUnitId)) {
             interstitial
= interstitialAdViews[adUnitId];
             
if(interstitial.IsLoaded()) {
                 interstitial
.Show();
                 
//interstitial.LoadAd(createAdRequest());
             
} else {
                 
Debug.Log("Interstitial is not ready yet.");
             
}
         
} else {
             
Debug.Log("Interstitial is not requested yet.");
             
RequestInterstitial(adUnitId);
         
}
     
}
 
     
#region Banner callback handlers
 
     
public void HandleAdLoaded(object sender, EventArgs args) {
         
Debug.Log("HandleAdLoaded event received. " + args.ToString());
     
}
 
     
public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args) {
         
Debug.Log("HandleFailedToReceiveAd event received with message: " + args.Message);
     
}
 
     
public void HandleAdOpened(object sender, EventArgs args) {
         
Debug.Log("HandleAdOpened event received " + args.ToString());
     
}
 
     
void HandleAdClosing(object sender, EventArgs args) {
         
Debug.Log("HandleAdClosing event received " + args.ToString());
     
}
 
     
public void HandleAdClosed(object sender, EventArgs args) {
         
Debug.Log("HandleAdClosed event received " + args.ToString());
     
}
 
     
public void HandleAdLeftApplication(object sender, EventArgs args) {
         
Debug.Log("HandleAdLeftApplication event received " + args.ToString());
     
}
 
     
#endregion
 
     
#region Interstitial callback handlers
 
     
public void HandleInterstitialLoaded(object sender, EventArgs args) {
         
Debug.Log("HandleInterstitialLoaded event received. " + args.ToString());
     
}
 
     
public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args) {
         
Debug.Log("HandleInterstitialFailedToLoad event received with message: " + args.Message);
     
}
 
     
public void HandleInterstitialOpened(object sender, EventArgs args) {
         
Debug.Log("HandleInterstitialOpened event received " + args.ToString());
     
}
 
     
void HandleInterstitialClosing(object sender, EventArgs args) {
         
Debug.Log("HandleInterstitialClosing event received " + args.ToString());
     
}
 
     
public void HandleInterstitialClosed(object sender, EventArgs args) {
         
Debug.Log("HandleInterstitialClosed event received " + args.ToString());
     
}
 
     
public void HandleInterstitialLeftApplication(object sender, EventArgs args) {
         
Debug.Log("HandleInterstitialLeftApplication event received " + args.ToString());
     
}
 
     
#endregion
 
}



have a great test ahaed,

PAHeartBeat

unread,
Jan 28, 2015, 8:29:58 AM1/28/15
to google-adm...@googlegroups.com
HI Punnet,

I have made a small script which can cache multiple interstitial ads, last script has some issue, I will push new scripts and project soon over gitHub
...
Reply all
Reply to author
Forward
0 new messages