kwai激励视频竞价逻辑

This commit is contained in:
juncong lee 2025-09-15 10:19:03 +08:00
parent d5ed16b469
commit 94fe636f02
25 changed files with 510 additions and 12 deletions

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e1de1c3815a024a79ba1edf8c1972ac2
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,97 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace WZ
{
public static class KwaiAdsConfigParser
{
private static KwaiBiddingConfig _rvConfig;
private static KwaiBiddingConfig _ivConfig;
public static void Parse(string json, bool isRv)
{
try
{
if (string.IsNullOrEmpty(json))
{
LoggerUtils.Error("KwaiBiddingConfig JSON string is null or empty");
}
if (isRv)
{
_rvConfig = JsonUtility.FromJson<KwaiBiddingConfig>(json);
}
else
{
_ivConfig = JsonUtility.FromJson<KwaiBiddingConfig>(json);
}
LoggerUtils.Info("KwaiBiddingConfig parsed successfully");
}
catch (Exception e)
{
LoggerUtils.Error($"Error parsing KwaiBiddingConfig: {e.Message}");
}
}
public static bool GetKwaiRvFloorOpen()
{
if (_rvConfig == null)
{
return false;
}
return _rvConfig.kwai_floor_open == 1;
}
public static int GetRvParallelRequests()
{
if (_rvConfig == null)
{
return 0;
}
return _rvConfig.parallel_requests;
}
public static List<FloorConfig> GetRvFloorConfigs()
{
if (_rvConfig == null)
{
return null;
}
return _rvConfig.floors;
}
public static string GetKwaiAppId()
{
return _rvConfig?.kwai_appid;
}
/// <summary>
/// 获取 Kwai App Token
/// </summary>
public static string GetKwaiAppToken()
{
return _rvConfig?.kwai_apptoken;
}
[System.Serializable]
public class FloorConfig
{
public string id;
public string unite_id;
public int price;
}
[System.Serializable]
public class KwaiBiddingConfig
{
public int kwai_floor_open;
public string kwai_appid;
public string kwai_apptoken;
public int parallel_requests;
public List<FloorConfig> floors;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 96816b8448fb34ac2b7c431715db83dd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -2,17 +2,20 @@ 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
private string _appId = "";
private string _token = "";
public string _appId = "";
public string _token = "";
public string _rewardAdUnitId = "";
public string _interstitialAdUnitId = "";
public double _rewardAdRevenue = -1;
@ -33,10 +36,18 @@ namespace WZ
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())
{
KwaiFloorAdsManager.Instance.InitializeWithFloors();
}
var kwaiAdConfig = new KwaiAds.Scripts.Api.KwaiAdConfig.Builder()
.SetAppId(_appId)
.SetToken(_token)
@ -44,20 +55,34 @@ namespace WZ
.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();
KwaiFloorAdsManager.Instance.LoadKwaiBiddingConfig();
}
#region 广
public void LoadRewarded()
{
if (!KwaiAdsConfigParser.GetKwaiRvFloorOpen())
{
LoadRewardedStandard();
return;
}
KwaiFloorAdsManager.Instance.LoadRewardedWithFloors();
}
public void LoadRewardedStandard()
{
if (string.IsNullOrEmpty(_rewardAdUnitId)) return;
if (_rewardAdController != null)
@ -75,31 +100,49 @@ namespace WZ
}
public bool IsRewardedAvailable()
{
if (!KwaiAdsConfigParser.GetKwaiRvFloorOpen())
{
if (string.IsNullOrEmpty(_rewardAdUnitId)) return false;
return _rewardAdController != null && _rewardAdController.IsReady();
}
else
{
// 对于竞价模式,检查是否有成功的楼层广告
return KwaiFloorAdsManager.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
{
_showFailedCallback?.Invoke();
_rvShowFailedCallback?.Invoke();
LoadRewarded();
}
}
else
{
KwaiFloorAdsManager.Instance.ShowRewarded(()=> { _rvShowFailedCallback?.Invoke(); });
}
}
public double GetRewardedRevenue()
{
return _rewardAdRevenue;
}
#endregion
#region
@ -185,7 +228,7 @@ namespace WZ
type,
type == AdsType.Rewarded ? _rewardAdRevenue : _interstitiaAdRevenue,
type == AdsType.Rewarded ? _rvPos : _ivPos,
AdPlayCountManager.GetAdsActionCount(type,AdPlayCountManager.PLAY_COUNT_SUFFIX));
AdPlayCountManager.GetAdsActionCount(type, AdPlayCountManager.PLAY_COUNT_SUFFIX));
ShuShuEvent.Instance.OnAdRevenueEvent(ClientName,
ClientName,
@ -193,7 +236,7 @@ namespace WZ
type.ToString(),
type == AdsType.Rewarded ? _rewardAdRevenue : _interstitiaAdRevenue,
type == AdsType.Rewarded ? _rvPos : _ivPos,
AdPlayCountManager.GetAdsActionCount(type,AdPlayCountManager.PLAY_COUNT_SUFFIX));
AdPlayCountManager.GetAdsActionCount(type, AdPlayCountManager.PLAY_COUNT_SUFFIX));
}
#endregion

