【実施時期】
2019年4月上旬
【動作環境】
Windows10 64Bit
Unity2018.3.9 64Bit
Google Admob Ads Unity Plugin Ver.3.16
スクリプトエディタ:VisualStudio2017
【症状】
AdmobのUnityPluginをインポートし、テスト広告を実装して
プロジェクトを実行してみましたが、広告が表示されません。
【エラーメッセージ】
NullReferenceException:オブジェクト参照がオブジェクトのインスタンスに設定されていません
ADManager.HandleBannerADEvents(System.Boolean subscribe)(Assets / Scripts / ADManager.cs:123)
[原因]
bannerADのインスタンス化に失敗したため、NULL例外エラーが発生したと思われます。
【参考にしたチュートリアルのURL】
TouTube:
Unity Game Monetization | Admob Unity Tutorial
https://www.youtube.com/watch?v=tGtRx1nOiGg
/* AdManager.cs */ using System.Collections; using System.Collections.Generic; using System; using UnityEngine; using GoogleMobileAds.Api; public class ADManager : MonoBehaviour { private string APP_ID = "ca-app-pub-3709341320635003~7864168412"; private BannerView bannerAD; private InterstitialAd interstitialAd; private RewardBasedVideoAd rewardVideoAd; void Start() { // this is when you publish your app MobileAds.Initialize(APP_ID); //RequestBanner(); RequestInterstitial(); RequestVideoAD(); } void RequestBanner() { string banner_ID = "ca-app-pub-3940256099942544/6300978111"; // テスト用のUnitID if (bannerAD != null) { bannerAD.Destroy(); } bannerAD = new BannerView(banner_ID, AdSize.SmartBanner, AdPosition.Bottom); // FOR REAL APP //AdRequest adRequest = new AdRequest.Builder().Build(); // FOR TESTING AdRequest adRequest = new AdRequest.Builder() .AddTestDevice("2077ef9a63d2b398840261c8221a0c9b").Build(); bannerAD.LoadAd(adRequest); } void RequestInterstitial() { string interstitial_ID = "ca-app-pub-3940256099942544/1033173712"; // テスト用のUnitID interstitialAd = new InterstitialAd(interstitial_ID); // FOR REAL APP //AdRequest adRequest = new AdRequest.Builder().Build(); // FOR TESTING AdRequest adRequest = new AdRequest.Builder() .AddTestDevice("2077ef9a63d2b398840261c8221a0c9b").Build(); interstitialAd.LoadAd(adRequest); } void RequestVideoAD() { string video_ID = "ca-app-pub-3940256099942544/5224354917"; // テスト用のUnitID rewardVideoAd = RewardBasedVideoAd.Instance; // FOR REAL APP //AdRequest adRequest = new AdRequest.Builder().Build(); // FOR TESTING AdRequest adRequest = new AdRequest.Builder() .AddTestDevice("2077ef9a63d2b398840261c8221a0c9b").Build(); rewardVideoAd.LoadAd(adRequest, video_ID); } public void Display_Banner() { bannerAD.Show(); } public void Display_InterstitialAD() { if (interstitialAd.IsLoaded()) { interstitialAd.Show(); } } public void Display_Reward_Video() { if(rewardVideoAd.IsLoaded()) { rewardVideoAd.Show(); } } // HANDLE EVENTS public void HandleOnAdLoaded(object sender, EventArgs args) { // ad is loaded show it Display_Banner(); } public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args) { // ad failed to load load it again RequestBanner(); } public void HandleOnAdOpened(object sender, EventArgs args) { MonoBehaviour.print("HandleAdOpened event received"); } public void HandleOnAdClosed(object sender, EventArgs args) { MonoBehaviour.print("HandleAdClosed event received"); } public void HandleOnAdLeavingApplication(object sender, EventArgs args) { MonoBehaviour.print("HandleAdLeavingApplication event received"); } void HandleBannerADEvents(bool subscribe) { if(subscribe) { // Called when an ad request has successfully loaded. bannerAD.OnAdLoaded += HandleOnAdLoaded; // Called when an ad request failed to load. bannerAD.OnAdFailedToLoad += HandleOnAdFailedToLoad; // Called when an ad is clicked. bannerAD.OnAdOpening += HandleOnAdOpened; // Called when the user returned from the app after an ad click. bannerAD.OnAdClosed += HandleOnAdClosed; // Called when the ad click caused the user to leave the application. bannerAD.OnAdLeavingApplication += HandleOnAdLeavingApplication; } else { // Called when an ad request has successfully loaded. bannerAD.OnAdLoaded -= HandleOnAdLoaded; // Called when an ad request failed to load. bannerAD.OnAdFailedToLoad -= HandleOnAdFailedToLoad; // Called when an ad is clicked. bannerAD.OnAdOpening -= HandleOnAdOpened; // Called when the user returned from the app after an ad click. bannerAD.OnAdClosed -= HandleOnAdClosed; // Called when the ad click caused the user to leave the application. bannerAD.OnAdLeavingApplication -= HandleOnAdLeavingApplication; } } void OnEnable() { HandleBannerADEvents(true); } void OnDisable() { HandleBannerADEvents(false); } } // class
ちなみに、「バナー広告」の処理をメントアウトすると、上記のエラーは消えますが、テストデバイスと広告の両方は表示されません。
お手数ですが、何卒よろしくお願いします。
あなたの回答
tips
プレビュー