| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- using System;
- using UnityEngine;
- using UnityEngine.UI;
- namespace Fateforge
- {
- public sealed class RewardedAdTestScreen : MonoBehaviour
- {
- [SerializeField] Text environmentLabel;
- [SerializeField] Text statusLabel;
- [SerializeField] Text detailLabel;
- [SerializeField] Text rewardLabel;
- [SerializeField] Text resultLabel;
- [SerializeField] Button playButton;
- [SerializeField] Button completeButton;
- [SerializeField] Button closeButton;
- IRewardedAdService ads;
- AdRequest pending;
- bool earned;
- int rewardCount;
- float nextPoll;
- void Start()
- {
- #if UNITY_EDITOR
- var simulator = new SimulatedAdService();
- ads = simulator;
- environmentLabel.text = "Unity Editor / Simulation";
- completeButton.onClick.AddListener(() => simulator.Complete(AdResult.Rewarded));
- closeButton.onClick.AddListener(() => simulator.Complete(AdResult.Closed));
- #elif UNITY_IOS
- ads = new IosRewardedAds(MobileConfig.Load());
- environmentLabel.text = "iOS device / AdMob rewarded";
- #else
- environmentLabel.text = "Open this scene in Unity or on iOS.";
- #endif
- if (ads != null) ads.Result += OnAdResult;
- playButton.onClick.AddListener(ShowAd);
- RefreshScreen();
- }
- void Update()
- {
- if (Time.unscaledTime < nextPoll) return;
- nextPoll = Time.unscaledTime + 0.5f;
- #if UNITY_IOS && !UNITY_EDITOR
- ((IosRewardedAds)ads).Poll();
- #endif
- RefreshScreen();
- }
- void ShowAd()
- {
- if (pending != null || ads?.Available != true) return;
- pending = new AdRequest { requestId = Guid.NewGuid().ToString("N") };
- earned = false;
- resultLabel.text = "Waiting for the reward callback.";
- if (!ads.Show(pending))
- {
- pending = null;
- resultLabel.text = "The ad could not open. Try again when ready.";
- }
- RefreshScreen();
- }
- void OnAdResult(AdRequest request, AdResult result)
- {
- if (pending == null || request != pending) return;
- if (result == AdResult.Rewarded)
- {
- if (earned) return;
- earned = true;
- rewardCount++;
- resultLabel.text = "Reward received.";
- }
- else
- {
- pending = null;
- resultLabel.text = result == AdResult.Closed
- ? (earned ? "Ad closed. Reward received." : "Ad closed before reward.")
- : "Ad presentation failed.";
- }
- RefreshScreen();
- }
- void RefreshScreen()
- {
- bool ready = pending == null && ads?.Available == true;
- statusLabel.text = ads == null ? "Unavailable" : pending != null ? "Showing ad" : ready ? "Ready" : "Loading";
- playButton.interactable = ready;
- rewardLabel.text = $"Rewards this session {rewardCount}";
- bool simulate = false;
- #if UNITY_EDITOR
- simulate = pending != null;
- #endif
- completeButton.gameObject.SetActive(simulate);
- closeButton.gameObject.SetActive(simulate);
- #if UNITY_IOS && !UNITY_EDITOR
- string error = ((IosRewardedAds)ads).LastError;
- detailLabel.text = error ?? (ready ? "Tap below to play the rewarded ad." :
- pending != null ? "Watch until the reward is earned." : "AdMob will load and retry automatically.");
- #else
- detailLabel.text = simulate ? "Choose how the simulated ad ends." :
- ready ? "Tap below to start a simulated ad." : "";
- #endif
- }
- void OnDestroy()
- {
- if (ads != null) ads.Result -= OnAdResult;
- (ads as IDisposable)?.Dispose();
- }
- }
- }
|