Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AdManager.cs 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using GoogleMobileAds.Api;
  5. public class AdManager : MonoBehaviour
  6. {
  7. private static AdManager _instance;
  8. public static AdManager Instance
  9. {
  10. get
  11. {
  12. if (_instance == null)
  13. {
  14. GameObject adManager = new GameObject("AdManager");
  15. _instance = adManager.AddComponent<AdManager>();
  16. DontDestroyOnLoad(adManager);
  17. }
  18. return _instance;
  19. }
  20. }
  21. private InterstitialAd _interstitialAd;
  22. #if UNITY_ANDROID
  23. private string _adUnitId = "ca-app-pub-3570555734266765/3828409575";
  24. #elif UNITY_IPHONE
  25. private string _adUnitId = "ca-app-pub-3570555734266765/4523825408";
  26. #else
  27. private string _adUnitId = "unused";
  28. #endif
  29. void Awake()
  30. {
  31. if (_instance == null)
  32. {
  33. _instance = this;
  34. DontDestroyOnLoad(gameObject);
  35. }
  36. else if (_instance != this)
  37. {
  38. Destroy(gameObject);
  39. return;
  40. }
  41. MobileAds.Initialize(initStatus => { });
  42. LoadInterstitialAd();
  43. }
  44. // Update is called once per frame
  45. public void LoadInterstitialAd()
  46. {
  47. if (_interstitialAd != null)
  48. {
  49. _interstitialAd.Destroy();
  50. _interstitialAd = null;
  51. }
  52. Debug.Log("Loading the interstitial ad.");
  53. var adRequest = new AdRequest();
  54. InterstitialAd.Load(_adUnitId, adRequest, (InterstitialAd ad, LoadAdError error) => {
  55. if (error != null || ad == null)
  56. {
  57. Debug.LogError("Interstitial ad failed to load an ad with error : " + error);
  58. return;
  59. }
  60. Debug.Log("Interstitial ad loaded with response : " + ad.GetResponseInfo());
  61. _interstitialAd = ad;
  62. RegisterEventHandlers(_interstitialAd);
  63. });
  64. }
  65. private void RegisterEventHandlers(InterstitialAd interstitialAd)
  66. {
  67. interstitialAd.OnAdPaid += (AdValue adValue) => {
  68. Debug.Log(string.Format("Interstitial ad paid {0} {1}.", adValue.Value, adValue.CurrencyCode));
  69. };
  70. interstitialAd.OnAdImpressionRecorded += () => {
  71. Debug.Log("Interstitial ad recorded an impression.");
  72. };
  73. interstitialAd.OnAdClicked += () => {
  74. Debug.Log("Interstitial ad was clicked.");
  75. };
  76. interstitialAd.OnAdFullScreenContentOpened += () => {
  77. Debug.Log("Interstitial ad full screen content opened.");
  78. };
  79. interstitialAd.OnAdFullScreenContentClosed += () => {
  80. Debug.Log("Interstitial ad full screen content closed.");
  81. LoadInterstitialAd();
  82. Time.timeScale = 1f;
  83. };
  84. interstitialAd.OnAdFullScreenContentFailed += (AdError error) => {
  85. Debug.LogError("Interstitial ad failed to open full screen content with error : " + error);
  86. LoadInterstitialAd();
  87. };
  88. }
  89. public void ShowInterstitialAd()
  90. {
  91. if (_interstitialAd != null && _interstitialAd.CanShowAd())
  92. {
  93. Debug.Log("Showing interstitial ad.");
  94. _interstitialAd.Show();
  95. }
  96. else
  97. {
  98. Debug.LogError("Interstitial ad is not ready yet.");
  99. }
  100. }
  101. }