###前提・実現したいこと
UnityでAdMobの導入後以下の警告メッセージがゲームを実行するたびに表示されてしまいます。
調べるとMonoBehaviourを継承したClassのインスタンスを作成する時にNewを使用してインスタンスを作成すると出てくるエラー
らしいのですが調べたサイトのように書き換えてみたもののエラーが逆に増えてしまったため
こちらで質問することにしました。
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all UnityEngine.MonoBehaviour:.ctor() ButtonBehaviour:.ctor() GoogleMobileAds.Unity.BannerClient:CreateButtonBehavior() (at Assets/GoogleMobileAds/Platforms/Unity/BannerClient.cs:64) GoogleMobileAds.Unity.BannerClient:ShowBannerView() (at Assets/GoogleMobileAds/Platforms/Unity/BannerClient.cs:143) GoogleMobileAds.Unity.BannerClient:LoadAd(AdRequest) (at Assets/GoogleMobileAds/Platforms/Unity/BannerClient.cs:123) GoogleMobileAds.Api.BannerView:LoadAd(AdRequest) (at Assets/GoogleMobileAds/Api/BannerView.cs:61) AdMobManager:RequestBanner() (at Assets/Project/Scripts/Admob/AdMobManager.cs:40) AdMobManager:Awake() (at Assets/Project/Scripts/Admob/AdMobManager.cs:19) ``` ###該当のソースコード BannerClient.cs
using System;
using System.Reflection;
using System.Collections.Generic;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;
using UnityEngine;
using UnityEngine.UI;
namespace GoogleMobileAds.Unity
{
public class BannerClient : BaseAdDummyClient, IBannerClient
{
// Ad event fired when the banner ad has been received.
public event EventHandler<EventArgs> OnAdLoaded;
// Ad event fired when the banner ad has failed to load.
public event EventHandler<AdFailedToLoadEventArgs> OnAdFailedToLoad;
// Ad event fired when the banner ad is opened.
public event EventHandler<EventArgs> OnAdOpening;
// Ad event fired when the banner ad is closed.
public event EventHandler<EventArgs> OnAdClosed;
// Ad event fired when the banner ad is leaving the application.
public event EventHandler<EventArgs> OnAdLeavingApplication;
// Ad event fired when the banner ad is estimated to have earned money.
public event EventHandler<AdValueEventArgs> OnPaidEvent;
private Dictionary<AdSize, string> prefabAds = new Dictionary<AdSize, string>() { {AdSize.Banner, "DummyAds/Banners/BANNER"}, {AdSize.SmartBanner, "DummyAds/Banners/SMART_BANNER" }, {AdSize.MediumRectangle, "DummyAds/Banners/MEDIUM_RECTANGLE" }, {AdSize.IABBanner, "DummyAds/Banners/FULL_BANNER" }, {AdSize.Leaderboard, "DummyAds/Banners/LEADERBOARD" }, {new AdSize (320,100), "DummyAds/Banners/LARGE_BANNER" } }; private ButtonBehaviour buttonBehaviour; private void AddClickBehavior(GameObject dummyAd) { Image myImage = dummyAd.GetComponentInChildren<Image>(); Button button = myImage.GetComponentInChildren<Button>(); button.onClick.AddListener(() => { buttonBehaviour.OpenURL(); }); }
//該当部分-----------------------------------------------
private void CreateButtonBehavior()
{
buttonBehaviour = new ButtonBehaviour(); //←警告メッセージが出ている箇所
buttonBehaviour.OnAdOpening += OnAdOpening;
buttonBehaviour.OnLeavingApplication += OnAdLeavingApplication;
}
//-----------------------------------------------
<省略>
// Shows the banner view on the screen.
public void ShowBannerView()
{
dummyAd = AdBehaviour.ShowAd(prefabAd, getRectTransform(prefabAd).anchoredPosition);
CreateButtonBehavior();
AddClickBehavior(dummyAd);
}
###該当のソースコード ButtonBehaviour.cs
using UnityEngine;
using UnityEngine.UI;
using System;
public class ButtonBehaviour : MonoBehaviour
{
public event EventHandler<EventArgs> OnAdOpening;
public event EventHandler<EventArgs> OnLeavingApplication;
public void OpenURL() { Debug.Log("Opened URL"); Application.OpenURL("http://google.com"); if (OnAdOpening != null) { OnAdOpening.Invoke(this, new EventArgs()); } if (OnLeavingApplication != null) { OnLeavingApplication.Invoke(this, new EventArgs()); } }
調べたサイト [UNITY5の備忘録 様](http://fumotoppara.blog.fc2.com/blog-entry-9.html) [Unity+UnrealEngine4+Blog 様](https://nabesi777.hatenablog.com/entry/2018/12/07/%E3%82%A8%E3%83%A9%E3%83%BC%E8%A7%A3%E6%B1%BA%EF%BC%9AYou_are_trying_to_create_a_MonoBehaviour_using_the_%27new%27_keyword__This_is_not_allowed__MonoBehaviours_can_only_be_added_using_AddComponent%28) インポートしたGoogleMobileAdsのバージョンは5.4.0になります。 CreateButtonBehavior()の部分をコメントアウトした場合は警告は出なくなりますが 今度はバナーをクリックした場合にエラーが表示されます。(パソコンのみ) Unityのバージョン:2018.4.28f1 言語:C# よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー