275 lines
10 KiB
C#
275 lines
10 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using JetBrains.Annotations;
|
|
using KwaiAds.Scripts.Api.Interstitial;
|
|
using KwaiAds.Scripts.Api.Reward;
|
|
using KwaiAds.Scripts.Common;
|
|
using UnityEngine;
|
|
using static WZ.KwaiAdsConfigParser;
|
|
|
|
namespace WZ
|
|
{
|
|
public class KwaiAdsManager : D_MonoSingleton<KwaiAdsManager>, IAdService
|
|
{
|
|
//目前都是测试id
|
|
public string _appId = "";
|
|
public 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 bool _initialized { get; private set; } = false;
|
|
public bool _receivedReward = false;
|
|
|
|
public void Initialize()
|
|
{
|
|
LoggerUtils.Debug("KwaiAdsManager Initialize start" + _appId + " token:" + _token + " rewardAdUnitId:" + _rewardAdUnitId + " interstitialAdUnitId:" + _interstitialAdUnitId);
|
|
if (string.IsNullOrEmpty(_appId) || string.IsNullOrEmpty(_token) || _initialized) return;
|
|
|
|
if (KwaiAdsConfigParser.GetKwaiRvFloorOpen())
|
|
{
|
|
KwaiFloorRvManager.Instance.InitializeWithFloors();
|
|
}
|
|
|
|
if (KwaiAdsConfigParser.GetKwaiIvFloorOpen())
|
|
{
|
|
KwaiFloorIvManager.Instance.InitializeWithFloors();
|
|
}
|
|
|
|
var kwaiAdConfig = new KwaiAds.Scripts.Api.KwaiAdConfig.Builder()
|
|
.SetAppId(_appId)
|
|
.SetToken(_token)
|
|
.SetDebugLog(false)
|
|
.Build();
|
|
|
|
KwaiAds.Scripts.Api.KwaiAdsSdk.Initialize(kwaiAdConfig, new InitResultCallbackImpl());
|
|
|
|
_initialized = true;
|
|
}
|
|
|
|
|
|
public void RefreshAdsData()
|
|
{
|
|
_appId = AdConfigParser.GetKwaiAppId();
|
|
_token = AdConfigParser.GetKwaiAppToken();
|
|
_rewardAdUnitId = AdConfigParser.GetKwaiAdUnits(AdsType.Rewarded).FirstOrDefault();
|
|
_interstitialAdUnitId = AdConfigParser.GetKwaiAdUnits(AdsType.Interstitial).FirstOrDefault();
|
|
KwaiFloorRvManager.Instance.LoadKwaiBiddingConfig();
|
|
KwaiFloorIvManager.Instance.LoadKwaiBiddingConfig();
|
|
}
|
|
|
|
|
|
#region 激励广告
|
|
public void LoadRewarded()
|
|
{
|
|
AdsActionEvents.TrackAdStartLoad(Platfrom, AdsType.Rewarded);
|
|
if (!KwaiAdsConfigParser.GetKwaiRvFloorOpen())
|
|
{
|
|
LoadRewardedStandard();
|
|
return;
|
|
}
|
|
|
|
KwaiFloorRvManager.Instance.LoadRewardedWithFloors();
|
|
}
|
|
|
|
public void LoadRewardedStandard()
|
|
{
|
|
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());
|
|
_rvStartLoadTime = Time.realtimeSinceStartup;
|
|
}
|
|
|
|
public bool IsRewardedAvailable()
|
|
{
|
|
|
|
if (!KwaiAdsConfigParser.GetKwaiRvFloorOpen())
|
|
{
|
|
if (string.IsNullOrEmpty(_rewardAdUnitId)) return false;
|
|
return _rewardAdController != null && _rewardAdController.IsReady();
|
|
}
|
|
else
|
|
{
|
|
// 对于竞价模式,检查是否有成功的楼层广告
|
|
return KwaiFloorRvManager.Instance.IsRewardedAvailable();
|
|
}
|
|
}
|
|
|
|
public void DisplayRewarded(string _adPos, Action<bool, double> _rewardCallback = null, Action _showFailedCallback = null)
|
|
{
|
|
_rvPos = _adPos;
|
|
_rvCloseCallback = _rewardCallback;
|
|
_rvShowFailedCallback = _showFailedCallback;
|
|
|
|
if (!KwaiAdsConfigParser.GetKwaiRvFloorOpen())
|
|
{
|
|
if (_rewardAdController != null)
|
|
{
|
|
_rewardAdController.Show();
|
|
}
|
|
else
|
|
{
|
|
_rvShowFailedCallback?.Invoke();
|
|
LoadRewarded();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
KwaiFloorRvManager.Instance.ShowRewarded(()=> { _rvShowFailedCallback?.Invoke(); });
|
|
}
|
|
}
|
|
|
|
public double GetRewardedRevenue()
|
|
{
|
|
return _rewardAdRevenue;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 插屏
|
|
public void LoadInterstitial()
|
|
{
|
|
AdsActionEvents.TrackAdStartLoad(Platfrom, AdsType.Interstitial);
|
|
if (!KwaiAdsConfigParser.GetKwaiIvFloorOpen())
|
|
{
|
|
LoadInterstitialStandard();
|
|
return;
|
|
}
|
|
KwaiFloorIvManager.Instance.LoadInterstitialWithFloors();
|
|
}
|
|
|
|
private void LoadInterstitialStandard()
|
|
{
|
|
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());
|
|
_ivStartLoadTime = Time.realtimeSinceStartup;
|
|
}
|
|
|
|
public bool IsInterstitialAvailable()
|
|
{
|
|
if (!KwaiAdsConfigParser.GetKwaiIvFloorOpen())
|
|
{
|
|
if (string.IsNullOrEmpty(_interstitialAdUnitId)) return false;
|
|
return _interstitialAdController != null && _interstitialAdController.IsReady();
|
|
}
|
|
else
|
|
{
|
|
return KwaiFloorIvManager.Instance.IsInterstitialAdAvailable();
|
|
}
|
|
}
|
|
|
|
public void DisplayInterstitial(string _adPos, IvType _IvType = IvType.IV1, Action<double> _closeCallback = null)
|
|
{
|
|
_ivPos = _adPos;
|
|
_ivCloseCallback = _closeCallback;
|
|
|
|
if (!KwaiAdsConfigParser.GetKwaiIvFloorOpen())
|
|
{
|
|
if (_interstitialAdController != null)
|
|
{
|
|
_interstitialAdController.Show();
|
|
}
|
|
else
|
|
{
|
|
_closeCallback?.Invoke(0);
|
|
LoadInterstitial();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
KwaiFloorIvManager.Instance.ShowInterstitialAd(()=> { _ivCloseCallback?.Invoke(0); });
|
|
}
|
|
}
|
|
|
|
public double GetInterstitialRevenue()
|
|
{
|
|
return _interstitiaAdRevenue;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 横幅广告功能
|
|
public void LoadBanner() { }
|
|
public bool IsBannerAvailable(BannerType bannerType) { return false; }
|
|
public void DisplayBanner(BannerType bannerType,BannerAlignType bannerAlignType) { }
|
|
public void HideBanner(BannerType bannerType) { }
|
|
public double GetBannerRevenue(BannerType bannerType) { 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 : _ivPos,
|
|
AdPlayCountManager.GetAdsActionCount(type, AdPlayCountManager.PLAY_COUNT_SUFFIX));
|
|
|
|
ShuShuEvent.Instance.OnAdRevenueEvent(ClientName,
|
|
ClientName,
|
|
type == AdsType.Rewarded ? _rewardAdUnitId : _interstitialAdUnitId,
|
|
type.ToString(),
|
|
type == AdsType.Rewarded ? _rewardAdRevenue : _interstitiaAdRevenue,
|
|
type == AdsType.Rewarded ? _rvPos : _ivPos,
|
|
AdPlayCountManager.GetAdsActionCount(type, AdPlayCountManager.PLAY_COUNT_SUFFIX));
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
} |