RewardedAdTestScreen.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace Fateforge
  5. {
  6. public sealed class RewardedAdTestScreen : MonoBehaviour
  7. {
  8. [SerializeField] Text environmentLabel;
  9. [SerializeField] Text statusLabel;
  10. [SerializeField] Text detailLabel;
  11. [SerializeField] Text rewardLabel;
  12. [SerializeField] Text resultLabel;
  13. [SerializeField] Button playButton;
  14. [SerializeField] Button completeButton;
  15. [SerializeField] Button closeButton;
  16. IRewardedAdService ads;
  17. AdRequest pending;
  18. bool earned;
  19. int rewardCount;
  20. float nextPoll;
  21. void Start()
  22. {
  23. #if UNITY_EDITOR
  24. var simulator = new SimulatedAdService();
  25. ads = simulator;
  26. environmentLabel.text = "Unity Editor / Simulation";
  27. completeButton.onClick.AddListener(() => simulator.Complete(AdResult.Rewarded));
  28. closeButton.onClick.AddListener(() => simulator.Complete(AdResult.Closed));
  29. #elif UNITY_IOS
  30. ads = new IosRewardedAds(MobileConfig.Load());
  31. environmentLabel.text = "iOS device / AdMob rewarded";
  32. #else
  33. environmentLabel.text = "Open this scene in Unity or on iOS.";
  34. #endif
  35. if (ads != null) ads.Result += OnAdResult;
  36. playButton.onClick.AddListener(ShowAd);
  37. RefreshScreen();
  38. }
  39. void Update()
  40. {
  41. if (Time.unscaledTime < nextPoll) return;
  42. nextPoll = Time.unscaledTime + 0.5f;
  43. #if UNITY_IOS && !UNITY_EDITOR
  44. ((IosRewardedAds)ads).Poll();
  45. #endif
  46. RefreshScreen();
  47. }
  48. void ShowAd()
  49. {
  50. if (pending != null || ads?.Available != true) return;
  51. pending = new AdRequest { requestId = Guid.NewGuid().ToString("N") };
  52. earned = false;
  53. resultLabel.text = "Waiting for the reward callback.";
  54. if (!ads.Show(pending))
  55. {
  56. pending = null;
  57. resultLabel.text = "The ad could not open. Try again when ready.";
  58. }
  59. RefreshScreen();
  60. }
  61. void OnAdResult(AdRequest request, AdResult result)
  62. {
  63. if (pending == null || request != pending) return;
  64. if (result == AdResult.Rewarded)
  65. {
  66. if (earned) return;
  67. earned = true;
  68. rewardCount++;
  69. resultLabel.text = "Reward received.";
  70. }
  71. else
  72. {
  73. pending = null;
  74. resultLabel.text = result == AdResult.Closed
  75. ? (earned ? "Ad closed. Reward received." : "Ad closed before reward.")
  76. : "Ad presentation failed.";
  77. }
  78. RefreshScreen();
  79. }
  80. void RefreshScreen()
  81. {
  82. bool ready = pending == null && ads?.Available == true;
  83. statusLabel.text = ads == null ? "Unavailable" : pending != null ? "Showing ad" : ready ? "Ready" : "Loading";
  84. playButton.interactable = ready;
  85. rewardLabel.text = $"Rewards this session {rewardCount}";
  86. bool simulate = false;
  87. #if UNITY_EDITOR
  88. simulate = pending != null;
  89. #endif
  90. completeButton.gameObject.SetActive(simulate);
  91. closeButton.gameObject.SetActive(simulate);
  92. #if UNITY_IOS && !UNITY_EDITOR
  93. string error = ((IosRewardedAds)ads).LastError;
  94. detailLabel.text = error ?? (ready ? "Tap below to play the rewarded ad." :
  95. pending != null ? "Watch until the reward is earned." : "AdMob will load and retry automatically.");
  96. #else
  97. detailLabel.text = simulate ? "Choose how the simulated ad ends." :
  98. ready ? "Tap below to start a simulated ad." : "";
  99. #endif
  100. }
  101. void OnDestroy()
  102. {
  103. if (ads != null) ads.Result -= OnAdResult;
  104. (ads as IDisposable)?.Dispose();
  105. }
  106. }
  107. }