123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using GoogleMobileAds.Api;
-
-
- public class AdManager : MonoBehaviour
- {
- private static AdManager _instance;
- public static AdManager Instance
- {
- get
- {
- if (_instance == null)
- {
- GameObject adManager = new GameObject("AdManager");
- _instance = adManager.AddComponent<AdManager>();
- DontDestroyOnLoad(adManager);
- }
- return _instance;
- }
- }
-
- private InterstitialAd _interstitialAd;
- #if UNITY_ANDROID
- private string _adUnitId = "ca-app-pub-3570555734266765/3828409575";
- #elif UNITY_IPHONE
- private string _adUnitId = "ca-app-pub-3570555734266765/4523825408";
- #else
- private string _adUnitId = "unused";
- #endif
- void Awake()
- {
- if (_instance == null)
- {
- _instance = this;
- DontDestroyOnLoad(gameObject);
- }
- else if (_instance != this)
- {
- Destroy(gameObject);
- return;
- }
-
- MobileAds.Initialize(initStatus => { });
- LoadInterstitialAd();
- }
-
- // Update is called once per frame
- public void LoadInterstitialAd()
- {
- if (_interstitialAd != null)
- {
- _interstitialAd.Destroy();
- _interstitialAd = null;
- }
- Debug.Log("Loading the interstitial ad.");
- var adRequest = new AdRequest();
- InterstitialAd.Load(_adUnitId, adRequest, (InterstitialAd ad, LoadAdError error) => {
- if (error != null || ad == null)
- {
- Debug.LogError("Interstitial ad failed to load an ad with error : " + error);
- return;
- }
- Debug.Log("Interstitial ad loaded with response : " + ad.GetResponseInfo());
- _interstitialAd = ad;
- RegisterEventHandlers(_interstitialAd);
- });
- }
-
- private void RegisterEventHandlers(InterstitialAd interstitialAd)
- {
- interstitialAd.OnAdPaid += (AdValue adValue) => {
- Debug.Log(string.Format("Interstitial ad paid {0} {1}.", adValue.Value, adValue.CurrencyCode));
- };
- interstitialAd.OnAdImpressionRecorded += () => {
- Debug.Log("Interstitial ad recorded an impression.");
- };
- interstitialAd.OnAdClicked += () => {
- Debug.Log("Interstitial ad was clicked.");
- };
- interstitialAd.OnAdFullScreenContentOpened += () => {
- Debug.Log("Interstitial ad full screen content opened.");
- };
- interstitialAd.OnAdFullScreenContentClosed += () => {
- Debug.Log("Interstitial ad full screen content closed.");
- LoadInterstitialAd();
- Time.timeScale = 1f;
- };
- interstitialAd.OnAdFullScreenContentFailed += (AdError error) => {
- Debug.LogError("Interstitial ad failed to open full screen content with error : " + error);
- LoadInterstitialAd();
- };
- }
-
- public void ShowInterstitialAd()
- {
- if (_interstitialAd != null && _interstitialAd.CanShowAd())
- {
- Debug.Log("Showing interstitial ad.");
- _interstitialAd.Show();
- }
- else
- {
- Debug.LogError("Interstitial ad is not ready yet.");
- }
- }
- }
|