206 lines
		
	
	
		
			7.7 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			206 lines
		
	
	
		
			7.7 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System;
 | |
| using System.Collections;
 | |
| using System.Collections.Generic;
 | |
| using System.Linq;
 | |
| using KwaiAds.Scripts.Api.Interstitial;
 | |
| using KwaiAds.Scripts.Api.Reward;
 | |
| using UnityEngine;
 | |
| 
 | |
| namespace WZ
 | |
| {
 | |
|     public class KwaiAdsManager : D_MonoSingleton<KwaiAdsManager>, IAdService
 | |
|     {
 | |
|         //目前都是测试id
 | |
|         private string _appId = "";
 | |
|         private string _token = "";
 | |
|         public string _rewardAdUnitId = "";
 | |
|         public string _interstitialAdUnitId = "";
 | |
|         public double _rewardAdRevenue = -1;
 | |
|         public double _interstitiaAdRevenue = -1;
 | |
|         private IRewardAdController _rewardAdController;
 | |
|         private IInterstitialAdController _interstitialAdController;
 | |
|         public string _rvPos;
 | |
|         public string _ivPos;
 | |
|         public Action<bool, double> _rvCloseCallback = null;
 | |
|         public Action<double> _ivCloseCallback = null;
 | |
|         public Action _rvShowFailedCallback = null;
 | |
|         public int _rewardRetryAttempt;
 | |
|         public int _interRetryAttempt;
 | |
|         public float _rvStartLoadTime = 0;
 | |
|         public float _ivStartLoadTime = 0;
 | |
|         public string ClientName => "Kwai";
 | |
| 
 | |
|         public PlatformType Platfrom => PlatformType.Kwai;
 | |
| 
 | |
|         public void Initialize()
 | |
|         {
 | |
|             if (string.IsNullOrEmpty(_appId) || string.IsNullOrEmpty(_token)) return;
 | |
|             var kwaiAdConfig = new KwaiAds.Scripts.Api.KwaiAdConfig.Builder()
 | |
|                 .SetAppId(_appId)
 | |
|                 .SetToken(_token)
 | |
|                 .SetDebugLog(false)
 | |
|                 .Build();
 | |
| 
 | |
|             KwaiAds.Scripts.Api.KwaiAdsSdk.Initialize(kwaiAdConfig, new InitResultCallbackImpl());
 | |
|         }
 | |
| 
 | |
|         public void RefreshAdsData()
 | |
|         {
 | |
|             _appId = AdConfigParser.GetKwaiAppId();
 | |
|             _token = AdConfigParser.GetKwaiAppToken();
 | |
|             _rewardAdUnitId = AdConfigParser.GetKwaiAdUnits(AdsType.Rewarded).FirstOrDefault();
 | |
|             _interstitialAdUnitId = AdConfigParser.GetKwaiAdUnits(AdsType.Interstitial).FirstOrDefault();
 | |
|         }
 | |
| 
 | |
| 
 | |
|         #region 激励广告
 | |
|         public void LoadRewarded()
 | |
|         {
 | |
|             if (string.IsNullOrEmpty(_rewardAdUnitId)) return;
 | |
|             if (_rewardAdController != null)
 | |
|             {
 | |
|                 _rewardAdController.Destroy();
 | |
|                 _rewardAdController = null;
 | |
|                 _rewardAdRevenue = 0;
 | |
|             }
 | |
| 
 | |
|             _rewardAdController = KwaiAds.Scripts.Api.KwaiAdsSdk.SDK.getRewardAdController();
 | |
|             KwaiRewardAdRequest kwaiRewardAdRequest = new KwaiRewardAdRequest(_rewardAdUnitId);
 | |
|             _rewardAdController.Load(kwaiRewardAdRequest, new RewardAdListener(), new RewardAdLoadListener());
 | |
|             AdsActionEvents.TrackAdStartLoad(Platfrom, "", "", AdsType.Rewarded);
 | |
|             _ivStartLoadTime = Time.realtimeSinceStartup;
 | |
|         }
 | |
| 
 | |
|         public bool IsRewardedAvailable()
 | |
|         {
 | |
|             if (string.IsNullOrEmpty(_rewardAdUnitId)) return false;
 | |
|             return _rewardAdController != null && _rewardAdController.IsReady();
 | |
|         }
 | |
| 
 | |
|         public void DisplayRewarded(string _adPos, Action<bool, double> _rewardCallback = null, Action _showFailedCallback = null)
 | |
|         {
 | |
|             _adPos = _rvPos;
 | |
|             _rvCloseCallback = _rewardCallback;
 | |
|             _rvShowFailedCallback = _showFailedCallback;
 | |
|             if (_rewardAdController != null)
 | |
|             {
 | |
|                 _rewardAdController.Show();
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 _showFailedCallback?.Invoke();
 | |
|                 LoadRewarded();
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public double GetRewardedRevenue()
 | |
|         {
 | |
|             return _rewardAdRevenue;
 | |
|         }
 | |
|         #endregion
 | |
| 
 | |
|         #region 插屏
 | |
|         public void LoadInterstitial()
 | |
|         {
 | |
|             if (string.IsNullOrEmpty(_interstitialAdUnitId)) return;
 | |
|             if (_interstitialAdController != null)
 | |
|             {
 | |
|                 _interstitialAdController.Destroy();
 | |
|                 _interstitialAdController = null;
 | |
|                 _interstitiaAdRevenue = 0;
 | |
|             }
 | |
| 
 | |
|             _interstitialAdController = KwaiAds.Scripts.Api.KwaiAdsSdk.SDK.getInterstitialAdController();
 | |
|             KwaiInterstitialAdRequest kwaiInterstitialAdRequest = new KwaiInterstitialAdRequest(_interstitialAdUnitId);
 | |
|             _interstitialAdController.Load(kwaiInterstitialAdRequest, new InterstitialAdListener(), new InterstitialAdLoadListener());
 | |
|             AdsActionEvents.TrackAdStartLoad(Platfrom, "", "", AdsType.Interstitial);
 | |
|             _ivStartLoadTime = Time.realtimeSinceStartup;
 | |
|         }
 | |
| 
 | |
|         public bool IsInterstitialAvailable()
 | |
|         {
 | |
|             if (string.IsNullOrEmpty(_interstitialAdUnitId)) return false;
 | |
|             return _interstitialAdController != null && _interstitialAdController.IsReady();
 | |
|         }
 | |
| 
 | |
|         public void DisplayInterstitial(string _adPos, IvType _IvType = IvType.IV1, Action<double> _closeCallback = null)
 | |
|         {
 | |
|             _ivCloseCallback = _closeCallback;
 | |
|             if (_interstitialAdController != null)
 | |
|             {
 | |
|                 _interstitialAdController.Show();
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 _closeCallback?.Invoke(0);
 | |
|                 LoadInterstitial();
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public double GetInterstitialRevenue()
 | |
|         {
 | |
|             return _interstitiaAdRevenue;
 | |
|         }
 | |
| 
 | |
|         #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 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 TrackAdImpression(AdsType type)
 | |
|         {
 | |
|             AdjustTrackEvent.Instance.TrackAdEvent(type == AdsType.Rewarded ? _rewardAdRevenue : _interstitiaAdRevenue,
 | |
|                 ClientName,
 | |
|                 type == AdsType.Rewarded ? _rewardAdUnitId : _interstitialAdUnitId,
 | |
|                 type == AdsType.Rewarded ? _rewardAdUnitId : _interstitialAdUnitId);
 | |
| 
 | |
|             FireBaseAnalyticsManager.Instance.OnAdRevenueEvent(ClientName,
 | |
|                 ClientName,
 | |
|                 type == AdsType.Rewarded ? _rewardAdUnitId : _interstitialAdUnitId,
 | |
|                 type,
 | |
|                 type == AdsType.Rewarded ? _rewardAdRevenue : _interstitiaAdRevenue,
 | |
|                 type == AdsType.Rewarded ? _rvPos : "",
 | |
|                 AdPlayCountManager.GetAdPlayCount(type));
 | |
| 
 | |
|             ShuShuEvent.Instance.OnAdRevenueEvent(ClientName,
 | |
|                 ClientName,
 | |
|                 type == AdsType.Rewarded ? _rewardAdUnitId : _interstitialAdUnitId,
 | |
|                 type.ToString(),
 | |
|                 type == AdsType.Rewarded ? _rewardAdRevenue : _interstitiaAdRevenue,
 | |
|                 type == AdsType.Rewarded ? _rvPos : "",
 | |
|                 AdPlayCountManager.GetAdPlayCount(type));
 | |
| 
 | |
|             AdsActionEvents.TrackAdImpression(Platfrom,
 | |
|                 ClientName,
 | |
|                 type == AdsType.Rewarded ? _rewardAdUnitId : _interstitialAdUnitId,
 | |
|                 type,
 | |
|                 type == AdsType.Rewarded ? _rvPos : "",
 | |
|                 type == AdsType.Rewarded ? _rewardAdRevenue : _interstitiaAdRevenue);
 | |
|         }
 | |
| 
 | |
|         #endregion
 | |
| 
 | |
|     }
 | |
| 
 | |
| } |