View File

@ -0,0 +1,181 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using KwaiAds.Scripts.Api.Reward;
using UnityEngine;
using static WZ.KwaiAdsConfigParser;
namespace WZ
{
public class KwaiFloorAdsManager : D_MonoSingleton<KwaiFloorAdsManager>
{
private int _rvParallelRequests = 3;
private List<FloorConfig> _rvFloorConfigs = new List<FloorConfig>();
public Dictionary<string, IRewardAdController> _rvFloorAdControllers = new Dictionary<string, IRewardAdController>();
private List<FloorConfig> _sortedFloors = new List<FloorConfig>();
private int _currentFloorIndex = 0;
private bool _isRequestingFloors = false;
private List<FloorConfig> _currentRequestBatch = new List<FloorConfig>();
private FloorConfig _successfulFloor = null;
public void InitializeWithFloors()
{
// 对楼层按价格从高到低排序
_sortedFloors = _rvFloorConfigs.OrderByDescending(f => f.price).ToList();
LoggerUtils.Debug("Sorted floors: " + string.Join(", ", _sortedFloors.Select(f => $"{f.id}:{f.price}")));
}
public void LoadKwaiBiddingConfig()
{
_rvParallelRequests = KwaiAdsConfigParser.GetRvParallelRequests();
_rvFloorConfigs = KwaiAdsConfigParser.GetRvFloorConfigs();
KwaiAdsManager.Instance._appId = KwaiAdsConfigParser.GetKwaiAppId();
KwaiAdsManager.Instance._token = KwaiAdsConfigParser.GetKwaiAppToken();
LoggerUtils.Debug($"Kwai bidding config loaded. FloorOpen: {KwaiAdsConfigParser.GetKwaiRvFloorOpen()}, ParallelRequests: {_rvParallelRequests}, Floors: {_rvFloorConfigs.Count}");
}
public void LoadRewardedWithFloors()
{
if (_rvFloorConfigs == null || _rvFloorConfigs.Count == 0)
{
LoggerUtils.Debug("No floor configs available, using standard load");
KwaiAdsManager.Instance.LoadRewardedStandard();
return;
}
// 重置状态
_currentFloorIndex = 0;
_successfulFloor = null;
_isRequestingFloors = true;
// 清理之前的广告控制器
foreach (var controller in _rvFloorAdControllers.Values)
{
controller.Destroy();
}
_rvFloorAdControllers.Clear();
// 开始请求楼层广告
RequestNextFloorBatch();
}
private void RequestNextFloorBatch()
{
if (!_isRequestingFloors || _successfulFloor != null) return;
// 获取下一批要请求的楼层
_currentRequestBatch = new List<FloorConfig>();
int count = 0;
while (_currentFloorIndex < _sortedFloors.Count && count < _rvParallelRequests)
{
_currentRequestBatch.Add(_sortedFloors[_currentFloorIndex]);
_currentFloorIndex++;
count++;
}
if (_currentRequestBatch.Count == 0)
{
// 所有楼层都请求完毕,没有填充
LoggerUtils.Debug("All floors requested, no fill");
_isRequestingFloors = false;
return;
}
LoggerUtils.Debug($"Requesting floor batch: {string.Join(", ", _currentRequestBatch.Select(f => $"{f.id}({f.price})"))}");
// 并行请求当前批次的楼层
foreach (var floor in _currentRequestBatch)
{
RequestFloorAd(floor);
}
}
private void RequestFloorAd(FloorConfig floor)
{
IRewardAdController controller = KwaiAds.Scripts.Api.KwaiAdsSdk.SDK.getRewardAdController();
_rvFloorAdControllers[floor.id] = controller;
KwaiRewardAdRequest kwaiRewardAdRequest = new KwaiRewardAdRequest(floor.unite_id);
controller.Load(kwaiRewardAdRequest,
new FloorRewardAdListener(this, floor),
new FloorRewardAdLoadListener(this, floor));
AdsActionEvents.TrackAdStartLoad(KwaiAdsManager.Instance.Platfrom, floor.id, floor.unite_id, AdsType.Rewarded);
}
// 处理楼层广告加载成功
public void OnFloorAdLoaded(FloorConfig floor, IRewardAdController controller)
{
if (!_isRequestingFloors || _successfulFloor != null) return;
LoggerUtils.Debug($"Floor ad loaded: {floor.id} with price: {floor.price}");
// 暂停其他并行请求
_successfulFloor = floor;
_isRequestingFloors = false;
// 取消其他楼层的请求
foreach (var kvp in _rvFloorAdControllers)
{
if (kvp.Key != floor.id)
{
kvp.Value.Destroy();
}
}
}
// 处理楼层广告加载失败
public void OnFloorAdFailed(FloorConfig floor, string error)
{
if (!_isRequestingFloors || _successfulFloor != null) return;
LoggerUtils.Debug($"Floor ad failed: {floor.id} with error: {error}");
// 检查当前批次是否全部失败
bool allFailedInBatch = true;
foreach (var f in _currentRequestBatch)
{
if (f.id == floor.id) continue;
if (_rvFloorAdControllers.ContainsKey(f.id) &&
_rvFloorAdControllers[f.id] != null &&
_rvFloorAdControllers[f.id].IsReady())
{
allFailedInBatch = false;
break;
}
}
// 如果当前批次全部失败,请求下一批
if (allFailedInBatch)
{
RequestNextFloorBatch();
}
}
public bool IsRewardedAvailable()
{
return _successfulFloor != null &&
_rvFloorAdControllers.ContainsKey(_successfulFloor.id) &&
_rvFloorAdControllers[_successfulFloor.id] != null &&
_rvFloorAdControllers[_successfulFloor.id].IsReady();
}
public void ShowRewarded(Action _action)
{
if (_successfulFloor != null &&
_rvFloorAdControllers.ContainsKey(_successfulFloor.id) &&
_rvFloorAdControllers[_successfulFloor.id] != null)
{
_rvFloorAdControllers[_successfulFloor.id].Show();
}
else
{
_action?.Invoke();
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 23078a0a599c742cf9f3f4dccc9b4a83
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e7ee96560784941fe83a70b743bd2b41
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9415a725b0d844289820c0e842c56952
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5fa25ee61c10f462eb5d5fb7ab1ba0d7
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 99cfe15d65a714f84a84176b04c4de0f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,50 @@
using System.Collections;
using System.Collections.Generic;
using KwaiAds.Scripts.Api.Reward;
using UnityEngine;
using static WZ.KwaiAdsConfigParser;
namespace WZ
{
public class FloorRewardAdListener : IRewardAdListener
{
private KwaiFloorAdsManager _manager;
private FloorConfig _floor;
public FloorRewardAdListener(KwaiFloorAdsManager manager, FloorConfig floor)
{
_manager = manager;
_floor = floor;
}
public void OnAdClick()
{
}
public void OnAdClose()
{
}
public void OnAdPlayComplete()
{
}
public void OnAdShow()
{
}
public void OnAdShowFailed(int code, string msg)
{
}
public void OnRewardEarned()
{
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 00750012be89d44c3a4b3a8211231b5f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,35 @@
using System.Collections;
using System.Collections.Generic;
using KwaiAds.Scripts.Api.Reward;
using UnityEngine;
using static WZ.KwaiAdsConfigParser;
namespace WZ
{
public class FloorRewardAdLoadListener : IRewardAdLoadListener
{
private KwaiFloorAdsManager _manager;
private FloorConfig _floor;
public FloorRewardAdLoadListener(KwaiFloorAdsManager manager, FloorConfig floor)
{
_manager = manager;
_floor = floor;
}
public void OnAdLoadFailed(string trackId, int code, string msg)
{
_manager.OnFloorAdFailed(_floor, msg);
}
public void OnAdLoadStart(string trackId)
{
}
public void OnAdLoadSuccess(string trackId, string price)
{
_manager.OnFloorAdLoaded(_floor, _manager._rvFloorAdControllers[_floor.id]);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 97e7949432bd84517b1f6580218ffb94
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: c1eaf30ce69a744fea7ce85741acf3bc
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: