using System;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class AdManagerObject : MonoBehaviour {
public static AdManagerObject instance;
private InterstitialAd interstitial;
private RewardBasedVideoAd rewardBasedVideo;
void Awake () {
if (!instance) {
instance = this;
} else {
Destroy (this.gameObject);
}
DontDestroyOnLoad(this.gameObject) ;
// Get singleton reward based video ad reference.
rewardBasedVideo = RewardBasedVideoAd.Instance;
// RewardBasedVideoAd is a singleton, so handlers should only be registered once.
rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
RequestRewardBasedVideo();
}
void Start(){
RequestInterstitial ();
}
public void RequestInterstitial(){
interstitial = new InterstitialAd ("YOUR_INTER_ID");
interstitial.OnAdLoaded += HandleAdLoaded;
interstitial.OnAdFailedToLoad += HandleAdFailedToLoad;
interstitial.OnAdClosed += HandleAdClosed;
AdRequest request1 = new AdRequest.Builder ().Build ();
interstitial.LoadAd (request1);
}
public void RequestRewardBasedVideo()
{
if (!rewardBasedVideo.IsLoaded()){
string adUnitId = "YOUR_REWARD_ID";
AdRequest request2 = new AdRequest.Builder().Build();
rewardBasedVideo.LoadAd(request2, adUnitId);
}
}
void HandleAdLoaded (object sender, System.EventArgs e)
{
}
void HandleAdFailedToLoad (object sender, AdFailedToLoadEventArgs e)
{
}
void HandleAdClosed (object sender, System.EventArgs e)
{
interstitial.Destroy ();
interstitial.OnAdLoaded -= HandleAdLoaded;
interstitial.OnAdFailedToLoad -= HandleAdFailedToLoad;
interstitial.OnAdClosed -= HandleAdClosed;
RequestInterstitial ();
}
public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
{
}
public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
}
public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
{
}
public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
RequestRewardBasedVideo();
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
string type = args.Type;
int amount = (int) args.Amount;
}
public void ShowInterstitialAd(){
if (interstitial.IsLoaded ()) {
interstitial.Show ();
}
}
public void ShowRewardAd(){
if (rewardBasedVideo.IsLoaded()){
rewardBasedVideo.Show();
}
}
void OnDestroy(){
interstitial.Destroy ();
}
}