I am working on a game and I have to show Google ads in the game, I have integrated it as its described by google developers, its compiles well but when I call BannerView.Show(); then the game shows a message like “MyGame Activity Stopped Responding” and game restarts. I am attaching the code please check it and if you find anything wrong or find some solution please tell me.
I have latest google-play-services_lib
Latest android sdk
and latest AdMob unity plugin from google.
StartGame.cs
using UnityEngine;
using System.Collections;
public class StartGame : MonoBehaviour {
// Use this for initialization
void Start () {
ShowAds();
}
// Update is called once per frame
void Update () {
}
void ShowAds(){
GoogleMobileAdsScript.RequestBanner ();
GoogleMobileAdsScript.bannerView.Show();
Debug.Log("Showing Banner");
}
}
GoogleMobileAdsScript.cs
using System;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class GoogleMobileAdsScript : MonoBehaviour
{
static public BannerView bannerView;
static public InterstitialAd interstitial;
static GoogleMobileAdsScript ads;
void Start () {
ads = this;
}
static public void RequestBanner()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "---------------------------------------------------------";
#elif UNITY_IPHONE
string adUnitId = "---------------------------------------------------------";
#else
string adUnitId = "unexpected_platform";
#endif
// Create a 320x50 banner at the top of the screen.
bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);
// Register for ad events.
bannerView.AdLoaded += ads.HandleAdLoaded;
bannerView.AdFailedToLoad += ads.HandleAdFailedToLoad;
bannerView.AdOpened += ads.HandleAdOpened;
bannerView.AdClosing += ads.HandleAdClosing;
bannerView.AdClosed += ads.HandleAdClosed;
bannerView.AdLeftApplication += ads.HandleAdLeftApplication;
// Load a banner ad.
bannerView.LoadAd(ads.createAdRequest());
}
static public void RequestInterstitial()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "---------------------------------------------------------";
#elif UNITY_IPHONE
string adUnitId = "---------------------------------------------------------";
#else
string adUnitId = "unexpected_platform";
#endif
// Create an interstitial.
interstitial = new InterstitialAd(adUnitId);
// Register for ad events.
interstitial.AdLoaded += ads.HandleInterstitialLoaded;
interstitial.AdFailedToLoad += ads.HandleInterstitialFailedToLoad;
interstitial.AdOpened += ads.HandleInterstitialOpened;
interstitial.AdClosing += ads.HandleInterstitialClosing;
interstitial.AdClosed += ads.HandleInterstitialClosed;
interstitial.AdLeftApplication += ads.HandleInterstitialLeftApplication;
// Load an interstitial ad.
interstitial.LoadAd(ads.createAdRequest());
}
// Returns an ad request with custom ad targeting.
private AdRequest createAdRequest()
{
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();
}
static public void ShowInterstitial()
{
if (interstitial.IsLoaded())
{
interstitial.Show();
}
else
{
print("Interstitial is not ready yet.");
}
}
#region Banner callback handlers
public void HandleAdLoaded(object sender, EventArgs args)
{
print("HandleAdLoaded event received.");
}
public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
print("HandleFailedToReceiveAd event received with message: " + args.Message);
}
public void HandleAdOpened(object sender, EventArgs args)
{
print("HandleAdOpened event received");
}
void HandleAdClosing(object sender, EventArgs args)
{
print("HandleAdClosing event received");
}
public void HandleAdClosed(object sender, EventArgs args)
{
print("HandleAdClosed event received");
}
public void HandleAdLeftApplication(object sender, EventArgs args)
{
print("HandleAdLeftApplication event received");
}
#endregion
#region Interstitial callback handlers
public void HandleInterstitialLoaded(object sender, EventArgs args)
{
print("HandleInterstitialLoaded event received.");
}
public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
print("HandleInterstitialFailedToLoad event received with message: " + args.Message);
}
public void HandleInterstitialOpened(object sender, EventArgs args)
{
print("HandleInterstitialOpened event received");
}
void HandleInterstitialClosing(object sender, EventArgs args)
{
print("HandleInterstitialClosing event received");
}
public void HandleInterstitialClosed(object sender, EventArgs args)
{
RequestInterstitial();
print("HandleInterstitialClosed event received");
}
public void HandleInterstitialLeftApplication(object sender, EventArgs args)
{
print("HandleInterstitialLeftApplication event received");
}
#endregion
}
Thanks
1926119–124415–GoogleMobileAdsScript.cs (4.73 KB)
1926119–124416–StartGame.cs (368 Bytes)