//c#
void HandleAdClosing(object sender, EventArgs args)
{
Cam.backgroundColor = Color.red;
Debug.Log("HandleAdClosing event received");
}<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="23" />
//c#
using System;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;
// Example script showing how to invoke the Google Mobile Ads Unity plugin.
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
public Camera Cam;
private BannerView bannerView;
private InterstitialAd interstitial;
private float deltaTime = 0.0f;
private static string outputMessage = "";
public static string OutputMessage
{
set { outputMessage = value; }
}
void Update()
{
// Calculate simple moving average for time to render screen. 0.1 factor used as smoothing
// value.
deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
}
void OnGUI()
{
GUIStyle style = new GUIStyle();
Rect rect = new Rect(0, 0, Screen.width, Screen.height);
style.alignment = TextAnchor.LowerRight;
style.fontSize = (int)(Screen.height * 0.06);
style.normal.textColor = new Color(0.0f, 0.0f, 0.5f, 1.0f);
float fps = 1.0f / deltaTime;
string text = string.Format("{0:0.} fps", fps);
GUI.Label(rect, text, style);
// Puts some basic buttons onto the screen.
GUI.skin.button.fontSize = (int)(0.03f * Screen.height);
Rect requestBannerRect = new Rect(0.1f * Screen.width, 0.05f * Screen.height,
0.8f * Screen.width, 0.1f * Screen.height);
if (GUI.Button(requestBannerRect, "Request Banner"))
{
RequestBanner();
}
Rect showBannerRect = new Rect(0.1f * Screen.width, 0.175f * Screen.height,
0.8f * Screen.width, 0.1f * Screen.height);
if (GUI.Button(showBannerRect, "Show Banner"))
{
bannerView.Show();
}
Rect destroyBannerRect = new Rect(0.1f * Screen.width, 0.3f * Screen.height,
0.8f * Screen.width, 0.1f * Screen.height);
if (GUI.Button(destroyBannerRect, "Destroy Banner"))
{
bannerView.Destroy();
}
Rect requestInterstitialRect = new Rect(0.1f * Screen.width, 0.425f * Screen.height,
0.8f * Screen.width, 0.1f * Screen.height);
if (GUI.Button(requestInterstitialRect, "Request Interstitial"))
{
RequestInterstitial();
}
Rect showInterstitialRect = new Rect(0.1f * Screen.width, 0.55f * Screen.height,
0.8f * Screen.width, 0.1f * Screen.height);
if (GUI.Button(showInterstitialRect, "Show Interstitial"))
{
ShowInterstitial();
}
Rect textOutputRect = new Rect(0.1f * Screen.width, 0.925f * Screen.height,
0.8f * Screen.width, 0.05f * Screen.height);
GUI.Label(textOutputRect, outputMessage);
}
private void RequestBanner()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
#else
string adUnitId = "unexpected_platform";
#endif
// Create a 320x50 banner at the top of the screen.
bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);
// Register for ad events.
bannerView.OnAdLoaded += HandleAdLoaded;
bannerView.OnAdFailedToLoad += HandleAdFailedToLoad;
bannerView.OnAdLoaded += HandleAdOpened;
bannerView.OnAdClosed += HandleAdClosed;
bannerView.OnAdLeavingApplication += HandleAdLeftApplication;
// Load a banner ad.
bannerView.LoadAd(createAdRequest());
}
private void RequestInterstitial()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
#else
string adUnitId = "unexpected_platform";
#endif
// Create an interstitial.
interstitial = new InterstitialAd(adUnitId);
// Register for ad events.
interstitial.OnAdLoaded += HandleInterstitialLoaded;
interstitial.OnAdFailedToLoad += HandleInterstitialFailedToLoad;
interstitial.OnAdOpening += HandleInterstitialOpened;
interstitial.OnAdClosed += HandleInterstitialClosed;
interstitial.OnAdLeavingApplication += HandleInterstitialLeftApplication;
// Load an interstitial ad.
interstitial.LoadAd(createAdRequest());
}
// Returns an ad request with custom ad targeting.
private AdRequest createAdRequest()
{
return new AdRequest.Builder()
.AddTestDevice(AdRequest.TestDeviceSimulator)
.AddTestDevice("16564E08A2534032")
.AddKeyword("game")
.SetGender(Gender.Male)
.SetBirthday(new DateTime(1985, 1, 1))
.TagForChildDirectedTreatment(false)
.AddExtra("color_bg", "9B30FF")
.Build();
}
private void ShowInterstitial()
{
if (interstitial.IsLoaded())
{
interstitial.Show();
}
else
{
Debug.Log("Interstitial is not ready yet.");
}
}
#region Banner callback handlers
public void HandleAdLoaded(object sender, EventArgs args)
{
Cam.backgroundColor = Color.red;
Debug.Log("HandleAdLoaded event received.");
}
public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
Cam.backgroundColor = Color.red;
Debug.Log("HandleFailedToReceiveAd event received with message: " + args.Message);
}
public void HandleAdOpened(object sender, EventArgs args)
{
Cam.backgroundColor = Color.red;
Debug.Log("HandleAdOpened event received");
}
void HandleAdClosing(object sender, EventArgs args)
{
Cam.backgroundColor = Color.red;
Debug.Log("HandleAdClosing event received");
}
public void HandleAdClosed(object sender, EventArgs args)
{
Cam.backgroundColor = Color.red;
Debug.Log("HandleAdClosed event received");
}
public void HandleAdLeftApplication(object sender, EventArgs args)
{
Cam.backgroundColor = Color.red;
Debug.Log("HandleAdLeftApplication event received");
}
#endregion
#region Interstitial callback handlers
public void HandleInterstitialLoaded(object sender, EventArgs args)
{
Cam.backgroundColor = Color.red;
Debug.Log("HandleInterstitialLoaded event received.");
}
public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
Cam.backgroundColor = Color.red;
Debug.Log("HandleInterstitialFailedToLoad event received with message: " + args.Message);
}
public void HandleInterstitialOpened(object sender, EventArgs args)
{
Cam.backgroundColor = Color.red;
Debug.Log("HandleInterstitialOpened event received");
}
void HandleInterstitialClosing(object sender, EventArgs args)
{
Cam.backgroundColor = Color.red;
Debug.Log("HandleInterstitialClosing event received");
}
public void HandleInterstitialClosed(object sender, EventArgs args)
{
Cam.backgroundColor = Color.red;
Debug.Log("HandleInterstitialClosed event received");
}
public void HandleInterstitialLeftApplication(object sender, EventArgs args)
{
Cam.backgroundColor = Color.red;
Debug.Log("HandleInterstitialLeftApplication event received");
}
#endregion
}
// Create an interstitial.
interstitial = new InterstitialAd(adUnitId);
// Register for ad events.
interstitial.OnAdLoaded += HandleInterstitialLoaded;
interstitial.OnAdFailedToLoad += HandleInterstitialFailedToLoad;
interstitial.OnAdOpening += HandleInterstitialOpened;
interstitial.OnAdClosed += HandleInterstitialClosed;
interstitial.OnAdLeavingApplication += HandleInterstitialLeftApplication;
// Load an interstitial ad. private void RequestBanner() { #if UNITY_EDITOR string adUnitId = "unused"; #elif UNITY_ANDROID string adUnitId = "ca-app-pub-2456842918997107/5030646679"; #elif UNITY_IPHONE string adUnitId = "ca-app-pub-2456842918997107/5030646679"; #else string adUnitId = "unexpected_platform"; #endif
// Create a 320x50 banner at the top of the screen. bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top); // Register for ad events.
bannerView.OnAdLoaded += HandleAdLoaded; bannerView.OnAdFailedToLoad += HandleAdFailedToLoad; bannerView.OnAdOpening += HandleAdOpening; //bannerView.OnAdOpened += ... ; //Causes error: `GoogleMobileAds.Api.InterstitialAd' does not contain a definition for `OnAdOpened' and no extension method `OnAdOpened' of type `GoogleMobileAds.Api.InterstitialAd' could be found bannerView.OnAdClosed += HandleAdClosed; bannerView.OnAdLeavingApplication += HandleAdLeftApplication; // Load a banner bannerView.LoadAd(createAdRequest()); }
private void RequestInterstitial() { #if UNITY_EDITOR string adUnitId = "unused"; #elif UNITY_ANDROID string adUnitId = "ca-app-pub-2456842918997107/3722037079"; #elif UNITY_IPHONE string adUnitId = "ca-app-pub-2456842918997107/3722037079"; #else string adUnitId = "unexpected_platform"; #endif
// Create an interstitial. interstitial = new InterstitialAd(adUnitId); // Register for ad events. interstitial.OnAdLoaded += HandleInterstitialLoaded; interstitial.OnAdFailedToLoad += HandleInterstitialFailedToLoad; interstitial.OnAdOpening += HandleInterstitialOpening; //interstitial.OnAdOpened += ... ; //Causes error: `GoogleMobileAds.Api.InterstitialAd' does not contain a definition for `OnAdOpened' and no extension method `OnAdOpened' of type `GoogleMobileAds.Api.InterstitialAd' could be found interstitial.OnAdClosed += HandleInterstitialClosed; interstitial.OnAdLeavingApplication += HandleInterstitialLeftApplication; // Load an interstitial ad. interstitial.LoadAd(createAdRequest()); } #region Banner callback handlers
public void HandleAdLoaded(object sender, EventArgs args) {
Cam.backgroundColor = Color.red; Debug.Log("HandleAdLoaded event received."); }
public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args) { Cam.backgroundColor = Color.red; Debug.Log("HandleFailedToReceiveAd event received with message: " + args.Message); }
public void HandleAdOpening(object sender, EventArgs args) { Cam.backgroundColor = Color.red; Debug.Log("HandleAdOpened event received"); }
void HandleAdClosing(object sender, EventArgs args) { Cam.backgroundColor = Color.red; Debug.Log("HandleAdClosing event received"); }
public void HandleAdClosed(object sender, EventArgs args) { Cam.backgroundColor = Color.red; Debug.Log("HandleAdClosed event received"); }
public void HandleAdLeftApplication(object sender, EventArgs args) { Cam.backgroundColor = Color.red; Debug.Log("HandleAdLeftApplication event received"); }
#endregion
#region Interstitial callback handlers
public void HandleInterstitialLoaded(object sender, EventArgs args) { Cam.backgroundColor = Color.red; Debug.Log("HandleInterstitialLoaded event received."); }
public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args) { Cam.backgroundColor = Color.red; Debug.Log("HandleInterstitialFailedToLoad event received with message: " + args.Message); }
public void HandleInterstitialOpening(object sender, EventArgs args) { Cam.backgroundColor = Color.red; Debug.Log("HandleInterstitialOpening event received"); }
public void HandleInterstitialClosed(object sender, EventArgs args) { Cam.backgroundColor = Color.red; Debug.Log("HandleInterstitialClosed event received"); }
public void HandleInterstitialLeftApplication(object sender, EventArgs args) { Cam.backgroundColor = Color.red; Debug.Log("HandleInterstitialLeftApplication event received"); }
#endregion