Files
Commercialization.topon/Topon_Adapter/Runtime/Scripts/AwardVideoPlayer.cs

236 lines
6.6 KiB
C#
Raw Normal View History

2026-04-22 20:59:37 +08:00
using System;
2023-01-29 12:44:57 +08:00
using System.Collections.Generic;
using AnyThinkAds.Api;
2026-04-22 20:59:37 +08:00
using AnyThinkAds.Common;
using AnyThinkAds.ThirdParty.LitJson;
2023-01-29 12:44:57 +08:00
using Runtime.ADAggregator;
using UnityEngine;
2026-04-22 20:59:37 +08:00
public class AwardVideoPlayer : ADPlayer, ATRewardedVideoListener
2023-01-29 12:44:57 +08:00
{
2026-04-22 20:59:37 +08:00
private IATRewardedVideoAdClient _client;
private Action<bool> _onVideoComplete;
2023-01-29 12:44:57 +08:00
private ADListenerAggregator _aggregator;
2026-04-22 20:59:37 +08:00
private bool _autoLoadRegistered;
2023-01-29 12:44:57 +08:00
public override void OnInit()
{
2026-04-22 20:59:37 +08:00
_client = AnyThinkAds.ATAdsClientFactory.BuildRewardedVideoAdClient();
_aggregator = new ADListenerAggregator();
_aggregator.BindAwardVideoListener(_client, this);
2023-01-29 12:44:57 +08:00
}
public override void ShowAD(Action onClose, Action<bool> onVideoComplete)
{
2026-04-22 20:59:37 +08:00
if (!IsReadly())
2023-01-29 12:44:57 +08:00
{
2026-04-22 20:59:37 +08:00
return;
2023-01-29 12:44:57 +08:00
}
2026-04-22 20:59:37 +08:00
curState = 0;
_onVideoComplete = onVideoComplete;
adListener.onClose = onClose;
adListener.onVideoComplete = OnVideoComplete;
var json = new Dictionary<string, string> { { ATConst.SCENARIO, AdScene } };
var mapJson = JsonMapper.ToJson(json);
if (UseAutoLoad())
{
EnsureAutoLoadRegistered();
_client.showAutoAd(Key, mapJson);
return;
}
_client.showAd(Key, mapJson);
2023-01-29 12:44:57 +08:00
}
2026-04-22 20:59:37 +08:00
2023-01-29 12:44:57 +08:00
private void OnVideoComplete(bool obj)
{
2026-04-22 20:59:37 +08:00
_onVideoComplete?.Invoke(obj);
_onVideoComplete = null;
2023-01-29 12:44:57 +08:00
}
public override bool IsReadly()
{
2026-04-22 17:44:49 +08:00
if (UseAutoLoad())
{
2026-04-22 20:59:37 +08:00
if (_client != null && _client.autoLoadRewardedVideoReadyForPlacementID(Key))
2026-04-22 17:44:49 +08:00
{
2026-04-22 20:59:37 +08:00
curState = 2;
2026-04-22 17:44:49 +08:00
return true;
}
2026-04-22 20:59:37 +08:00
return curState == 2;
2026-04-22 17:44:49 +08:00
}
2026-04-22 20:59:37 +08:00
if (curState == 2)
2026-04-22 17:44:49 +08:00
{
return true;
}
2026-04-22 20:59:37 +08:00
if (_client != null && _client.hasAdReady(Key))
2026-04-22 17:44:49 +08:00
{
2026-04-22 20:59:37 +08:00
curState = 2;
2026-04-22 17:44:49 +08:00
return true;
}
return false;
2023-01-29 12:44:57 +08:00
}
public override void LoadAD()
{
2026-04-22 20:59:37 +08:00
if (curState != 0)
2023-01-29 12:44:57 +08:00
{
2026-04-22 20:59:37 +08:00
return;
}
2026-04-22 17:44:49 +08:00
2026-04-22 20:59:37 +08:00
if (UseAutoLoad())
{
EnsureAutoLoadRegistered();
_client.setAutoLocalExtra(Key, JsonMapper.ToJson(BuildRewardedExtra()));
curState = _client.autoLoadRewardedVideoReadyForPlacementID(Key) ? 2 : 1;
return;
}
2026-04-22 17:44:49 +08:00
2026-04-22 20:59:37 +08:00
if (_client != null && _client.hasAdReady(Key))
{
curState = 2;
return;
2023-01-29 12:44:57 +08:00
}
2026-04-22 20:59:37 +08:00
curState = 1;
_client.loadVideoAd(Key, JsonMapper.ToJson(BuildRewardedExtra()));
}
2023-01-29 12:44:57 +08:00
public void onRewardedVideoAdLoaded(string placementId)
{
2026-04-22 20:59:37 +08:00
Debug.Log($"[Topon] Rewarded loaded. placementId={placementId}");
curState = 2;
2023-01-29 12:44:57 +08:00
}
public void onRewardedVideoAdLoadFail(string placementId, string code, string message)
{
Debug.LogError($"激励视频加载失败: {message} , code:{code} , placementId: {placementId}");
2026-04-22 20:59:37 +08:00
curState = 0;
2023-01-29 12:44:57 +08:00
}
public void onRewardedVideoAdPlayStart(string placementId, ATCallbackInfo callbackInfo)
{
2026-04-22 20:59:37 +08:00
Debug.Log($"[Topon] Rewarded play start. placementId={placementId}");
2023-01-29 12:44:57 +08:00
}
public void onRewardedVideoAdPlayEnd(string placementId, ATCallbackInfo callbackInfo)
{
2026-04-22 20:59:37 +08:00
Debug.Log($"[Topon] Rewarded play end. placementId={placementId}");
2023-01-29 12:44:57 +08:00
}
public void onRewardedVideoAdPlayFail(string placementId, string code, string message)
{
Debug.LogError($"激励视频播放失败: {message} , code:{code} , placementId: {placementId}");
curState = 0;
2026-04-22 20:59:37 +08:00
adListener.OnShowError();
2023-01-29 12:44:57 +08:00
}
public void onRewardedVideoAdPlayClosed(string placementId, bool isReward, ATCallbackInfo callbackInfo)
{
2026-04-22 20:59:37 +08:00
Debug.Log($"[Topon] Rewarded closed. placementId={placementId}, reward={isReward}");
adListener.OnRewardVerify(isReward, 1, "");
adListener.OnAdClose();
2023-01-29 12:44:57 +08:00
}
public void onRewardedVideoAdPlayClicked(string placementId, ATCallbackInfo callbackInfo)
{
2026-04-22 20:59:37 +08:00
Debug.Log($"[Topon] Rewarded clicked. placementId={placementId}");
2023-01-29 12:44:57 +08:00
}
public void onReward(string placementId, ATCallbackInfo callbackInfo)
{
2026-04-22 20:59:37 +08:00
Debug.Log($"[Topon] Rewarded reward callback. placementId={placementId}");
adListener.OnRewardVerify(true, 1, "");
adListener.OnAdClose();
2023-01-29 12:44:57 +08:00
}
public void startLoadingADSource(string placementId, ATCallbackInfo callbackInfo)
{
}
public void finishLoadingADSource(string placementId, ATCallbackInfo callbackInfo)
{
}
public void failToLoadADSource(string placementId, ATCallbackInfo callbackInfo, string code, string message)
{
2026-04-22 20:59:37 +08:00
OnError(code, message);
2023-01-29 12:44:57 +08:00
}
public void startBiddingADSource(string placementId, ATCallbackInfo callbackInfo)
{
}
public void finishBiddingADSource(string placementId, ATCallbackInfo callbackInfo)
{
}
public void failBiddingADSource(string placementId, ATCallbackInfo callbackInfo, string code, string message)
{
}
2026-04-22 17:44:49 +08:00
public override void OnPlayRequestStarted()
{
if (UseAutoLoad())
{
EnsureAutoLoadRegistered();
2026-04-22 20:59:37 +08:00
_client?.setAutoLocalExtra(Key, JsonMapper.ToJson(BuildRewardedExtra()));
2026-04-22 17:44:49 +08:00
}
}
public override int MaxLoadAttempts => ToponAdController.CurrentOptions?.RewardedMaxLoadAttempts ?? base.MaxLoadAttempts;
public override float LoadRetryDelaySeconds =>
Math.Max(0f, (ToponAdController.CurrentOptions?.RewardedLoadRetryDelayMs ?? 750) / 1000f);
public override bool AutoPreloadOnInit => ToponAdController.CurrentOptions?.RewardedPrewarmOnInit ?? true;
private bool UseAutoLoad()
{
return ToponAdController.CurrentOptions?.RewardedAutoLoad ?? true;
}
private void EnsureAutoLoadRegistered()
{
if (_autoLoadRegistered)
{
return;
}
2026-04-22 20:59:37 +08:00
_client?.addAutoLoadAdPlacementID(new[] { Key });
2026-04-22 17:44:49 +08:00
_autoLoadRegistered = true;
}
private Dictionary<string, string> BuildRewardedExtra()
{
return new Dictionary<string, string>
{
{ ATConst.USERID_KEY, ADManager.Instance.UserId },
{ ATConst.USER_EXTRA_DATA, "user_extra_data" }
};
}
public override void EnterAdScenario(string scenario)
{
if (string.IsNullOrWhiteSpace(scenario) || string.Equals(scenario, "__default__", StringComparison.Ordinal))
{
return;
}
if (UseAutoLoad())
{
EnsureAutoLoadRegistered();
2026-04-22 20:59:37 +08:00
_client?.entryAutoAdScenarioWithPlacementID(Key, scenario);
2026-04-22 17:44:49 +08:00
return;
}
2026-04-22 20:59:37 +08:00
_client?.entryScenarioWithPlacementID(Key, scenario);
2026-04-22 17:44:49 +08:00
}
}