GameScreenView.Ads.cs 2.5 KB

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