#define UNITY_ANDROID
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using GoogleMobileAds.Api;
public class NewBehaviourScript : MonoBehaviour
{
private BannerView bannerView;
// Start is called before the first frame update
void Start()
{
// Initialize the Google Mobile Ads SDK.
// Before loading ads, have your app initialize the Google Mobile Ads SDK
// by calling MobileAds.initialize() which initializes the SDK and calls back
// a completion listener once initialization is complete (or after a 30-second
// timeout). This needs to be done only once, ideally at app launch.
MobileAds.Initialize(initStatus =>
{
Dictionary<string, AdapterStatus> map = initStatus.getAdapterStatusMap();
foreach (KeyValuePair<string, AdapterStatus> keyValuePair in map)
{
string className = keyValuePair.Key;
AdapterStatus adapterStatus = keyValuePair.Value;
switch (adapterStatus.InitializationState)
{
case AdapterState.Ready:
// The adapter initialization ready.
MonoBehaviour.print($"Adapter: {className} is {adapterStatus.Description}");
// Adapter: ExampleClass is Ready
break;
case AdapterState.NotReady:
// The adapter initialization not ready.
MonoBehaviour.print($"Adapter: {className} is {adapterStatus.Description}");
break;
default:
break;
}
}
});
this.RequestBanner();
}
private void RequestBanner()
{
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-3940256099942544/6300978111";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-3940256099942544/2934735716";
#else
string adUnitId = "unexpected_platform";
#endif
// Create a 320x50 banner at the top of the screen.
this.bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);
// Called when an ad request has successfully loaded.
this.bannerView.OnAdLoaded += this.HandleOnAdLoaded;
// Called when an ad request failed to load.
this.bannerView.OnAdFailedToLoad += this.HandleOnAdFailedToLoad;
// Called when an ad is clicked.
this.bannerView.OnAdOpening += this.HandleOnAdOpened;
// Called when the user returned from the app after an ad click.
this.bannerView.OnAdClosed += this.HandleOnAdClosed;
// Removed OnAdLeavingApplication event for all formats.
//this.bannerView.OnAdLeavingApplication += this.HandleOnAdLeavingApplication;
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the banner with the request.
this.bannerView.LoadAd(request);
}
// Update is called once per frame
void Update()
{
}
public void HandleOnAdLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdLoaded event received");
}
public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print("HandleFailedToReceiveAd event received with message: "
+ args.ToString());
}
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");
//}
private void OnDestroy()
{
this.bannerView.Destroy();
}
}