LoginSignup
0
0

【Unity】Admobバナーのサンプルコードを用いた場合に、バナーが表示されない場合の対処法 【エラー】

Last updated at Posted at 2024-04-10

はじめに

Admobの初期設定が終わり、いざAdmobバナーのサンプルコードをそのまま用いて実装しようとしても、エラーが発生するか、もしくはバナーが表示されない場合があります。
本記事では、こちらのURLの初期設定が終わった方に向けて、このサンプルコードを用いてバナーを実装する際の、正しい実装方法と注意点について説明します。
(本記事は、2024年4月の記事です。)

本記事では、注意点はこのように黄色の枠で囲っています

Admobバナーのサンプルコードの実装方法

基本的に、このサンプルコードからコピペで実装していきます。

1. usingを追加し、Start()メソッドをコピペする

using GoogleMobileAds;
using GoogleMobileAds.Api;
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    public void Start()
    {
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize((InitializationStatus initStatus) =>
        {
            // This callback is called once the MobileAds SDK is initialized.
        });
    }
}

ここで、クラス名はファイル名と一致していれば何でも構いません。

2. クラス内にメソッドを追加する

・バナービューを作成する

// These ad units are configured to always serve test ads.
#if UNITY_ANDROID
  private string _adUnitId = "ca-app-pub-3940256099942544/6300978111";
#elif UNITY_IPHONE
  private string _adUnitId = "ca-app-pub-3940256099942544/2934735716";
#else
  private string _adUnitId = "unused";
#endif

  BannerView _bannerView;

  /// <summary>
  /// Creates a 320x50 banner view at top of the screen.
  /// </summary>
  public void CreateBannerView()
  {
      Debug.Log("Creating banner view");

      // If we already have a banner, destroy the old one.
      if (_bannerView != null)
      {
          DestroyAd();
      }

      // Create a 320x50 banner at top of the screen
      _bannerView = new BannerView(_adUnitId, AdSize.Banner, AdPosition.Top);
  }

ここで、DestroyAdというメソッドは後にも先にも存在しないので、以下のコードに修正してください。

/// DestroyAd();
_bannerView.Destroy();
_bannerView = null;

・バナー広告を読み込む

/// <summary>
/// Creates the banner view and loads a banner ad.
/// </summary>
public void LoadAd()
{
    // create an instance of a banner view first.
    if(_bannerView == null)
    {
        CreateBannerView();
    }

    // create our request used to load the ad.
    var adRequest = new AdRequest();

    // send the request to load the ad.
    Debug.Log("Loading banner ad.");
    _bannerView.LoadAd(adRequest);
}

CreateBannerViewメソッドのみだと空のオブジェクトが生成されるだけので、LoadAdメソッドは絶対に追加するようにしてください

3. Start()にメソッドを追加する

クラス内にメソッドを追加しただけではそれらのメソッドは呼び出されないので、Startで呼び出しましょう。

public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    public void Start()
    {
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize((InitializationStatus initStatus) =>
        {
            // This callback is called once the MobileAds SDK is initialized.
        });
        
        // 以下にコードを追加
        CreateBannerView();
        LoadAd();
    }
}

繰り返しになりますが、ちゃんとLoadAdも追加しましょう

最終的なコード

最終的なコードはこちらです。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;

public class GoogleMobileAdsDemoScript : MonoBehaviour
{

    public void Start()
    {
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize((InitializationStatus initStatus) =>
        {
            // This callback is called once the MobileAds SDK is initialized.
        });
        CreateBannerView();
        LoadAd();
    }

    // These ad units are configured to always serve test ads.
#if UNITY_ANDROID
  private string _adUnitId = "ca-app-pub-3940256099942544/6300978111";
#elif UNITY_IPHONE
  private string _adUnitId = "ca-app-pub-3940256099942544/2934735716";
#else
    private string _adUnitId = "unused";
#endif

    BannerView _bannerView;

    /// <summary>
    /// Creates a 320x50 banner view at top of the screen.
    /// </summary>
    public void CreateBannerView()
    {
        Debug.Log("Creating banner view");

        // If we already have a banner, destroy the old one.
        if (_bannerView != null)
        {
            _bannerView.Destroy();
            _bannerView = null;
        }

        // Create a 320x50 banner at top of the screen
        _bannerView = new BannerView(_adUnitId, AdSize.Banner, AdPosition.Top);
    }


    /// <summary>
    /// Creates the banner view and loads a banner ad.
    /// </summary>
    public void LoadAd()
    {
        // create an instance of a banner view first.
        if (_bannerView == null)
        {
            CreateBannerView();
        }

        // create our request used to load the ad.
        var adRequest = new AdRequest();

        // send the request to load the ad.
        Debug.Log("Loading banner ad.");
        _bannerView.LoadAd(adRequest);
    }

}

まとめ

本記事では、Admobバナーのサンプルコードを実装する際の注意点について説明しました。
この方法で上手くいかなかった方がいればコメントお願いします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0