実現したいこと
- リワード広告の広告を閉じたら報酬を獲得して、ゲームを再開できるようにしたい
前提・発生している問題
現在自分はAdmobの広告を使ってUnityでゲームを開発し、GooglePlayConsoleで内部テストを行っています。そのため、Admobのテスト広告を現在使っています。自分のスマートフォンでそのテストを行って見たところ、バナー広告、インタースティシャル広告、リワード広告全て表示されていました。しかしながら、リワード広告を見て、閉じるボタンを押した際にゲームがかたまり、広告を読み込んでいる画面(下のコードのRewardingObがSetActive(true)の状態)のまま停止していました。それは1分以上待っても解決されませんでした。バナー広告、インタースティシャル広告はクリックしても元のゲームに戻り、再開することが出来ました。また、unity上でリワード広告を閉じてもゲーム内で報酬をもらう事ができ、ゲームを再開することができました。
該当のソースコード
この下はAdmobの処理のコードです。こちらのコードは自分のコードをそのまま載せています。
c#
1using System; 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5using GoogleMobileAds; 6using GoogleMobileAds.Api; 7 8public class AdmobScript : MonoBehaviour 9{ 10 public bool watchreward; 11 public bool rewarderror; 12 public bool canshowedreward = true; 13 14 //参考 https://sole-game-creater.com/unity-admob-android/ 15 private BannerView bannerView; 16 17 public void Start() 18 { 19 watchreward = false; 20 // Google AdMob Initial 21 MobileAds.Initialize(initStatus => { }); 22 23 this.RequestBanner(); 24 } 25 26 private void RequestBanner() 27 { 28 //前のテスト用 29 /* 30#if UNITY_ANDROID 31 string adUnitId = "ca-app-pub-3940256099942544/6300978111"; // テスト用広告ユニットID 32#elif UNITY_IPHONE 33 string adUnitId = "ca-app-pub-3940256099942544/2934735716"; // テスト用広告ユニットID 34#else 35 string adUnitId = "unexpected_platform"; 36#endif 37 */ 38 39 // These ad units are configured to always serve test ads. 40#if UNITY_EDITOR 41 string adUnitId = "unused"; 42#elif UNITY_ANDROID 43 string adUnitId = "ca-app-pub-3212738706492790/6113697308"; 44#elif UNITY_IPHONE 45 string adUnitId = "ca-app-pub-3212738706492790/5381898163"; 46#else 47 string adUnitId = "unexpected_platform"; 48#endif 49 50 AdSize adaptiveSize = AdSize.GetCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(AdSize.FullWidth); 51 // Create a 320x50 banner at the bottom of the screen. 52 this.bannerView = new BannerView(adUnitId, adaptiveSize, AdPosition.Bottom); 53 54 // Create an empty ad request. 55 AdRequest request = new AdRequest(); 56 57 // Load the banner with the request. 58 bannerView.LoadAd(request); 59 60 } 61 62 63 private InterstitialAd interstitial; 64 public void loadInterstitialAd() 65 { 66#if UNITY_ANDROID 67 string adUnitId = "ca-app-pub-3940256099942544/1033173712"; 68#elif UNITY_IPHONE 69 string adUnitId = "ca-app-pub-3940256099942544/4411468910"; 70#else 71 string adUnitId = "unexpected_platform"; 72#endif 73 InterstitialAd.Load(adUnitId, new AdRequest(), 74 (InterstitialAd ad, LoadAdError loadAdError) => 75 { 76 if (loadAdError != null) 77 { 78 // Interstitial ad failed to load with error 79 interstitial.Destroy(); 80 return; 81 } 82 else if (ad == null) 83 { 84 // Interstitial ad failed to load. 85 return; 86 } 87 ad.OnAdFullScreenContentClosed += () => { 88 HandleOnAdClosed(); 89 }; 90 ad.OnAdFullScreenContentFailed += (AdError error) => 91 { 92 HandleOnAdClosed(); 93 }; 94 interstitial = ad; 95 }); 96 } 97 private void HandleOnAdClosed() 98 { 99 this.interstitial.Destroy(); 100 this.loadInterstitialAd(); 101 } 102 public void showInterstitialAd() 103 { 104 if (interstitial != null && interstitial.CanShowAd()) 105 { 106 interstitial.Show(); 107 } 108 else 109 { 110 Debug.Log("Interstitial Ad not load"); 111 } 112 } 113 114 // These ad units are configured to always serve test ads. 115#if UNITY_ANDROID 116 private string _adUnitIdreward = "ca-app-pub-3940256099942544/5224354917"; 117#elif UNITY_IPHONE 118 private string _adUnitIdreward = "ca-app-pub-3940256099942544/1712485313"; 119#else 120 private string _adUnitIdreward = "unused"; 121#endif 122 123 private RewardedAd _rewardedAd; 124 125 /// <summary> 126 /// Loads the rewarded ad. 127 /// </summary> 128 public void LoadRewardedAd() 129 { 130 // Clean up the old ad before loading a new one. 131 if (_rewardedAd != null) 132 { 133 _rewardedAd.Destroy(); 134 _rewardedAd = null; 135 } 136 137 Debug.Log("Loading the rewarded ad."); 138 139 // create our request used to load the ad. 140 var adRequest = new AdRequest(); 141 142 // send the request to load the ad. 143 RewardedAd.Load(_adUnitIdreward, adRequest, 144 (RewardedAd ad, LoadAdError error) => 145 { 146 // if error is not null, the load request failed. 147 if (error != null || ad == null) 148 { 149 Debug.LogError("Rewarded ad failed to load an ad " + 150 "with error : " + error); 151 return; 152 } 153 154 Debug.Log("Rewarded ad loaded with response : " 155 + ad.GetResponseInfo()); 156 157 _rewardedAd = ad; 158 }); 159 160 RegisterEventHandlers(_rewardedAd); 161 RegisterReloadHandler(_rewardedAd); 162 } 163 164 public void ShowRewardedAd() 165 { 166 const string rewardMsg = 167 "Rewarded ad rewarded the user. Type: {0}, amount: {1}."; 168 169 if (_rewardedAd != null && _rewardedAd.CanShowAd()) 170 { 171 _rewardedAd.Show((Reward reward) => 172 { 173 // TODO: Reward the user. 174 Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount)); 175 }); 176 canshowedreward = true; 177 } 178 else 179 { 180 canshowedreward = false; 181 } 182 } 183 184 private void RegisterEventHandlers(RewardedAd ad) 185 { 186 // Raised when the ad is estimated to have earned money. 187 ad.OnAdPaid += (AdValue adValue) => 188 { 189 Debug.Log(String.Format("Rewarded ad paid {0} {1}.", 190 adValue.Value, 191 adValue.CurrencyCode)); 192 }; 193 // Raised when an impression is recorded for an ad. 194 ad.OnAdImpressionRecorded += () => 195 { 196 Debug.Log("Rewarded ad recorded an impression."); 197 }; 198 // Raised when a click is recorded for an ad. 199 ad.OnAdClicked += () => 200 { 201 Debug.Log("Rewarded ad was clicked."); 202 }; 203 // Raised when an ad opened full screen content. 204 ad.OnAdFullScreenContentOpened += () => 205 { 206 Debug.Log("Rewarded ad full screen content opened."); 207 }; 208 // Raised when the ad closed full screen content. 209 ad.OnAdFullScreenContentClosed += () => 210 { 211 Debug.Log("Rewarded ad full screen content closed."); 212 StartCoroutine(AdFullScreenContentClosed()); 213 }; 214 // Raised when the ad failed to open full screen content. 215 ad.OnAdFullScreenContentFailed += (AdError error) => 216 { 217 Debug.LogError("Rewarded ad failed to open full screen content " + 218 "with error : " + error); 219 rewarderror = true; 220 }; 221 } 222 223 IEnumerator AdFullScreenContentClosed() 224 { 225 yield return null; 226 yield return null; 227 watchreward = true; 228 } 229 230 private void RegisterReloadHandler(RewardedAd ad) 231 { 232 // Raised when the ad closed full screen content. 233 ad.OnAdFullScreenContentClosed += () => 234 { 235 Debug.Log("Rewarded Ad full screen content closed."); 236 237 // Reload the ad so that we can show another as soon as possible. 238 LoadRewardedAd(); 239 }; 240 // Raised when the ad failed to open full screen content. 241 ad.OnAdFullScreenContentFailed += (AdError error) => 242 { 243 Debug.LogError("Rewarded ad failed to open full screen content " + 244 "with error : " + error); 245 246 // Reload the ad so that we can show another as soon as possible. 247 LoadRewardedAd(); 248 }; 249 } 250}
こちらはゲーム側のコードです。広告を閉じる部分だけ切り取っています。情報が足りない部分があったら教えてください。
c#
1using System; 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5using UnityEngine.UI; 6using UnityEngine.EventSystems; 7using UnityEngine.SceneManagement; 8using DG.Tweening; 9 10public class numbersystem : MonoBehaviour 11{ 12 [SerializeField] AdmobScript admobscript; 13 [SerializeField] GameObject RewardingOb; 14 15 16 void FixedUpdate(){ 17 //広告処理 18 if (admobscript.watchreward) 19 { 20 StartCoroutine(EarnedReward()); 21 } 22 } 23 24 IEnumerator EarnedReward() 25 { 26 yield return null; 27 yield return null; 28 yield return null; 29 30 ShowAnswerReward(); 31 RewardingOb.SetActive(false); 32 admobscript.watchreward = false; 33 } 34}
スマートフォンでテストを行った時広告を閉じたときRewardingObがfalseになりませんでした。
試したこと
https://nae3na.hatenablog.com/entry/admob-reward-earned-crash
この上のサイトを参考にして、直そうとしましたが、解決には至りませんでした。
今回のコードはこの上のサイトを参考にして書き直したものです。
補足情報(FW/ツールのバージョンなど)
Unity2022.3.15f1を使っています
回答1件
あなたの回答
tips
プレビュー