,using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using GoogleMobileAds.Api;
public class AdManager : MonoBehaviour {
public static AdManager Instance;
private string appId = "MyAppID";
private BannerView bannerad;
private InterstitialAd interstitialAd;
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(this);
}
}
void Start ()
{
MobileAds.Initialize(appId);
RequestBanner();
RequestInterstisial();
Invoke("DisplayBanner", 2.5f);
}
void RequestBanner()
{
string banner_ID = "MYBANNERID";
bannerad = new BannerView(banner_ID, AdSize.SmartBanner, AdPosition.Bottom);
bannerad.LoadAd(adRequest);
}
void RequestInterstisial()
{
string interstitial_ID = "MYINTERID";
interstitialAd = new InterstitialAd(interstitial_ID);
interstitialAd.LoadAd(adRequest);
}
public void DisplayBanner()
{
bannerad.Show();
}
public void Display_Inter()
{
if (interstitialAd.IsLoaded())
{
interstitialAd.Show();
}
}
//Handle
public void HandleOnAdLoaded(object sender, EventArgs args)
{
DisplayBanner();
}
public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
RequestBanner();
}
public void HandleOnAdOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdOpened event received");
}
public void HandleOnAdClosed(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdClosed event received");
}
public void HandleOnAdLeavingApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdLeavingApplication event received");
}
void HandleBannerAdEvent(bool subscribe)
{
if(subscribe == true)
{
// Called when an ad request has successfully loaded.
bannerad.OnAdLoaded += HandleOnAdLoaded;
// Called when an ad request failed to load.
bannerad.OnAdFailedToLoad += HandleOnAdFailedToLoad;
// Called when an ad is clicked.
bannerad.OnAdOpening += HandleOnAdOpened;
// Called when the user returned from the app after an ad click.
bannerad.OnAdClosed += HandleOnAdClosed;
// Called when the ad click caused the user to leave the application.
bannerad.OnAdLeavingApplication += HandleOnAdLeavingApplication;
}
else
{
// Called when an ad request has successfully loaded.
bannerad.OnAdLoaded -= HandleOnAdLoaded;
// Called when an ad request failed to load.
bannerad.OnAdFailedToLoad -= HandleOnAdFailedToLoad;
// Called when an ad is clicked.
bannerad.OnAdOpening -= HandleOnAdOpened;
// Called when the user returned from the app after an ad click.
bannerad.OnAdClosed -= HandleOnAdClosed;
// Called when the ad click caused the user to leave the application.
bannerad.OnAdLeavingApplication -= HandleOnAdLeavingApplication;
}
}
void OnEnable()
{
HandleBannerAdEvent(true);
}
void OnDisable()
{
HandleBannerAdEvent(false);
}
}
↧






