using System; using System.Collections; using System.Collections.Generic; using BigoAds.Scripts.Api; using UnityEngine; namespace WZ { public class BigoAdsManager : D_MonoSingleton, IAdService { public string ClientName => "Bigo"; public PlatformType Platfrom => PlatformType.Bigo; private BigoRewardedAdManager _rewardedAdManager; private BigoInterstitialAdManager _interstitialAdManager; private List _interstitialAdUnits = new List(); private List _rewardedAdUnits = new List(); public string _bigo_app_id; public bool _initialized { get; private set; } = false; private string _rvPos; private string _IvPos; private Action _rvCloseCallback = null; private Action _ivCloseCallback = null; private Action _rvShowFailedCallback = null; private float _rvStartLoadTime = 0; private float _ivStartLoadTime = 0; public void Initialize() { LoggerUtils.Debug("[Bigo] sdk init start"+_bigo_app_id+" rvid:"+_rewardedAdUnits+" ivid:"+_interstitialAdUnits); if (string.IsNullOrEmpty(_bigo_app_id)|| _initialized) return; BigoAdSdk.OnInitFinish += () => { LoggerUtils.Debug($"[Bigo] sdk init success"); LoadRewarded(); LoadInterstitial(); _initialized = true; }; var config = new BigoAdConfig.Builder() .SetAppId(_bigo_app_id) .SetDebugLog(false) .Build(); BigoAdSdk.Initialize(config); } public void RefreshAdsData() { _bigo_app_id = AdConfigParser.GetBigoAppId(); _rewardedAdUnits = AdConfigParser.GetBigoAdUnits(AdsType.Rewarded); _interstitialAdUnits = AdConfigParser.GetBigoAdUnits(AdsType.Interstitial); } #region 激励广告功能 // 各个回调处理方法 private void OnRewardedAdLoaded(string adUnitId) { AdsActionEvents.TrackAdLoaded(Platfrom,ClientName, adUnitId, AdsType.Rewarded,Time.realtimeSinceStartup - _rvStartLoadTime); } private void OnRewardedAdLoadFailed(string adUnitId, int code, string msg) { AdsActionEvents.TrackAdFailToLoad(Platfrom,ClientName, adUnitId, AdsType.Rewarded,Time.realtimeSinceStartup - _rvStartLoadTime, msg); } private void OnRewardedAdShowed(string adUnitId) { } private void OnRewardedAdDismissed(string adUnitId,double revenue) { AdsActionEvents.TrackAdClosed(Platfrom,ClientName, adUnitId, AdsType.Rewarded, _rvPos, revenue); _rvCloseCallback?.Invoke(true,revenue); _rvCloseCallback = null; LoadRewarded(); } private void OnRewardedAdError(string adUnitId, int code, string msg) { AdsActionEvents.TrackAdFailToShow(Platfrom, AdsType.Rewarded, msg, _rvPos); _rvShowFailedCallback?.Invoke(); _rvShowFailedCallback = null; LoadRewarded(); } private void OnRewardedAdClicked(string adUnitId,double revenue) { AdsActionEvents.TrackAdClicked(Platfrom,ClientName,adUnitId,AdsType.Rewarded,_rvPos,revenue); } public void LoadRewarded() { _rewardedAdManager?.Destroy(); if (_rewardedAdUnits.Count > 0) { _rewardedAdManager = new BigoRewardedAdManager(); _rewardedAdManager.InitializeAdUnits( _rewardedAdUnits, onAdLoaded: OnRewardedAdLoaded, onAdLoadFailed: OnRewardedAdLoadFailed, onAdShowed: OnRewardedAdShowed, onAdDismissed: OnRewardedAdDismissed, onAdError: OnRewardedAdError, onAdClicked: OnRewardedAdClicked ); _rvStartLoadTime = Time.realtimeSinceStartup; // todo 暂时支持一个广告位,后续支持多个广告位 AdsActionEvents.TrackAdStartLoad(Platfrom,ClientName,_rewardedAdUnits[0], AdsType.Rewarded); } } public bool IsRewardedAvailable() { if (!_initialized || _rewardedAdManager == null) return false; return _rewardedAdManager.GetAvailableAdUnits().Count > 0; } public void DisplayRewarded(string adPos, Action rewardCallback = null, Action showFailedCallback = null) { _rvPos = adPos; _rvCloseCallback = rewardCallback; _rvShowFailedCallback = showFailedCallback; _rewardedAdManager.ShowHighestPayingAd(); } public double GetRewardedRevenue() { if (!_initialized || _rewardedAdManager == null) return -1; return _rewardedAdManager.GetHighestPayingAdRevenue(); } #region 插页广告功能 private void OnInterstitialAdLoaded(string adUnitId) { AdsActionEvents.TrackAdLoaded(Platfrom,ClientName, adUnitId, AdsType.Interstitial,Time.realtimeSinceStartup - _ivStartLoadTime); } private void OnInterstitialAdLoadFailed(string adUnitId, int code, string msg) { AdsActionEvents.TrackAdFailToLoad(Platfrom,ClientName, adUnitId, AdsType.Interstitial,Time.realtimeSinceStartup - _ivStartLoadTime, msg); } private void OnInterstitialAdShowed(string adUnitId) { } private void OnInterstitialAdDismissed(string adUnitId,double revenue) { AdsActionEvents.TrackAdClosed(Platfrom,ClientName, adUnitId, AdsType.Interstitial, "", GetInterstitialRevenue()); _ivCloseCallback?.Invoke(revenue); _ivCloseCallback = null; LoadInterstitial(); } private void OnInterstitialAdError(string adUnitId, int code, string msg) { AdsActionEvents.TrackAdFailToShow(Platfrom, AdsType.Interstitial, msg, ""); _ivCloseCallback?.Invoke(0); _ivCloseCallback = null; LoadInterstitial(); } private void OnInterstitialAdClicked(string adUnitId) { AdsActionEvents.TrackAdClicked(Platfrom,ClientName, adUnitId, AdsType.Interstitial, "", GetInterstitialRevenue()); } public void LoadInterstitial() { _interstitialAdManager?.Destroy(); if (_interstitialAdUnits.Count > 0) { _interstitialAdManager = new BigoInterstitialAdManager(); _interstitialAdManager.InitializeAdUnits( _interstitialAdUnits, onAdLoaded: OnInterstitialAdLoaded, onAdLoadFailed: OnInterstitialAdLoadFailed, onAdShowed: OnInterstitialAdShowed, onAdDismissed: OnInterstitialAdDismissed, onAdError: OnInterstitialAdError, onAdClicked: OnInterstitialAdClicked ); _ivStartLoadTime = Time.realtimeSinceStartup; // todo 暂时支持一个广告位,后续支持多个广告位 AdsActionEvents.TrackAdStartLoad(Platfrom,ClientName,_interstitialAdUnits[0], AdsType.Interstitial); } } public bool IsInterstitialAvailable() { if (!_initialized || _interstitialAdManager == null) return false; return _interstitialAdManager.GetAvailableAdUnits().Count > 0; } public void DisplayInterstitial(string ivPos, IvType _IvType = IvType.IV1, Action closeCallback = null) { _IvPos = ivPos; _ivCloseCallback = closeCallback; _interstitialAdManager.ShowHighestPayingAd(); } public double GetInterstitialRevenue() { if (!_initialized || _interstitialAdManager == null) return -1; return _interstitialAdManager.GetHighestPayingAdRevenue(); } #endregion #region 开屏广告功能 public void LoadSplash() { } public bool IsSplashAvailable() { return false; } public void DisplaySplash() { } public double GetSplashRevenue() { return 0; } #endregion #region 原生广告功能 public void LoadNative() { } public bool IsNativeAvailable(string adUnitId) { return false; } public void DisplayNative(string _adPos, string adUnitId, NativeAdPosition position) { } public void RemoveNative(string adUnitId) { } public double GetNativeRevenue(string adUnitId) { return 0; } #endregion #region 横幅广告功能 public void LoadBanner() { } public bool IsBannerAvailable() { return false; } public void DisplayBanner() { } public void HideBanner() { } public double GetBannerRevenue() { return 0; } #endregion #region 收益上报 public void TrackAdImpression(double revenue, AdsType type, string placement) { AdjustTrackEvent.Instance.TrackAdEvent(revenue, ClientName, placement, placement); FireBaseAnalyticsManager.Instance.OnAdRevenueEvent(ClientName, ClientName, placement, type, revenue, type == AdsType.Rewarded ? _rvPos : "", AdPlayCountManager.GetAdPlayCount(type)); ShuShuEvent.Instance.OnAdRevenueEvent(ClientName, ClientName, placement, type.ToString(), revenue, type == AdsType.Rewarded ? _rvPos : "", AdPlayCountManager.GetAdPlayCount(type)); AdsActionEvents.TrackAdImpression(Platfrom,ClientName,placement,type,type == AdsType.Rewarded ? _rvPos : "",revenue); } #endregion } #endregion }