This commit is contained in:
Savya Bikram Shah
2026-06-01 16:52:00 +05:45
parent 89011184d5
commit 705d6b4825
4 changed files with 764 additions and 126 deletions

View File

@@ -55,20 +55,20 @@ public class ColorbookFlowController : IAsyncStartable, IDisposable
await _drawingCatalog.InitializeAsync(cancellation);
if (!_navigatingToGameplay) _loadingScreen.Hide();
PrewarmRewardedAdAsync(_scopeCts.Token).Forget();
PrewarmInterstitialAdAsync(_scopeCts.Token).Forget();
}
private async UniTaskVoid PrewarmRewardedAdAsync(CancellationToken ct)
private async UniTaskVoid PrewarmInterstitialAdAsync(CancellationToken ct)
{
try
{
if (!_ads.IsInitialized) await _ads.InitializeAsync(ct);
if (!_ads.IsReady(AdFormat.Rewarded)) await _ads.LoadAsync(AdFormat.Rewarded, ct);
if (!_ads.IsReady(AdFormat.Interstitial)) await _ads.LoadAsync(AdFormat.Interstitial, ct);
}
catch (OperationCanceledException) { }
catch (Exception ex)
{
UnityEngine.Debug.LogWarning($"[ColorbookFlow] Rewarded prewarm failed: {ex.Message}");
UnityEngine.Debug.LogWarning($"[ColorbookFlow] Interstitial prewarm failed: {ex.Message}");
}
}
@@ -101,40 +101,51 @@ public class ColorbookFlowController : IAsyncStartable, IDisposable
_loadingScreen.Show();
_loadingScreen.SetProgress(0f);
await ShowRewardedAdAsync(ct);
// Fire the interstitial but never await it: the ad overlays the transition while the level
// loads underneath, so a missed/dropped ad callback can't stall the flow at 0% anymore.
ShowInterstitialAdAsync(ct).Forget();
// Ad SDKs can resume the continuation on a background thread; the scene load below
// must run on the Unity main thread. No-op when already there.
await UniTask.SwitchToMainThread(ct);
var progress = new Progress<float>(p => _loadingScreen.SetProgress(p * 0.5f));
var mappedProgress = new Progress<float>(p => _loadingScreen.SetProgress(0.5f + p * 0.25f));
await _progression.SetLastOpenedAsync(templateId);
await _scenes.LoadSceneAsync(nameof(GameScene.Gameplay), progress: progress, cancellationToken: default);
await _scenes.UnloadSceneAsync(nameof(GameScene.Colorbook), progress: mappedProgress,
cancellationToken: default);
}
private async UniTask ShowRewardedAdAsync(CancellationToken ct)
{
const int InitTimeoutMs = 4000;
try
{
if (!_ads.IsInitialized)
var progress = new Progress<float>(p => _loadingScreen.SetProgress(p * 0.5f));
var mappedProgress = new Progress<float>(p => _loadingScreen.SetProgress(0.5f + p * 0.25f));
await _progression.SetLastOpenedAsync(templateId);
await _scenes.LoadSceneAsync(nameof(GameScene.Gameplay), progress: progress, cancellationToken: default);
await _scenes.UnloadSceneAsync(nameof(GameScene.Colorbook), progress: mappedProgress,
cancellationToken: default);
}
catch (OperationCanceledException) { /* scope disposed */ }
catch (Exception ex)
{
// Navigation failed mid-flight: release the latch and drop the loading screen so the
// user can retry instead of being stuck on a loader frozen at 0%.
UnityEngine.Debug.LogException(ex);
_navigatingToGameplay = false;
_loadingScreen.Hide();
}
}
// Fire-and-forget interstitial. Shows only if one is already prewarmed; otherwise it kicks a
// load for next time and returns immediately. Never blocks the level load — by design the
// scene swap below does not depend on the ad's close callback, so the ad can never stall it.
private async UniTaskVoid ShowInterstitialAdAsync(CancellationToken ct)
{
try
{
if (!_ads.IsInitialized) return;
if (!_ads.IsReady(AdFormat.Interstitial))
{
using var timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(ct);
timeoutCts.CancelAfter(InitTimeoutMs);
await _ads.InitializeAsync(timeoutCts.Token);
_ads.LoadAsync(AdFormat.Interstitial, ct).Forget();
return;
}
if (!_ads.IsReady(AdFormat.Rewarded)) return;
await _ads.ShowAsync(AdFormat.Rewarded, ct);
await _ads.ShowAsync(AdFormat.Interstitial, ct);
}
catch (OperationCanceledException) { }
catch (Exception ex)
{
UnityEngine.Debug.LogWarning($"[ColorbookFlow] Rewarded ad skipped: {ex.Message}");
UnityEngine.Debug.LogWarning($"[ColorbookFlow] Interstitial skipped: {ex.Message}");
}
}