198 lines
6.3 KiB
C#
198 lines
6.3 KiB
C#
|
using System;
|
|||
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
namespace RND.Mediation
|
|||
|
{
|
|||
|
public class MoPubSDK : IAdsSDK
|
|||
|
{
|
|||
|
public string BannerUnit => Settings.ADS_BANNER_UNIT;
|
|||
|
public string InterstitialUnit => Settings.ADS_INTERSTITIAL_UNIT;
|
|||
|
public string RewardUnit => Settings.ADS_REWARD_UNIT;
|
|||
|
public string SdkKey => Settings.ADS_MEDIATION_KEY;
|
|||
|
|
|||
|
private readonly List<EnumProductAdType> _queue
|
|||
|
= new List<EnumProductAdType>();
|
|||
|
|
|||
|
#if RND_MOPUB
|
|||
|
private float _stampRequest = 0;
|
|||
|
private const float REQUEST_DELAY = 3.0f;
|
|||
|
#endif
|
|||
|
|
|||
|
public void Init(Action callback)
|
|||
|
{
|
|||
|
#if RND_MOPUB
|
|||
|
MoPub.LoadBannerPluginsForAdUnits(BannerUnit);
|
|||
|
MoPub.LoadInterstitialPluginsForAdUnits(InterstitialUnit);
|
|||
|
MoPub.LoadRewardedVideoPluginsForAdUnits(RewardUnit);
|
|||
|
|
|||
|
MoPubManager.OnRewardedVideoReceivedRewardEvent += (a, b, c) => OnRewardSuccess($"MoPubSDK: Reward receive {c}");
|
|||
|
MoPubManager.OnRewardedVideoExpiredEvent += (a) => OnRewardFail("MoPubSDK: Reward expired.");
|
|||
|
MoPubManager.OnRewardedVideoFailedEvent += OnRewardedVideoFailedEvent;
|
|||
|
MoPubManager.OnRewardedVideoFailedToPlayEvent += (a, b) => OnRewardFail("MoPubSDK: Reward video play failed.");
|
|||
|
MoPubManager.OnRewardedVideoClosedEvent += (a) => OnRewardClosed("MoPubSDK: Reward video closed.");
|
|||
|
MoPubManager.OnRewardedVideoLeavingApplicationEvent += (a) => OnRewardFail("MoPubSDK: Application leaved.");
|
|||
|
MoPubManager.OnRewardedVideoLoadedEvent += OnRewardedVideoLoadedEvent;
|
|||
|
|
|||
|
MoPubManager.OnInterstitialClickedEvent += (a) => HandleInterstitialLeftApplication();
|
|||
|
MoPubManager.OnInterstitialShownEvent += (a) => OnInterSuccess("MoPubSDK: Interstitial shown");
|
|||
|
MoPubManager.OnInterstitialDismissedEvent += (a) => OnInterFail("MoPubSDK: Interstitial dismissed");
|
|||
|
MoPubManager.OnInterstitialFailedEvent += (a, b) => OnInterFail("MoPubSDK: Interstitial failed");
|
|||
|
MoPubManager.OnInterstitialExpiredEvent += (a) => OnInterFail("MoPubSDK: Interstitial expired");
|
|||
|
|
|||
|
Ads.OnRemoveAdsStateChanged += OnRemoveAdsStateChangedEvent;
|
|||
|
|
|||
|
RequestBanner();
|
|||
|
ScheduleRequest(EnumProductAdType.REWARDED_VIDEO);
|
|||
|
ScheduleRequest(EnumProductAdType.INTERSTITIAL);
|
|||
|
G.Instance.StartCoroutine(RequestScheduler());
|
|||
|
|
|||
|
callback?.Invoke();
|
|||
|
Log.Info("MoPubSDK: Init");
|
|||
|
#endif
|
|||
|
}
|
|||
|
|
|||
|
private void RequestBanner()
|
|||
|
{
|
|||
|
#if RND_MOPUB
|
|||
|
foreach (var bannerAdUnit in MoPubInitHelper._bannerAdUnits)
|
|||
|
MoPub.RequestBanner(bannerAdUnit, MoPub.AdPosition.BottomCenter, MoPub.MaxAdSize.Width320Height50);
|
|||
|
#endif
|
|||
|
}
|
|||
|
|
|||
|
public void DestroyBanner()
|
|||
|
{
|
|||
|
#if RND_MOPUB
|
|||
|
foreach (var bannerAdUnit in MoPubInitHelper._bannerAdUnits)
|
|||
|
{
|
|||
|
MoPub.ShowBanner(bannerAdUnit, false);
|
|||
|
MoPub.DestroyBanner(bannerAdUnit);
|
|||
|
}
|
|||
|
#endif
|
|||
|
}
|
|||
|
|
|||
|
private void ScheduleRequest(EnumProductAdType requestType)
|
|||
|
{
|
|||
|
#if RND_MOPUB
|
|||
|
if (!_queue.Contains(requestType))
|
|||
|
_queue.Add(requestType);
|
|||
|
#endif
|
|||
|
}
|
|||
|
|
|||
|
private IEnumerator RequestScheduler()
|
|||
|
{
|
|||
|
#if RND_MOPUB
|
|||
|
while (true)
|
|||
|
{
|
|||
|
if (_queue.Count > 0 && Time.time - _stampRequest > REQUEST_DELAY)
|
|||
|
{
|
|||
|
switch (_queue[0])
|
|||
|
{
|
|||
|
case EnumProductAdType.INTERSTITIAL:
|
|||
|
RequestInter();
|
|||
|
break;
|
|||
|
case EnumProductAdType.REWARDED_VIDEO:
|
|||
|
RequestReward();
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
_queue.RemoveAt(0);
|
|||
|
_stampRequest = Time.time;
|
|||
|
}
|
|||
|
yield return new WaitForSeconds(1);
|
|||
|
}
|
|||
|
#else
|
|||
|
yield return null;
|
|||
|
#endif
|
|||
|
}
|
|||
|
|
|||
|
#if RND_MOPUB
|
|||
|
private void OnRewardSuccess(string obj)
|
|||
|
{
|
|||
|
ScheduleRequest(EnumProductAdType.RewardedVideo);
|
|||
|
Ads.GetReward?.Invoke();
|
|||
|
}
|
|||
|
|
|||
|
private void OnRewardClosed(string obj)
|
|||
|
{
|
|||
|
ScheduleRequest(EnumProductAdType.RewardedVideo);
|
|||
|
Ads.GetClose?.Invoke();
|
|||
|
}
|
|||
|
|
|||
|
private void OnRewardFail(string obj)
|
|||
|
{
|
|||
|
ScheduleRequest(EnumProductAdType.RewardedVideo);
|
|||
|
Ads.GetFail?.Invoke();
|
|||
|
}
|
|||
|
|
|||
|
private void RequestRewardedVideo()
|
|||
|
{
|
|||
|
ScheduleRequest(EnumProductAdType.RewardedVideo);
|
|||
|
}
|
|||
|
|
|||
|
private void OnInterSuccess(string obj)
|
|||
|
{
|
|||
|
ScheduleRequest(EnumProductAdType.Interstitial);
|
|||
|
}
|
|||
|
|
|||
|
private void OnInterFail(string obj)
|
|||
|
{
|
|||
|
ScheduleRequest(EnumProductAdType.Interstitial);
|
|||
|
}
|
|||
|
#endif
|
|||
|
private void OnRemoveAdsStateChangedEvent()
|
|||
|
{
|
|||
|
if (Ads.AdsIsRemoved)
|
|||
|
DestroyBanner();
|
|||
|
}
|
|||
|
|
|||
|
public bool HasRewardVideo()
|
|||
|
{
|
|||
|
#if RND_MOPUB
|
|||
|
foreach (var rewardedAdUnit in MoPubInitHelper._rewardedAdUnits)
|
|||
|
if (MoPub.HasRewardedVideo(rewardedAdUnit))
|
|||
|
return true;
|
|||
|
#endif
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
public bool HasInterstitialVideo()
|
|||
|
{
|
|||
|
#if RND_MOPUB
|
|||
|
foreach (var interstitialAdUnit in MoPubInitHelper._interstitialAdUnits)
|
|||
|
if (MoPub.IsInterstitialReady(interstitialAdUnit))
|
|||
|
return true;
|
|||
|
#endif
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
public void ShowBanner()
|
|||
|
{
|
|||
|
#if RND_MOPUB
|
|||
|
foreach (var bannerAdUnit in MoPubInitHelper._bannerAdUnits)
|
|||
|
MoPub.ShowBanner(bannerAdUnit, true);
|
|||
|
#endif
|
|||
|
}
|
|||
|
|
|||
|
public void ShowInterstitial()
|
|||
|
{
|
|||
|
#if RND_MOPUB
|
|||
|
if (HasInterstitialVideo() == false)
|
|||
|
return;
|
|||
|
|
|||
|
foreach (var interstitialAdUnit in MoPubInitHelper._interstitialAdUnits)
|
|||
|
MoPub.ShowInterstitialAd(interstitialAdUnit);
|
|||
|
#endif
|
|||
|
}
|
|||
|
|
|||
|
public void ShowReward()
|
|||
|
{
|
|||
|
#if RND_MOPUB
|
|||
|
foreach (var rewardedAdUnit in MoPubInitHelper._rewardedAdUnits)
|
|||
|
MoPub.ShowRewardedVideo(rewardedAdUnit);
|
|||
|
#endif
|
|||
|
}
|
|||
|
}
|
|||
|
}
|