GameScreenView.Ads.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using UnityEngine;
  2. public partial class GameScreenView
  3. {
  4. PopViewScreenView popView;
  5. bool adPlaying;
  6. bool revivedThisLevel;
  7. string pendingFailTitle;
  8. string pendingFailSubtitle;
  9. public bool IsAdPlaying => adPlaying;
  10. void BuildPopView()
  11. {
  12. GameObject root = InstantiatePanel("Prefab/12_popview", "AdPopView");
  13. if (root == null)
  14. return;
  15. popView = root.GetComponent<PopViewScreenView>();
  16. if (popView == null)
  17. popView = root.AddComponent<PopViewScreenView>();
  18. }
  19. bool IsPopViewVisible()
  20. {
  21. return popView != null && popView.gameObject.activeSelf;
  22. }
  23. bool CanOfferAdRevive()
  24. {
  25. if (revivedThisLevel)
  26. return false;
  27. return SaveService.CanAdReviveToday();
  28. }
  29. void OfferAdRevive(string title, string subtitle)
  30. {
  31. if (popView == null)
  32. {
  33. ShowResult(false, title, subtitle);
  34. return;
  35. }
  36. pendingFailTitle = title;
  37. pendingFailSubtitle = subtitle;
  38. var content = new PopViewContent
  39. {
  40. Title = L.Get("pop.out_of_moves"),
  41. Badge = L.F("pop.ad_moves_badge", GameDefinitions.AdReviveMoveBonus),
  42. Body = L.Get("pop.ad_body"),
  43. ConfirmText = L.Get("pop.watch_ad"),
  44. CancelText = L.Get("pop.end_run")
  45. };
  46. popView.Show(content, OnAdReviveConfirmed, OnAdReviveDeclined);
  47. }
  48. void OnAdReviveDeclined()
  49. {
  50. ShowResult(false, pendingFailTitle, pendingFailSubtitle);
  51. }
  52. void OnAdReviveConfirmed()
  53. {
  54. adPlaying = true;
  55. AdMobService.ShowRewarded(
  56. AdPlacement.Revive,
  57. CompleteAdRevive,
  58. HandleAdReviveFailed);
  59. }
  60. void HandleAdReviveFailed(string reason)
  61. {
  62. adPlaying = false;
  63. Debug.LogWarning("[GameScreen] Ad revive failed: " + reason);
  64. ShowBanner(L.Get("pop.ad_not_ready"), 1.2f, 22f);
  65. OfferAdRevive(pendingFailTitle, pendingFailSubtitle);
  66. }
  67. void CompleteAdRevive()
  68. {
  69. adPlaying = false;
  70. revivedThisLevel = true;
  71. ended = false;
  72. busy = false;
  73. SaveService.RecordAdRevive();
  74. state.AddMoves(GameDefinitions.AdReviveMoveBonus);
  75. RefreshHud();
  76. GameAudio.PlaySfx(GameSfx.Buy);
  77. ShowBanner(L.F("pop.ad_moves_badge", GameDefinitions.AdReviveMoveBonus), 1.2f, 30f);
  78. }
  79. }