Pro_Unit

InterstitialAdUnit

May 13th, 2021
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. using System;
  2. using GoogleMobileAds.Api;
  3. using UniRx;
  4. using UnityEngine;
  5.  
  6. [CreateAssetMenu(menuName = "Mobile Ads/Interstitial", fileName = "InterstitialAdUnit", order = 0)]
  7. public class InterstitialAdUnit : GoogleAdUnit
  8. {
  9.     private InterstitialAd _interstitial;
  10.     public Subject<Unit> OnLoaded { get; } = new Subject<Unit>();
  11.     public Subject<Unit> OnShowed { get; } = new Subject<Unit>();
  12.     public Subject<Unit> OnClosed { get; } = new Subject<Unit>();
  13.  
  14.     public override void Load()
  15.     {
  16.         // Initialize an InterstitialAd.
  17.         _interstitial = new InterstitialAd(AdUnitID);
  18.         _interstitial.OnAdLoaded += InterstitialOnOnAdLoaded;
  19.         _interstitial.OnAdOpening += InterstitialOnOnAdOpening;
  20.         _interstitial.OnAdClosed += InterstitialOnOnAdClosed;
  21.         // Create an empty ad request.
  22.         var request = new AdRequest.Builder()
  23.                 .AddExtra("npa", GoogleAds.NonPersonalizedAds.ToString())
  24.                 .Build();
  25.         // Load the interstitial with the request.
  26.         _interstitial.LoadAd(request);
  27. #if UNITY_EDITOR
  28.         OnLoaded.OnNext(Unit.Default);
  29. #endif
  30.     }
  31.  
  32.     private void InterstitialOnOnAdClosed(object sender, EventArgs e)
  33.     {
  34.         if (sender is InterstitialAd interstitialAd)
  35.             interstitialAd.OnAdOpening -= InterstitialOnOnAdOpening;
  36.  
  37.         GoogleAdsHandler.MainThread.Post(_ => OnClosed.OnNext(Unit.Default), null);
  38.     }
  39.  
  40.     private void InterstitialOnOnAdOpening(object sender, EventArgs e)
  41.     {
  42.         if (sender is InterstitialAd interstitialAd)
  43.             interstitialAd.OnAdOpening -= InterstitialOnOnAdOpening;
  44.  
  45.         GoogleAdsHandler.MainThread.Post(_ => OnShowed.OnNext(Unit.Default), null);
  46.     }
  47.  
  48.     private void InterstitialOnOnAdLoaded(object sender, EventArgs e)
  49.     {
  50.         if (sender is InterstitialAd interstitialAd)
  51.             interstitialAd.OnAdLoaded -= InterstitialOnOnAdLoaded;
  52.  
  53.         Debug.Log("GoogleAdsHandler.MainThread = " + GoogleAdsHandler.MainThread);
  54.  
  55.         GoogleAdsHandler.MainThread.Post(_ => OnLoaded.OnNext(Unit.Default), null);
  56.     }
  57.  
  58.     public override void Show()
  59.     {
  60.         _interstitial.Show();
  61. #if UNITY_EDITOR
  62.         OnShowed.OnNext(Unit.Default);
  63. #endif
  64.     }
  65.  
  66.     public override void ShowOnLoaded(Action<bool> onLoadedStatus)
  67.     {
  68.         if (_interstitial != null && _interstitial.IsLoaded())
  69.         {
  70.             onLoadedStatus?.Invoke(true);
  71.             Show();
  72.         }
  73.         else
  74.         {
  75.             onLoadedStatus?.Invoke(false);
  76.             OnLoaded.Take(1).Subscribe(_ =>
  77.             {
  78.                 Show();
  79.                 onLoadedStatus?.Invoke(true);
  80.             });
  81.             Load();
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment