質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
AdMob

AdMobは、事前に指定した条件に従ってアプリに広告を表示するGoogleのサービス。開発者の向けのサービスで、広告を掲載することにより、収益を得ることが可能です。その他、見た目や雰囲気などアプリに合う広告に変更したり、広告表示の場所を指定することもできます。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

340閲覧

(Admob) リワード広告の広告を閉じた際にゲームが動かなくなる

CPU

総合スコア13

AdMob

AdMobは、事前に指定した条件に従ってアプリに広告を表示するGoogleのサービス。開発者の向けのサービスで、広告を掲載することにより、収益を得ることが可能です。その他、見た目や雰囲気などアプリに合う広告に変更したり、広告表示の場所を指定することもできます。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

1グッド

1クリップ

投稿2023/12/29 16:04

実現したいこと

  • リワード広告の広告を閉じたら報酬を獲得して、ゲームを再開できるようにしたい

前提・発生している問題

現在自分は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を使っています

shinoharat👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

_aki__

2023/12/30 11:33

これってリワードが重複して獲得されてたりしませんか? StartCoroutine(EarnedReward()); が何度か呼ばれていそうなのが気になります。
CPU

2023/12/30 12:49 編集

質問ありがとうございます。unityのconsoleを確認してみたところ、Rewarded Ad full screen content closed.が2回連続で表示されていました。どのように修正すると解決できるのか分からないため教えていただけますでしょうか?
_aki__

2023/12/30 15:01

こんばんは、ご返信ありがとうございます。 ”Rewarded Ad full screen content closed”が2回表示されていることに関しましては、コードの中で2回記述されているのでエラーなのかどうかはわかりません🙏 私が気になったのは広告を閉じる部分です。 こうするとどうでしょうか? IEnumerator EarnedReward() { admobscript.watchreward = false; yield return null; yield return null; yield return null; ShowAnswerReward(); RewardingOb.SetActive(false); }
CPU

2023/12/30 16:00

>”Rewarded Ad full screen content closed”が2回表示されていることに関しましては、コードの中で2回記述されているのでエラーなのかどうかはわかりません おっしゃる通りでした。 変更してGooglePlayConsoleにアップロードしてスマートフォンで試してみましたが上手くいきませんでした。ShowAnswerReward()の中のはじめにHintImage.SetActive(false);という記述があるのですが、それも反応していませんでした。
_aki__

2023/12/31 00:25

ShowAnswerReward()が呼ばれていないとすると 処理がどこまで走っているのか探ると原因がわかるかもしれません。 広告を閉じたときのログは出ているようなので、 そのあと処理がどこまで進行しているのか確認してみるといいかも👌
guest

回答1

0

自己解決

LoadRewardedAd()を次のように変えればできました。原理は分かりません

c#

1 public void LoadRewardedAd() 2 { 3 // Clean up the old ad before loading a new one. 4 if (_rewardedAd != null) 5 { 6 _rewardedAd.Destroy(); 7 _rewardedAd = null; 8 } 9 10 Debug.Log("Loading the rewarded ad."); 11 12 // create our request used to load the ad. 13 var adRequest = new AdRequest(); 14 15 // send the request to load the ad. 16 RewardedAd.Load(_adUnitIdreward, adRequest, 17 (RewardedAd ad, LoadAdError error) => 18 { 19 // if error is not null, the load request failed. 20 if (error != null || ad == null) 21 { 22 Debug.LogError("Rewarded ad failed to load an ad " + 23 "with error : " + error); 24 return; 25 } 26 27 Debug.Log("Rewarded ad loaded with response : " 28 + ad.GetResponseInfo()); 29 30 RegisterEventHandlers(ad); 31 RegisterReloadHandler(ad); 32 _rewardedAd = ad; 33 }); 34 }

投稿2024/01/17 05:42

CPU

総合スコア13

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問