using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;
using System;
public class TestAD : MonoBehaviour
{
InterstitialAd interstitial;
// Use this for initialization
void Start()
{
RequestInterstitial();
}
// Update is called once per frame
void Update()
{
}
public void Show()
{
if (interstitial.IsLoaded())
{
Debug.Log("Show AD");
}
else
{
Debug.Log("AD not ready");
//Do something here
}
}
public void RequestInterstitial()
{
if (interstitial != null && interstitial.IsLoaded())
{
return;
}
#if UNITY_ANDROID
string UnitID = "my Android UnitID";
#elif UNITY_IOS
string UnitID = "my iOS UnitID";
#endif
if (interstitial != null)
{
Debug.Log("interstitial not null");
DestroyInterstitial();
}
else
{
Debug.Log("interstitial is null");
}
interstitial = new InterstitialAd(UnitID);
interstitial.OnAdLoaded += HandleInterstitialLoaded;
interstitial.OnAdFailedToLoad += HandleInterstitialFailedToLoad;
interstitial.OnAdOpening += HandleInterstitialOpened;
interstitial.OnAdClosed += HandleInterstitialClosed;
interstitial.OnAdLeavingApplication += HandleInterstitialLeftApplication;
AdRequest request = new AdRequest.Builder().Build();
interstitial.LoadAd(request);
}
void DestroyInterstitial()
{
interstitial.Destroy();
interstitial = null;
}
public void HandleInterstitialLoaded(object sender, EventArgs args)
{
Debug.Log("HandleInterstitialLoaded");
}
public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
Debug.Log("HandleInterstitialFailedToLoad : " + args.Message);
}
public void HandleInterstitialOpened(object sender, EventArgs args)
{
Debug.Log("HandleInterstitialOpened");
}
public void HandleInterstitialClosed(object sender, EventArgs args)
{
Debug.Log("HandleInterstitialClosed");
DestroyInterstitial();
//Do something here
RequestInterstitial();
}
public void HandleInterstitialLeftApplication(object sender, EventArgs args)
{
Debug.Log("HandleInterstitialLeftApplication");
}
}