You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

322 lines
9.7 KiB

using UnityEngine;
#if ADS_ENABLE && ADS_ADMOB && (UNITY_ANDROID || UNITY_IOS)
using GoogleMobileAds.Api;
using GoogleMobileAds.Api.Mediation.AppLovin;
using GoogleMobileAds.Api.Mediation.Vungle;
#endif
public class AdMobMgr : MonoSingleton<AdMobMgr>
{
#region Variables
#if ADS_ENABLE && ADS_ADMOB && (UNITY_ANDROID || UNITY_IOS)
protected static RewardedAd rewardBasedVideo = null;
#endif
protected static float RELOAD_DELAY = 1f;
protected static bool B_INIT = false;
protected static bool B_PLAYING = false;
protected static bool B_OPEN = false;
protected static bool B_CLOSE = false;
protected static bool B_REWARD = false;
protected static bool B_Ready = false;
protected static bool B_Loading = false;
#if VER_ONESTORE
public const string AdRewardUnitId = "ca-app-pub-9646992028424809/4855252576";
#elif UNITY_ANDROID
public const string AdRewardUnitId = "ca-app-pub-9646992028424809/2987260143";
#elif UNITY_IOS
public const string AdRewardUnitId = "ca-app-pub-9646992028424809/1314305573";
#else
public const string AdRewardUnitId = "unexpected_platform";
#endif
#endregion Variables
public void InitAdsMgr(AdsMgr adsmgr)
{
RELOAD_DELAY = 1f;
#if ADS_ENABLE && ADS_ADMOB && (UNITY_ANDROID || UNITY_IOS)
if (!B_INIT)
MobileAds.Initialize(OnAdsInitialized);
else
CheckRewardVideoLoad();
#endif
}
protected void InitRewardVideo()
{
if (!B_INIT)
return;
#if ADS_ENABLE && ADS_ADMOB && (UNITY_ANDROID || UNITY_IOS)
Debug.Log("Admob - InitRewardVideo");
AdRequest request;
if (DataMgr.IsGDPR && !DataMgr.IsGDPRAgree)
request = new AdRequest.Builder().AddExtra("max_ad_content_rating", "T").AddExtra("npa", "1").Build();
else
request = new AdRequest.Builder().AddExtra("max_ad_content_rating", "T").Build();
if (B_Ready)
{
B_Loading = true;
if (rewardBasedVideo != null)
rewardBasedVideo.Destroy();
rewardBasedVideo = new RewardedAd(AdRewardUnitId6);
rewardBasedVideo.OnAdLoaded += OnRewardVideoLoad;
rewardBasedVideo.OnAdFailedToLoad += OnRewardVideoFailedToLoad;
rewardBasedVideo.OnAdOpening += OnRewardVideoOpening;
rewardBasedVideo.OnAdFailedToShow += OnRewardVideoFailedToShow;
rewardBasedVideo.OnUserEarnedReward += OnRewardVideoRewarded;
rewardBasedVideo.OnAdClosed += OnRewardVideoClosed;
rewardBasedVideo.LoadAd(request);
}
#endif
}
protected void InitRewardVideo6()
{
#if ADS_ENABLE && ADS_ADMOB && (UNITY_ANDROID || UNITY_IOS)
if (!B_INIT)
return;
if (B_Loading)
return;
Debug.Log("Admob - InitRewardVideo6");
B_Loading = true;
AdRequest request;
if (DataMgr.IsGDPR && !DataMgr.IsGDPRAgree)
request = new AdRequest.Builder().AddExtra("max_ad_content_rating", "T").AddExtra("npa", "1").Build();
else
request = new AdRequest.Builder().AddExtra("max_ad_content_rating", "T").Build();
if (rewardBasedVideo != null)
rewardBasedVideo.Destroy();
rewardBasedVideo = new RewardedAd(AdRewardUnitId6);
rewardBasedVideo.OnAdLoaded += OnRewardVideoLoad;
rewardBasedVideo.OnAdFailedToLoad += OnRewardVideoFailedToLoad;
rewardBasedVideo.OnAdOpening += OnRewardVideoOpening;
rewardBasedVideo.OnAdFailedToShow += OnRewardVideoFailedToShow;
rewardBasedVideo.OnUserEarnedReward += OnRewardVideoRewarded;
rewardBasedVideo.OnAdClosed += OnRewardVideoClosed;
rewardBasedVideo.LoadAd(request);
#endif
}
public void CheckRewardVideoLoad()
{
#if ADS_ENABLE && ADS_ADMOB && (UNITY_ANDROID || UNITY_IOS)
if (!B_INIT)
return;
Debug.Log("Admob - CheckRewardVideoLoad");
if (rewardBasedVideo == null || !rewardBasedVideo.IsLoaded())
{
AdMobMgr.Instance.InitRewardVideo6();
}
#endif
}
protected void LoadRewardVideo6()
{
#if ADS_ENABLE && ADS_ADMOB && (UNITY_ANDROID || UNITY_IOS)
if (!B_INIT)
return;
Debug.Log("Admob - LoadRewardVideo6");
if (rewardBasedVideo == null)
{
InitRewardVideo6();
return;
}
AdRequest request;
if (DataMgr.IsGDPR && !DataMgr.IsGDPRAgree)
request = new AdRequest.Builder().AddExtra("max_ad_content_rating", "T").AddExtra("npa", "1").Build();
else
request = new AdRequest.Builder().AddExtra("max_ad_content_rating", "T").Build();
rewardBasedVideo.LoadAd(request);
#endif
}
#if ADS_ENABLE && ADS_ADMOB && (UNITY_ANDROID || UNITY_IOS)
public void OnAdsInitialized(InitializationStatus initstatus)
{
Debug.Log("Admob Initialized : " + initstatus.ToString());
B_INIT = true;
#if SERVER_DEV || SERVER_BETA || STAGE_TEST
Dictionary<string, AdapterStatus> map = initstatus.getAdapterStatusMap();
foreach (KeyValuePair<string, AdapterStatus> keyValuePair in map)
{
string className = keyValuePair.Key;
AdapterStatus status = keyValuePair.Value;
Debug.Log("Admob - [" + className + "] : " + status.InitializationState.ToString());
}
#endif
B_Ready = true;
InitRewardVideo();
AppLovin.Initialize();
}
public void OnRewardVideoLoad(object sender, System.EventArgs e)
{
Debug.Log("Admob OnAdLoaded");
CheckLoadRewardVideo(sender);
}
private void OnRewardVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
Debug.Log("Admob OnAdFailedToLoad\n" + args.LoadAdError.ToString());
//Debug.Log("Admob OnAdFailedToLoad : " + sender + " ::: " + sender.ToString() + " / " + args.LoadAdError.ToString());
ReloadRewardVideo(sender);
LoadAdError loadAdError = args.LoadAdError;
// Get response information, which may include results of mediation requests.
string strresponse = null;
ResponseInfo responseInfo = loadAdError.GetResponseInfo();
if (responseInfo != null)
strresponse = responseInfo.ToString();
AnalyticsMgr.EventFailAdsLoad(loadAdError.GetDomain(), loadAdError.GetCode(), loadAdError.GetMessage(), strresponse);
}
private void OnRewardVideoOpening(object sender, System.EventArgs e)
{
if (B_OPEN)
return;
B_OPEN = true;
B_PLAYING = true;
B_CLOSE = false;
B_REWARD = false;
Debug.Log("Admob OnAdOpening");
}
public void OnRewardVideoFailedToShow(object sender, AdErrorEventArgs args)
{
Debug.Log("Admob OnAdFailedToShow : " + args.ToString());
if (adsMgr != null)
{
adsMgr.InvokeAdsFinishFail();
}
AdError adError = args.AdError;
AnalyticsMgr.EventFailAdsShow(adError.GetDomain(), adError.GetCode(), adError.GetMessage());
}
public void OnRewardVideoRewarded(object sender, Reward e)
{
if (B_REWARD)
return;
B_REWARD = true;
B_PLAYING = false;
if (adsMgr != null)
adsMgr.InvokeAdsRew();
Debug.Log("Admob OnAdRewarded");
}
public void OnRewardVideoClosed(object sender, System.EventArgs e)
{
if (B_CLOSE)
return;
B_CLOSE = true;
Debug.Log("Admob OnAdClosed");
if (B_PLAYING)
B_PLAYING = false;
if (adsMgr != null)
adsMgr.InvokeAdsClose();
CheckRewardVideoLoad();
}
#endif
public void ShowRewardVideo(int itype)
{
#if ADS_ENABLE && ADS_ADMOB && (UNITY_ANDROID || UNITY_IOS)
B_PLAYING = true;
B_OPEN = false;
B_CLOSE = false;
B_REWARD = false;
rewardBasedVideo.Show();
#endif
}
public void CheckLoadRewardVideo(object sender)
{
#if UNITY_EDITOR || SERVER_DEV || SERVER_STAGE || STAGE_TEST || !ADS_ENABLE || !ADS_ADMOB
#else
if (sender == null)
return;
if (sender == rewardBasedVideo)
{
Debug.Log("Admob - CheckLoadRewardVideo6");
B_Loading = false;
return;
}
if (RELOAD_DELAY > 1f)
RELOAD_DELAY -= 0.2f;
#endif
}
public void ReloadRewardVideo(object sender)
{
#if UNITY_EDITOR || SERVER_DEV || SERVER_STAGE || STAGE_TEST || !ADS_ENABLE || !ADS_ADMOB
return;
#else
if (sender == null)
return;
if (sender == rewardBasedVideo)
{
Debug.Log("Admob - ReloadRewardVideo6");
B_Loading = false;
Invoke("InitRewardVideo6", RELOAD_DELAY * Time.timeScale);
return;
}
if (RELOAD_DELAY < 3f)
RELOAD_DELAY += 0.2f;
#endif
}
public static bool GetAdsAvailableOnly()
{
#if UNITY_EDITOR || !ADS_ENABLE || !ADS_ADMOB
return true;
#else
if (!B_INIT)
return false;
return B_Ready && rewardBasedVideo != null && rewardBasedVideo.IsLoaded();
#endif
}
public static bool GetAdsAvailable()
{
#if UNITY_EDITOR || !ADS_ENABLE || !ADS_ADMOB
return false;
#else
if (!B_INIT)
return false;
if (!B_Ready)
return false;
if (rewardBasedVideo == null)
{
//Debug.Log("Admob - VIDEO NULL");
AdMobMgr.Instance.InitRewardVideo6();
return false;
}
if (!rewardBasedVideo.IsLoaded())
{
//Debug.Log("Admob - VIDEO NOT LOADED");
AdMobMgr.Instance.InitRewardVideo6();
return false;
}
return true;
#endif
}
}