SamsonGame/Assets/Scripts/Managers/mediation/Ads.cs

108 lines
2.5 KiB
C#
Raw Normal View History

2021-12-29 20:50:11 +03:00
using System;
using UnityEngine;
using RND.Mediation;
using System.Globalization;
namespace RND
{
public static class Ads
{
public delegate void FailFunc();
public delegate void RewardFunc();
public static RewardFunc RewardReceived;
public static FailFunc RewardFailed;
public static event Action OnRemoveAdsStateChanged;
public static event Action OnRewardShowed;
private static bool _isInitialized;
private static IAdsSDK _sdk = new NullAdsSDK();
private const string ADS_REMOVED = "ads_removed";
public static bool AdsIsRemoved
{
get => AdsRemovedGet();
set
{
AdsRemovedSet(value);
OnRemoveAdsStateChanged?.Invoke();
}
}
public static void Init()
{
LoadSDK();
}
private static void LoadSDK()
{
#if RND_MOPUB
_sdk = new MoPubSDK();
#elif RND_MAX
_sdk = new MaxSDK();
#endif
_sdk.Init(OnSdkInitialized);
}
private static bool AdsRemovedGet()
{
if (PlayerPrefs.HasKey(ADS_REMOVED) == false)
AdsRemovedSet(false);
return PlayerPrefs.GetInt(ADS_REMOVED) == 1;
}
private static void AdsRemovedSet(bool value)
{
PlayerPrefs.SetInt(ADS_REMOVED, value ? 1 : 0);
}
public static bool HasRewardVideo()
{
return _sdk.HasRewardVideo();
}
public static bool HasInterstitialVideo()
{
return _sdk.HasInterstitialVideo();
}
public static void ShowBanner()
{
if (!_isInitialized || AdsIsRemoved)
return;
_sdk.ShowBanner();
}
public static void ShowInterstitial(string placement)
{
if (!_isInitialized || AdsIsRemoved)
return;
_sdk.ShowInterstitial();
}
public static void ShowReward(string placement, RewardFunc reward, FailFunc fail)
{
RewardReceived = reward;
RewardFailed = fail;
if (!_isInitialized || AdsIsRemoved)
return;
OnRewardShowed?.Invoke();
_sdk.ShowReward();
}
private static void OnSdkInitialized()
{
_isInitialized = true;
Log.Info("Ads: Manager is initialized");
}
}
}