502 lines
18 KiB
C#
502 lines
18 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using GoogleMobileAds.Api;
|
|
using GoogleMobileAds.Common;
|
|
using UnityEngine;
|
|
|
|
|
|
namespace WZ
|
|
{
|
|
|
|
public class AdmobAdsManager : D_MonoSingleton<AdmobAdsManager>, IAdService
|
|
{
|
|
public string ClientName => "Admob";
|
|
public PlatformType Platfrom => PlatformType.Admob;
|
|
private AdmobBannerAdManager _admobBannerAdManager;
|
|
private AdmobCollapsibleBannerAdManager _admobCollapsibleBannerAdManager;
|
|
private AdmobInterstitialAdManager _admobInterstitialAdManager;
|
|
private AdmobRewardedAdManager _admobRewardedAdManager;
|
|
private AdmobNativeAdManager _admobNativeAdManager;
|
|
private AdmobSplashAdManager _admobSplashAdManager;
|
|
private List<string> _interstitialAdUnits = new List<string>();
|
|
private List<string> _rewardedAdUnits = new List<string>();
|
|
private List<string> _nativeAdUnits = new List<string>();
|
|
private List<string> _splashAdUnits = new List<string>();
|
|
private List<string> _bannerAdUnits = new List<string>();
|
|
private List<string> _collapsibleBannerAdUnits = new List<string>();
|
|
private string _rvPos;
|
|
private string _ivPos;
|
|
private string _naPos;
|
|
private Action<bool,double> _rvCloseCallback = null;
|
|
private Action<double> _ivCloseCallback = null;
|
|
private Action _rvShowFailedCallback = null;
|
|
private float _rvStartLoadTime = 0;
|
|
private float _ivStartLoadTime = 0;
|
|
private float _spStartLoadTime = 0;
|
|
public bool _splashLoaded = false;
|
|
public bool _initialized { get; private set; } = false;
|
|
private bool _receivedReward = false;
|
|
|
|
public void Initialize()
|
|
{
|
|
if (_initialized) return;
|
|
MobileAds.RaiseAdEventsOnUnityMainThread = true;
|
|
_initialized = true;
|
|
|
|
MobileAds.Initialize(initStatus =>
|
|
{
|
|
LoadBanner();
|
|
if (_interstitialAdUnits.Count > 0) LoadInterstitial();
|
|
if (_rewardedAdUnits.Count > 0) LoadRewarded();
|
|
if (_splashAdUnits.Count > 0) AdsSplashManager.Instance.InitSplash();
|
|
if (_nativeAdUnits.Count > 0) LoadNative();
|
|
LoggerUtils.Debug("[Admob] init success");
|
|
});
|
|
|
|
|
|
}
|
|
|
|
public void RefreshAdsData()
|
|
{
|
|
_bannerAdUnits = AdConfigParser.GetAdmobAdUnits(AdsType.Banner);
|
|
_collapsibleBannerAdUnits = AdConfigParser.GetAdmobAdUnits(AdsType.Banner, BannerType.Collapsible);
|
|
_interstitialAdUnits = AdConfigParser.GetAdmobAdUnits(AdsType.Interstitial);
|
|
_nativeAdUnits = AdConfigParser.GetAdmobAdUnits(AdsType.Native);
|
|
_splashAdUnits = AdConfigParser.GetAdmobAdUnits(AdsType.Splash);
|
|
_rewardedAdUnits = AdConfigParser.GetAdmobAdUnits(AdsType.Rewarded);
|
|
|
|
_admobBannerAdManager?.ClearAds(_bannerAdUnits.ToArray());
|
|
_admobCollapsibleBannerAdManager?.ClearAds(_collapsibleBannerAdUnits.ToArray());
|
|
_admobInterstitialAdManager?.ClearAds(_interstitialAdUnits.ToArray());
|
|
_admobNativeAdManager?.ClearAds(_nativeAdUnits.ToArray());
|
|
_admobSplashAdManager?.ClearAds(_splashAdUnits.ToArray());
|
|
_admobRewardedAdManager?.ClearAds(_rewardedAdUnits.ToArray());
|
|
}
|
|
|
|
#region 激励广告功能
|
|
public void LoadRewarded()
|
|
{
|
|
_admobRewardedAdManager?.Destroy();
|
|
_admobRewardedAdManager = new AdmobRewardedAdManager();
|
|
_admobRewardedAdManager.InitializeAdUnits(
|
|
_rewardedAdUnits,
|
|
OnRewardedAdLoaded,
|
|
OnRewardedAdLoadFailed,
|
|
OnRewardedAdShowed,
|
|
OnRewardedAdDismissed,
|
|
OnRewardedAdError,
|
|
OnRewardEarnReward,
|
|
OnRewardedAdClicked
|
|
);
|
|
_rvStartLoadTime = Time.realtimeSinceStartup;
|
|
AdsActionEvents.TrackAdStartLoad(Platfrom, AdsType.Rewarded);
|
|
}
|
|
public void DisplayRewarded(string adPos, Action<bool,double> rewardCallback = null, Action showFailedCallback = null)
|
|
{
|
|
_rvPos = adPos;
|
|
_rvCloseCallback = rewardCallback;
|
|
_rvShowFailedCallback = showFailedCallback;
|
|
_admobRewardedAdManager.ShowHighestPayingAd();
|
|
}
|
|
|
|
public double GetRewardedRevenue()
|
|
{
|
|
return _admobRewardedAdManager.GetHighestPayingAdRevenue();
|
|
}
|
|
|
|
public bool IsRewardedAvailable()
|
|
{
|
|
if (!_initialized || _rewardedAdUnits.Count == 0) return false;
|
|
return _admobRewardedAdManager.GetAvailableAdUnits().Count > 0;
|
|
}
|
|
|
|
private void OnRewardedAdLoaded(string adSource,string adUnitId)
|
|
{
|
|
AdsActionEvents.TrackAdLoaded(Platfrom,adSource,adUnitId,AdsType.Rewarded,Time.realtimeSinceStartup - _rvStartLoadTime);
|
|
}
|
|
|
|
private void OnRewardedAdLoadFailed(string adUnitId, int errorCode, string errorMsg)
|
|
{
|
|
AdsActionEvents.TrackAdFailToLoad(Platfrom,"",adUnitId,AdsType.Rewarded,Time.realtimeSinceStartup - _rvStartLoadTime,errorMsg);
|
|
}
|
|
|
|
private void OnRewardedAdShowed(string adUnitId)
|
|
{
|
|
|
|
}
|
|
|
|
private void OnRewardedAdDismissed(string adSource,string adUnitId,double revenue)
|
|
{
|
|
AdsActionEvents.TrackAdClosed(Platfrom,adSource,adUnitId,AdsType.Rewarded,_rvPos,revenue);
|
|
_rvCloseCallback?.Invoke(_receivedReward,revenue);
|
|
_rvCloseCallback = null;
|
|
_receivedReward = false;
|
|
LoadRewarded();
|
|
}
|
|
|
|
private void OnRewardedAdError(string adUnitId, int errorCode, string errorMsg)
|
|
{
|
|
_rvShowFailedCallback?.Invoke();
|
|
_rvShowFailedCallback = null;
|
|
LoadRewarded();
|
|
}
|
|
|
|
private void OnRewardedAdClicked(string adSource,string adUnitId,double revenue)
|
|
{
|
|
AdsActionEvents.TrackAdClicked(Platfrom,adSource,adUnitId,AdsType.Rewarded,_rvPos,revenue);
|
|
}
|
|
|
|
private void OnRewardEarnReward(bool reward)
|
|
{
|
|
_receivedReward = reward;
|
|
}
|
|
#endregion
|
|
|
|
#region 插页广告功能
|
|
public void LoadInterstitial()
|
|
{
|
|
_admobInterstitialAdManager?.Destroy();
|
|
_admobInterstitialAdManager = new AdmobInterstitialAdManager();
|
|
_admobInterstitialAdManager.InitializeAdUnits(
|
|
_interstitialAdUnits,
|
|
OnInterstitialAdLoaded,
|
|
OnInterstitialAdLoadFailed,
|
|
OnInterstitialAdShowed,
|
|
OnInterstitialAdDismissed,
|
|
OnInterstitialAdError,
|
|
OnInterstitialAdPaid,
|
|
OnInterstitialAdClicked
|
|
);
|
|
_ivStartLoadTime = Time.realtimeSinceStartup;
|
|
AdsActionEvents.TrackAdStartLoad(Platfrom,AdsType.Interstitial);
|
|
}
|
|
public double GetInterstitialRevenue()
|
|
{
|
|
return _admobInterstitialAdManager.GetHighestPayingAdRevenue();
|
|
}
|
|
|
|
public void DisplayInterstitial(string ivPos, IvType _IvType = IvType.IV1, Action<double> closeCallback = null)
|
|
{
|
|
_ivPos = ivPos;
|
|
_ivCloseCallback = closeCallback;
|
|
_admobInterstitialAdManager.ShowHighestPayingAd();
|
|
}
|
|
|
|
public bool IsInterstitialAvailable()
|
|
{
|
|
return _admobInterstitialAdManager.GetAvailableAdUnits().Count > 0;
|
|
}
|
|
|
|
private void OnInterstitialAdLoaded(string adSource,string adUnitId)
|
|
{
|
|
AdsActionEvents.TrackAdLoaded(Platfrom,adSource,adUnitId,AdsType.Interstitial,Time.realtimeSinceStartup - _ivStartLoadTime);
|
|
}
|
|
|
|
private void OnInterstitialAdLoadFailed(string adUnitId, int errorCode, string errorMsg)
|
|
{
|
|
AdsActionEvents.TrackAdFailToLoad(Platfrom,"","",AdsType.Interstitial,Time.realtimeSinceStartup - _ivStartLoadTime,errorMsg);
|
|
}
|
|
private void OnInterstitialAdClicked(string adSource,string adUnitId,double revenue)
|
|
{
|
|
AdsActionEvents.TrackAdClicked(Platfrom, adSource, adUnitId, AdsType.Interstitial, _ivPos, revenue);
|
|
}
|
|
|
|
private void OnInterstitialAdPaid(AdValue adValue)
|
|
{
|
|
|
|
}
|
|
|
|
private void OnInterstitialAdShowed(string adUnitId)
|
|
{
|
|
|
|
}
|
|
|
|
private void OnInterstitialAdDismissed(string adSource,string adUnitId,double revenue)
|
|
{
|
|
AdsActionEvents.TrackAdClosed(Platfrom, adSource, adUnitId, AdsType.Interstitial, _ivPos, revenue);
|
|
_ivCloseCallback?.Invoke(revenue);
|
|
_ivCloseCallback = null;
|
|
LoadInterstitial();
|
|
}
|
|
|
|
private void OnInterstitialAdError(string adUnitId, int errorCode, string errorMsg)
|
|
{
|
|
_ivCloseCallback?.Invoke(0);
|
|
_ivCloseCallback = null;
|
|
LoadInterstitial();
|
|
}
|
|
#endregion
|
|
|
|
|
|
|
|
#region 开屏广告功能
|
|
public void LoadSplash()
|
|
{
|
|
_admobSplashAdManager?.Destroy();
|
|
_admobSplashAdManager = new AdmobSplashAdManager();
|
|
_admobSplashAdManager.InitializeAdUnits(
|
|
_splashAdUnits,
|
|
OnSplashAdLoaded,
|
|
OnSplashAdLoadFailed,
|
|
OnSplashAdShowed,
|
|
OnSplashAdDismissed,
|
|
OnSplashAdError,
|
|
OnSplashAdPaid,
|
|
OnSplashAdClicked
|
|
);
|
|
_spStartLoadTime = Time.realtimeSinceStartup;
|
|
AdsActionEvents.TrackAdStartLoad(Platfrom,AdsType.Splash);
|
|
}
|
|
|
|
public bool IsSplashAvailable()
|
|
{
|
|
if (!_initialized) return false;
|
|
return _admobSplashAdManager.GetAvailableAdUnits().Count > 0;
|
|
}
|
|
public void DisplaySplash()
|
|
{
|
|
_admobSplashAdManager.ShowHighestPayingAd();
|
|
}
|
|
|
|
public double GetSplashRevenue()
|
|
{
|
|
return _admobSplashAdManager.GetHighestPayingAdRevenue();
|
|
}
|
|
|
|
private void OnSplashAdLoaded(string adSource,string adUnitId)
|
|
{
|
|
_splashLoaded = true;
|
|
AdsActionEvents.TrackAdLoaded(Platfrom,adSource,adUnitId,AdsType.Splash,Time.realtimeSinceStartup - _spStartLoadTime);
|
|
}
|
|
|
|
private void OnSplashAdLoadFailed(string adUnitId, int errorCode, string errorMsg)
|
|
{
|
|
_splashLoaded = true;
|
|
AdsActionEvents.TrackAdFailToLoad(Platfrom,"","",AdsType.Splash,Time.realtimeSinceStartup - _spStartLoadTime,errorMsg);
|
|
}
|
|
|
|
private void OnSplashAdShowed(string adUnitId)
|
|
{
|
|
AdsSDKManager.Instance.otherAdsOnShow = true;
|
|
}
|
|
|
|
private void OnSplashAdDismissed(string adSource, string adUnitId, double revenue)
|
|
{
|
|
AdsActionEvents.TrackAdClosed(Platfrom, adSource, adUnitId, AdsType.Splash, "", revenue);
|
|
AdsSDKManager.Instance.otherAdsOnShow = false;
|
|
AdsSDKManager.Instance.OnSplashAdCloseCallback?.Invoke();
|
|
}
|
|
|
|
private void OnSplashAdError(string adUnitId, int errorCode, string errorMsg)
|
|
{
|
|
AdsSDKManager.Instance.otherAdsOnShow = false;
|
|
AdsSDKManager.Instance.OnSplashAdCloseCallback?.Invoke();
|
|
}
|
|
|
|
private void OnSplashAdClicked(string adSource,string adUnitId,double revenue)
|
|
{
|
|
AdsActionEvents.TrackAdClicked(Platfrom,adSource,adUnitId,AdsType.Splash,"",revenue);
|
|
}
|
|
|
|
private void OnSplashAdPaid(AdValue adValue)
|
|
{
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
#region 原生广告功能
|
|
public void LoadNative()
|
|
{
|
|
_admobNativeAdManager?.Destroy();
|
|
_admobNativeAdManager = new AdmobNativeAdManager();
|
|
_admobNativeAdManager.InitializeAdUnits(
|
|
_nativeAdUnits
|
|
);
|
|
AdsActionEvents.TrackAdStartLoad(Platfrom, AdsType.Native);
|
|
}
|
|
|
|
public bool IsNativeAvailable(string adUnitId)
|
|
{
|
|
return _admobNativeAdManager.IsAdAvailable(adUnitId);
|
|
}
|
|
|
|
public void DisplayNative(string _adPos, string adUnitId, NativeAdPosition position)
|
|
{
|
|
_naPos = _adPos;
|
|
_admobNativeAdManager.ShowAd(position, adUnitId);
|
|
}
|
|
|
|
public void RemoveNative(string adUnitId)
|
|
{
|
|
StartCoroutine(_admobNativeAdManager.RemoveNative(adUnitId));
|
|
}
|
|
|
|
public double GetNativeRevenue(string adUnitId)
|
|
{
|
|
return _admobNativeAdManager.GetAdRevenue(adUnitId);
|
|
}
|
|
#endregion
|
|
|
|
#region 横幅广告功能
|
|
public void LoadBanner()
|
|
{
|
|
LoadStandardBanner();
|
|
LoadCollapsingBanner();
|
|
AdsActionEvents.TrackAdStartLoad(Platfrom,AdsType.Banner);
|
|
}
|
|
|
|
private void LoadStandardBanner()
|
|
{
|
|
if (_bannerAdUnits.Count > 0)
|
|
{
|
|
_admobBannerAdManager?.Destroy();
|
|
_admobBannerAdManager = new AdmobBannerAdManager();
|
|
_admobBannerAdManager.InitializeAdUnits(BannerAlignType.CenterBottom,_bannerAdUnits);
|
|
}
|
|
|
|
}
|
|
|
|
public void LoadCollapsingBanner()
|
|
{
|
|
if (_collapsibleBannerAdUnits.Count > 0)
|
|
{
|
|
_admobCollapsibleBannerAdManager?.Destroy();
|
|
_admobCollapsibleBannerAdManager = new AdmobCollapsibleBannerAdManager();
|
|
_admobCollapsibleBannerAdManager.InitializeAdUnits(BannerAlignType.CenterBottom,_collapsibleBannerAdUnits);
|
|
}
|
|
}
|
|
|
|
public bool IsBannerAvailable(BannerType bannerType)
|
|
{
|
|
if (!_initialized) return false;
|
|
if (bannerType == BannerType.Standard)
|
|
{
|
|
return _admobBannerAdManager?.GetAvailableAdUnits().Count > 0;
|
|
}
|
|
else
|
|
{
|
|
return _admobCollapsibleBannerAdManager?.GetAvailableAdUnits().Count > 0;
|
|
}
|
|
}
|
|
|
|
public void HideBanner(BannerType bannerType)
|
|
{
|
|
if (bannerType == BannerType.Standard)
|
|
{
|
|
_admobBannerAdManager?.HideBanner();
|
|
}
|
|
else
|
|
{
|
|
_admobCollapsibleBannerAdManager?.HideBanner();
|
|
}
|
|
}
|
|
|
|
public double GetBannerRevenue(BannerType bannerType)
|
|
{
|
|
return _admobBannerAdManager.GetHighestPayingAdRevenue();
|
|
}
|
|
|
|
public void DisplayBanner(BannerType bannerType,BannerAlignType bannerAlignType)
|
|
{
|
|
if (bannerType == BannerType.Standard)
|
|
{
|
|
_admobBannerAdManager?.ShowHighestPayingAd(bannerAlignType);
|
|
}
|
|
else
|
|
{
|
|
_admobCollapsibleBannerAdManager?.ShowHighestPayingAd(bannerAlignType);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 收益上报
|
|
public void TrackAdImpression(AdapterResponseInfo loadedAdapterResponseInfo, AdValue adValue, AdsType type, string placement)
|
|
{
|
|
AdjustTrackEvent.Instance.TrackAdEvent(adValue.Value / 1000000f,
|
|
loadedAdapterResponseInfo.AdSourceName,
|
|
placement,
|
|
loadedAdapterResponseInfo.AdSourceInstanceName);
|
|
|
|
var position = type switch
|
|
{
|
|
AdsType.Rewarded => _rvPos,
|
|
AdsType.Interstitial => _ivPos,
|
|
AdsType.Native => _naPos,
|
|
_ => ""
|
|
};
|
|
|
|
FireBaseAnalyticsManager.Instance.OnAdRevenueEvent(PlatformType.Admob.ToString(),
|
|
loadedAdapterResponseInfo.AdSourceName,
|
|
placement,
|
|
type,
|
|
adValue.Value / 1000000f,
|
|
position,
|
|
AdPlayCountManager.GetAdsActionCount(type, AdPlayCountManager.PLAY_COUNT_SUFFIX));
|
|
|
|
ShuShuEvent.Instance.OnAdRevenueEvent(PlatformType.Admob.ToString(),
|
|
loadedAdapterResponseInfo.AdSourceName,
|
|
placement,
|
|
type.ToString(),
|
|
adValue.Value / 1000000f,
|
|
position,
|
|
AdPlayCountManager.GetAdsActionCount(type, AdPlayCountManager.PLAY_COUNT_SUFFIX));
|
|
}
|
|
#endregion
|
|
|
|
#region 广告行为事件上报
|
|
public void TrackAdAction(AdapterResponseInfo loadedAdapterResponseInfo, double revenue, AdsType type, string placement)
|
|
{
|
|
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 判断当前广告id的配置是否包含这个id
|
|
/// </summary>
|
|
/// <param name="adsType"></param>
|
|
/// <param name="adUnitId"></param>
|
|
/// <returns></returns>
|
|
public bool FindAdsID(AdsType adsType, string adUnitId,BannerType bannerType = BannerType.Standard)
|
|
{
|
|
if (adsType == AdsType.Rewarded)
|
|
{
|
|
return _rewardedAdUnits.Contains(adUnitId);
|
|
}
|
|
else if (adsType == AdsType.Interstitial)
|
|
{
|
|
return _interstitialAdUnits.Contains(adUnitId);
|
|
}
|
|
else if (adsType == AdsType.Native)
|
|
{
|
|
return _nativeAdUnits.Contains(adUnitId);
|
|
}
|
|
else if (adsType == AdsType.Splash)
|
|
{
|
|
return _splashAdUnits.Contains(adUnitId);
|
|
}
|
|
else if (adsType == AdsType.Banner)
|
|
{
|
|
if (bannerType == BannerType.Standard)
|
|
{
|
|
return _bannerAdUnits.Contains(adUnitId);
|
|
}
|
|
else
|
|
{
|
|
return _collapsibleBannerAdUnits.Contains(adUnitId);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|