Advertisement
Void_Ptr_YT

AdsManager

Sep 19th, 2023
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using UniRx;
  4. using UnityEngine;
  5.  
  6. namespace ElectrumGames.Core.Mediation
  7. {
  8.     public class AndroidAdsManager : IAdsManager, IDisposable
  9.     {
  10.         private const string AppKey = "1b9121145"; //is default - 85460dcd | my - 1b9121145 | cbh - 18895eccd
  11.        
  12.         private readonly IronSource _agent;
  13.         private Action<bool> _cashedEvent;
  14.  
  15.         public bool InterstitialReady => _agent.isInterstitialReady();
  16.         public bool RewardedVideoReady => _agent.isRewardedVideoAvailable();
  17.        
  18.         public AndroidAdsManager()
  19.         {
  20.             IronSourceConfig.Instance.setClientSideCallbacks(true);
  21.             _agent = IronSource.Agent;
  22.            
  23.             _agent.validateIntegration();
  24.             _agent.init(AppKey);
  25.  
  26.             IronSourceRewardedVideoEvents.onAdClosedEvent += OnSuccessfulRewardedVideoWatch;
  27.             IronSourceRewardedVideoEvents.onAdShowFailedEvent += OnFailedRewardedVideoWatch;
  28.         }
  29.        
  30.         public void Dispose()
  31.         {
  32.             IronSourceRewardedVideoEvents.onAdClosedEvent -= OnSuccessfulRewardedVideoWatch;
  33.             IronSourceRewardedVideoEvents.onAdShowFailedEvent -= OnFailedRewardedVideoWatch;
  34.         }
  35.  
  36.         public void LoadAds()
  37.         {
  38.             Observable.FromCoroutine(InterstitialLoadProcess).Subscribe();
  39.             Observable.FromCoroutine(RewardedVideoLoadProcess).Subscribe();
  40.         }
  41.  
  42.         public bool TryShowInterstitial()
  43.         {
  44.             if (!InterstitialReady)
  45.             {
  46.                 Observable.FromCoroutine(InterstitialLoadProcess).Subscribe();
  47.                 return false;
  48.             }
  49.  
  50.             _agent.showInterstitial();
  51.             Observable.FromCoroutine(InterstitialLoadProcess).Subscribe();
  52.             return true;
  53.         }
  54.  
  55.         public bool TryShowRewardedVideo(Action<bool> onComplete)
  56.         {
  57.             if (!RewardedVideoReady)
  58.             {
  59.                 Observable.FromCoroutine(RewardedVideoLoadProcess).Subscribe();
  60.                 return false;
  61.             }
  62.  
  63.             _cashedEvent = onComplete;
  64.             _agent.showRewardedVideo();
  65.             Observable.FromCoroutine(RewardedVideoLoadProcess).Subscribe();
  66.             return true;
  67.         }
  68.  
  69.         private IEnumerator InterstitialLoadProcess()
  70.         {
  71.             while (!InterstitialReady)
  72.             {
  73.                 _agent.loadInterstitial();
  74.                 yield return new WaitForEndOfFrame();
  75.             }
  76.         }
  77.  
  78.         private IEnumerator RewardedVideoLoadProcess()
  79.         {
  80.             while (!RewardedVideoReady)
  81.             {
  82.                 _agent.loadRewardedVideo();
  83.                 yield return new WaitForEndOfFrame();
  84.             }
  85.         }
  86.  
  87.         private void OnSuccessfulRewardedVideoWatch(IronSourceAdInfo info)
  88.         {
  89.             _cashedEvent?.Invoke(true);
  90.         }
  91.  
  92.         private void OnFailedRewardedVideoWatch(IronSourceError error, IronSourceAdInfo info)
  93.         {
  94.             _cashedEvent?.Invoke(false);
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement