From 02cb90c1c034d34807fa2d3f78766d01ad4bf1ce Mon Sep 17 00:00:00 2001 From: "CORE-FOLDCC\\Core" <1813547935@qq.com> Date: Thu, 4 Jun 2026 17:24:54 +0800 Subject: [PATCH] Fix TapADN ad lifecycle callbacks --- .../Runtime/DirichletAdTypes.cs | 17 ++++- .../Runtime/Scripts/TapadnAwardVideoPlayer.cs | 65 +++++++++++++++++-- .../Scripts/TapadnInteractionPlayer.cs | 22 ++++++- .../Runtime/Scripts/TapadnSplashPlayer.cs | 22 ++++++- 4 files changed, 119 insertions(+), 7 deletions(-) diff --git a/Assets/DirichletMediation/Runtime/DirichletAdTypes.cs b/Assets/DirichletMediation/Runtime/DirichletAdTypes.cs index 7dbfcd4..96a2462 100644 --- a/Assets/DirichletMediation/Runtime/DirichletAdTypes.cs +++ b/Assets/DirichletMediation/Runtime/DirichletAdTypes.cs @@ -752,6 +752,11 @@ namespace Dirichlet.Mediation /// public event Action Closed; + /// + /// Raised when the native layer reports that a loaded ad failed to show. + /// + public event Action ShowFailed; + internal DirichletAd(IDirichletPlatformBridge bridge, DirichletPlatformAdHandle platformHandle, long spaceId) { this.bridge = bridge ?? throw new ArgumentNullException(nameof(bridge)); @@ -812,6 +817,9 @@ namespace Dirichlet.Mediation case DirichletNativeEventNames.Close: OnClosed(); break; + case DirichletNativeEventNames.ShowError: + OnShowFailed(nativeEvent.Data); + break; } } @@ -829,6 +837,13 @@ namespace Dirichlet.Mediation { Closed?.Invoke(); } + + protected virtual void OnShowFailed(DirichletNativeEventData data) + { + var code = data != null && data.Code != 0 ? data.Code.ToString(CultureInfo.InvariantCulture) : "show_error"; + var message = data?.Message; + ShowFailed?.Invoke(new DirichletError(code, string.IsNullOrEmpty(message) ? "Ad failed to show" : message)); + } } public abstract class DirichletRewardAdBase : DirichletAd @@ -1085,6 +1100,7 @@ namespace Dirichlet.Mediation internal const string Click = "click"; internal const string Close = "close"; internal const string Reward = "reward"; + internal const string ShowError = "show_error"; } internal readonly struct DirichletNativeEvent @@ -1373,4 +1389,3 @@ namespace Dirichlet.Mediation } } - diff --git a/Assets/Tapadn_Adapter/Runtime/Scripts/TapadnAwardVideoPlayer.cs b/Assets/Tapadn_Adapter/Runtime/Scripts/TapadnAwardVideoPlayer.cs index 1bc9a83..4bb80c5 100644 --- a/Assets/Tapadn_Adapter/Runtime/Scripts/TapadnAwardVideoPlayer.cs +++ b/Assets/Tapadn_Adapter/Runtime/Scripts/TapadnAwardVideoPlayer.cs @@ -5,9 +5,15 @@ using UnityEngine; public sealed class TapadnAwardVideoPlayer : ADPlayer, IDirichletRewardVideoAutoAdListener { + private const float RewardCloseSettleDelaySeconds = 0.25f; + private DirichletAdNative _adNative; private DirichletRewardVideoAd _loadedAd; private bool _rewardVerified; + private bool _rewardVerifyReceived; + private bool _closePendingRewardVerify; + private bool _showSettled; + private AdTimeHandler _rewardCloseSettleHandler; public override int MaxLoadAttempts => TapadnAdController.CurrentOptions?.RewardedMaxLoadAttempts ?? base.MaxLoadAttempts; public override float LoadRetryDelaySeconds => Math.Max(0f, (TapadnAdController.CurrentOptions?.RewardedLoadRetryDelayMs ?? 500) / 1000f); @@ -72,6 +78,7 @@ public sealed class TapadnAwardVideoPlayer : ADPlayer, IDirichletRewardVideoAuto _loadedAd = ad; _loadedAd.Shown += OnManualShown; _loadedAd.Clicked += OnManualClicked; + _loadedAd.ShowFailed += OnManualShowFailed; _loadedAd.RewardVerified += OnManualRewardVerify; _loadedAd.Closed += OnManualClosed; curState = 2; @@ -89,6 +96,11 @@ public sealed class TapadnAwardVideoPlayer : ADPlayer, IDirichletRewardVideoAuto adListener.onClose = onClose; adListener.onVideoComplete = onVideoComplete; _rewardVerified = false; + _rewardVerifyReceived = false; + _closePendingRewardVerify = false; + _showSettled = false; + _rewardCloseSettleHandler?.Kill(); + _rewardCloseSettleHandler = null; curState = 0; if (UseAutoLoad()) @@ -99,13 +111,21 @@ public sealed class TapadnAwardVideoPlayer : ADPlayer, IDirichletRewardVideoAuto if (_loadedAd == null || !_loadedAd.Show()) { - adListener.OnShowError(); + OnError(new DirichletError("show_failed", "ShowRewardVideoAd returned false")); return; } } public void OnError(DirichletError error) { + if (_showSettled) + { + return; + } + + _showSettled = true; + _rewardCloseSettleHandler?.Kill(); + _rewardCloseSettleHandler = null; curState = 0; Debug.LogError($"[TapADN] Rewarded show failed. code={error?.Code}, message={error?.Message}"); adListener.OnShowError(); @@ -118,13 +138,30 @@ public sealed class TapadnAwardVideoPlayer : ADPlayer, IDirichletRewardVideoAuto public void OnAdClose() { - adListener.OnRewardVerify(_rewardVerified, TapadnAdController.CurrentOptions?.RewardAmount ?? 1, TapadnAdController.CurrentOptions?.RewardName ?? string.Empty); - adListener.OnAdClose(); + if (_showSettled) + { + return; + } + + if (_rewardVerifyReceived) + { + CompleteRewardedClose(); + return; + } + + _closePendingRewardVerify = true; + _rewardCloseSettleHandler?.Kill(); + _rewardCloseSettleHandler = ADManager.Instance.CreateTimer(RewardCloseSettleDelaySeconds, CompleteRewardedClose); } public void OnRewardVerify(DirichletRewardVerificationEventArgs args) { + _rewardVerifyReceived = true; _rewardVerified = args != null && args.IsVerified; + if (_closePendingRewardVerify) + { + CompleteRewardedClose(); + } } public void OnAdClick() @@ -145,9 +182,14 @@ public sealed class TapadnAwardVideoPlayer : ADPlayer, IDirichletRewardVideoAuto { } + private void OnManualShowFailed(DirichletError error) + { + OnError(error); + } + private void OnManualRewardVerify(DirichletRewardVerificationEventArgs args) { - _rewardVerified = args != null && args.IsVerified; + OnRewardVerify(args); } private void OnManualClosed() @@ -156,4 +198,19 @@ public sealed class TapadnAwardVideoPlayer : ADPlayer, IDirichletRewardVideoAuto _loadedAd = null; OnAdClose(); } + + private void CompleteRewardedClose() + { + if (_showSettled) + { + return; + } + + _showSettled = true; + _closePendingRewardVerify = false; + _rewardCloseSettleHandler?.Kill(); + _rewardCloseSettleHandler = null; + adListener.OnRewardVerify(_rewardVerified, TapadnAdController.CurrentOptions?.RewardAmount ?? 1, TapadnAdController.CurrentOptions?.RewardName ?? string.Empty); + adListener.OnAdClose(); + } } diff --git a/Assets/Tapadn_Adapter/Runtime/Scripts/TapadnInteractionPlayer.cs b/Assets/Tapadn_Adapter/Runtime/Scripts/TapadnInteractionPlayer.cs index f276348..43bec6a 100644 --- a/Assets/Tapadn_Adapter/Runtime/Scripts/TapadnInteractionPlayer.cs +++ b/Assets/Tapadn_Adapter/Runtime/Scripts/TapadnInteractionPlayer.cs @@ -7,6 +7,7 @@ public sealed class TapadnInteractionPlayer : ADPlayer, IDirichletInterstitialAu { private DirichletAdNative _adNative; private DirichletInterstitialAd _loadedAd; + private bool _showSettled; public override int MaxLoadAttempts => TapadnAdController.CurrentOptions?.InterstitialMaxLoadAttempts ?? base.MaxLoadAttempts; public override float LoadRetryDelaySeconds => Math.Max(0f, (TapadnAdController.CurrentOptions?.InterstitialLoadRetryDelayMs ?? 500) / 1000f); @@ -63,6 +64,7 @@ public sealed class TapadnInteractionPlayer : ADPlayer, IDirichletInterstitialAu _loadedAd = ad; _loadedAd.Shown += OnManualShown; _loadedAd.Clicked += OnManualClicked; + _loadedAd.ShowFailed += OnManualShowFailed; _loadedAd.Closed += OnManualClosed; curState = 2; Debug.Log($"[TapADN] Interstitial loaded. slot={Key}"); @@ -78,6 +80,7 @@ public sealed class TapadnInteractionPlayer : ADPlayer, IDirichletInterstitialAu { adListener.onClose = onClose; adListener.onVideoComplete = onVideoComplete; + _showSettled = false; curState = 0; if (UseAutoLoad()) @@ -88,12 +91,18 @@ public sealed class TapadnInteractionPlayer : ADPlayer, IDirichletInterstitialAu if (_loadedAd == null || !_loadedAd.Show()) { - adListener.OnShowError(); + OnError(new DirichletError("show_failed", "ShowInterstitialAd returned false")); } } public void OnError(DirichletError error) { + if (_showSettled) + { + return; + } + + _showSettled = true; curState = 0; Debug.LogError($"[TapADN] Interstitial show failed. code={error?.Code}, message={error?.Message}"); adListener.OnShowError(); @@ -106,6 +115,12 @@ public sealed class TapadnInteractionPlayer : ADPlayer, IDirichletInterstitialAu public void OnAdClose() { + if (_showSettled) + { + return; + } + + _showSettled = true; adListener.OnAdClose(); adListener.OnShowComplete(); } @@ -128,6 +143,11 @@ public sealed class TapadnInteractionPlayer : ADPlayer, IDirichletInterstitialAu { } + private void OnManualShowFailed(DirichletError error) + { + OnError(error); + } + private void OnManualClosed() { _loadedAd?.Destroy(); diff --git a/Assets/Tapadn_Adapter/Runtime/Scripts/TapadnSplashPlayer.cs b/Assets/Tapadn_Adapter/Runtime/Scripts/TapadnSplashPlayer.cs index 4dd9f24..85c3b24 100644 --- a/Assets/Tapadn_Adapter/Runtime/Scripts/TapadnSplashPlayer.cs +++ b/Assets/Tapadn_Adapter/Runtime/Scripts/TapadnSplashPlayer.cs @@ -7,6 +7,7 @@ public sealed class TapadnSplashPlayer : ADPlayer, IDirichletSplashAutoAdListene { private DirichletAdNative _adNative; private DirichletSplashAd _loadedAd; + private bool _showSettled; public override int MaxLoadAttempts => TapadnAdController.CurrentOptions?.SplashMaxLoadAttempts ?? base.MaxLoadAttempts; public override float LoadRetryDelaySeconds => Math.Max(0f, (TapadnAdController.CurrentOptions?.SplashLoadRetryDelayMs ?? 500) / 1000f); @@ -63,6 +64,7 @@ public sealed class TapadnSplashPlayer : ADPlayer, IDirichletSplashAutoAdListene _loadedAd = ad; _loadedAd.Shown += OnManualShown; _loadedAd.Clicked += OnManualClicked; + _loadedAd.ShowFailed += OnManualShowFailed; _loadedAd.Closed += OnManualClosed; curState = 2; Debug.Log($"[TapADN] Splash loaded. slot={Key}"); @@ -78,6 +80,7 @@ public sealed class TapadnSplashPlayer : ADPlayer, IDirichletSplashAutoAdListene { adListener.onClose = onClose; adListener.onVideoComplete = onVideoComplete; + _showSettled = false; curState = 0; if (UseAutoLoad()) @@ -88,12 +91,18 @@ public sealed class TapadnSplashPlayer : ADPlayer, IDirichletSplashAutoAdListene if (_loadedAd == null || !_loadedAd.Show()) { - adListener.OnShowError(); + OnError(new DirichletError("show_failed", "ShowSplashAd returned false")); } } public void OnError(DirichletError error) { + if (_showSettled) + { + return; + } + + _showSettled = true; curState = 0; Debug.LogError($"[TapADN] Splash show failed. code={error?.Code}, message={error?.Message}"); adListener.OnShowError(); @@ -106,6 +115,12 @@ public sealed class TapadnSplashPlayer : ADPlayer, IDirichletSplashAutoAdListene public void OnAdClose() { + if (_showSettled) + { + return; + } + + _showSettled = true; adListener.OnAdClose(); adListener.OnShowComplete(); } @@ -128,6 +143,11 @@ public sealed class TapadnSplashPlayer : ADPlayer, IDirichletSplashAutoAdListene { } + private void OnManualShowFailed(DirichletError error) + { + OnError(error); + } + private void OnManualClosed() { _loadedAd?.Destroy();