今回は、Unityで作ったアプリにAdMobを活用してバナー広告とインタースティシャル広告を実装する方法をご紹介します。
AdMobは広告の収益性が高く、安定した成果が期待できるため、アプリに広告を導入する際にはAdMobの利用を強くおすすめします。
AdMob側の設定

AdMobアカウントの設定
GoogleのAdMob(https://admob.google.com/intl/ja/home/)にアクセスしてアカウントを作成します。お申し込みのボタンから登録を行なってください。
アプリの登録
アカウントの作成が完了したら、
「アプリ」>「アプリを追加」から、iOS もしくは Android を選択してください。
今回は iOS を選んだ場合を例に解説していきますが、基本的な手順はどちらも共通ですので、Androidを選んだ方もそのまま読み進めて問題ありません。


そしてアプリの名前を検索して該当するアプリを選んでアプリを追加してください。

これでアプリの登録は完了です。
※アプリの審査が終わり、AppStoreやGooglePlayStoreに登録されても、AdMobの検索に引っ掛かるようになるまでは時間がかかる場合があります。もし、審査後3〜5日経っても検索に引っかからない場合、GoogleMobileAdsSDKに英語で以下のようなメッセージを送ると対応してくれます。

広告ユニットの作成
対象のアプリを選んだ後、広告ユニット>広告ユニットを追加

インタースティシャル広告とバナー広告の広告ユニットを作成してください。

ユニット名はbannerやinterとかで大丈夫です。アプリIDと広告ユニットIDは後々使いますがこれでAdMob側の設定は終わりです。

Unity側の設定

UnityプロジェクトにAdMob SDKを導入する
1.Google Mobile Ads Unity Plugin をダウンロード
2.Unityのプロジェクトに .unitypackage をインポート
3.自動的に GoogleMobileAds フォルダが作成され、必要なスクリプトが追加されます
※ iOS向けの場合は、Xcode側で CocoaPodsの設定が必要になります。
Unity側にAdMobのアプリIDを設定する
先ほど準備した①のアプリIDをUnity側に設定していきます。
Unity>Assets>GoogleMobileAds>SettingsでiOSまたはAndroidのアプリID(①)を設定します。


初期化処理の実装
using GoogleMobileAds.Api;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
public void Start()
{
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize(initStatus => { });
}
}
アプリ起動時に最初に表示されるシーンに、先ほどのコードをアタッチした GoogleAds という GameObject を配置してください。
これにより、アプリ起動と同時に広告の初期化処理が自動で実行されるようになります。
この処理は、広告を正しく動作させるために必須となるので、忘れずに設定しておきましょう。
インタースティシャル広告の実装の例
using GoogleMobileAds;
using GoogleMobileAds.Api;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GoogleAdsMovie : MonoBehaviour
{
#if UNITY_IPHONE
private string _adUnitId = "iOSのインタースティシャル広告のユニットID";
#else
private string _adUnitId = "Androidのインタースティシャル広告のユニットID";
#endif
private InterstitialAd _interstitialAd;
/// <summary>
/// Loads the interstitial ad.
/// </summary>
public void LoadInterstitialAd()
{
// Clean up the old ad before loading a new one.
if (_interstitialAd != null)
{
_interstitialAd.Destroy();
_interstitialAd = null;
}
Debug.Log("Loading the interstitial ad.");
// create our request used to load the ad.
var adRequest = new AdRequest();
// send the request to load the ad.
InterstitialAd.Load(_adUnitId, adRequest,
(InterstitialAd ad, LoadAdError error) =>
{
// if error is not null, the load request failed.
if (error != null || ad == null)
{
Debug.LogError("interstitial ad failed to load an ad " +
"with error : " + error);
return;
}
Debug.Log("Interstitial ad loaded with response : "
+ ad.GetResponseInfo());
_interstitialAd = ad;
// イベントハンドラを登録
RegisterEventHandlers(_interstitialAd);
});
}
public void ShowInterstitialAd()
{
if (_interstitialAd != null && _interstitialAd.CanShowAd())
{
Debug.Log("Showing interstitial ad.");
_interstitialAd.Show();
}
else
{
FadeManager.Instance.LoadScene("Title", 1f);
//SceneManager.LoadScene("Title");
LoadInterstitialAd();
Debug.LogError("Interstitial ad is not ready yet.");
}
}
private void RegisterEventHandlers(InterstitialAd interstitialAd)
{
// Raised when an impression is recorded for an ad.
interstitialAd.OnAdImpressionRecorded += () =>
{
Debug.Log("Interstitial ad recorded an impression.");
};
// Raised when a click is recorded for an ad.
interstitialAd.OnAdClicked += () =>
{
Debug.Log("Interstitial ad was clicked.");
};
// Raised when an ad opened full screen content.
interstitialAd.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Interstitial ad full screen content opened.");
};
// Raised when the ad closed full screen content.
interstitialAd.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Interstitial ad full screen content closed.");
SceneManager.LoadScene("Title");// 広告が閉じられたときに遷移
LoadInterstitialAd(); // 新しい広告を読み込む
};
// Raised when the ad failed to open full screen content.
interstitialAd.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Interstitial ad failed to open full screen content " +
"with error : " + error);
SceneManager.LoadScene("Title");// エラー時に遷移
LoadInterstitialAd(); // 新しい広告を読み込む
};
}
public void Start()
{
LoadInterstitialAd();
}
}
インタースティシャル広告を表示したいシーンに、上記のコードをアタッチした GoogleAds Movieという GameObject を配置してください。
広告が閉じられた時と、エラーで広告が準備できなかった場合にシーン移動する処理を追記しています。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Manager : MonoBehaviour
{
[SerializeField] GoogleAdsMovie googleAdsMovie;
void Update()
{
//広告を流したいタイミングで
googleAdsMovie.ShowInterstitialAd();
}
}
上記のコードはインタースティシャル広告の再生のタイミングを制御しています。この制御のコードをインタースティシャル広告を流すシーンでの主な制御を行うスクリプトに組み込むことで、インタースティシャル広告の実装が可能になります。
バナー広告の実装の例
using GoogleMobileAds;
using GoogleMobileAds.Api;
using UnityEngine;
public class GoogleAdsBanner : MonoBehaviour
{
#if UNITY_IPHONE
private string _adUnitId = "iOSアプリのAdMobのバナーユニットID";
#else
private string _adUnitId = "AndroidアプリのAdMobのバナーユニットID";
#endif
BannerView _bannerView;
public void CreateBannerView()
{
Debug.Log("Creating banner view");
if (_bannerView != null)
{
DestroyAd();
}
AdSize adSize = new AdSize(250, 250);
_bannerView = new BannerView(_adUnitId, adSize, 520, 78);
}
public void LoadAd()
{
if (_bannerView == null)
{
CreateBannerView();
}
var adRequest = new AdRequest();
Debug.Log("Loading banner ad.");
_bannerView.LoadAd(adRequest);
}
public void DestroyAd()
{
if (_bannerView != null)
{
Debug.Log("Destroying banner view.");
_bannerView.Destroy();
_bannerView = null;
}
}
void OnDestroy()
{
DestroyAd();
}
}
バナー広告を表示したいシーンに、上記のコードをアタッチした GoogleAdsBannerという GameObject を配置してください。
AdSize adSize = new AdSize(250, 250);ここの250,250はバナーのサイズが縦250px、横250pxであることを表します。自分が実現したい規格のサイズを調べてその値に変更してください。詳しくはhttps://developers.google.com/admob/android/banner/fixed-size?hl=jaでサイズ確認してください。
_bannerView = new BannerView(_adUnitId, adSize, 520, 78);ここはバナーの位置を変更しています。一度0,0でバナーが表示されるのを確認してから、px単位なので少しずつ値を変えながら、置きたい場所にバナーが現れるように変更していきましょう。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Manager : MonoBehaviour
{
[SerializeField] GoogleAdsBanner googleAdsBanner;
void Start()
{
if (//広告削除済み)
{
}
else if (//広告削除なし)
{
//バナー広告の再生
googleAdsBanner.LoadAd();
}
}
public void OnClick ExitButton()
{
//シーンから移動するときにバナーを消す
googleAdsBanner.DestroyAd();
//シーン移動
}
}
上記のコードはバナー広告の再生と削除のタイミングを制御しています。この制御のコードをバナー広告を流すシーンでの主な制御を行うスクリプトに組み込むことで、バナー広告の実装が可能になります。
まとめ
これで、バナー広告とインタースティシャル広告の2種類の広告をアプリに実装できるようになったはずです。
紹介したコードはそのままでは動作しませんが、ご自身のゲームに合わせてコメント文を参考に調整することで、簡単に導入できるはずです。
この記事が、みなさんのアプリ開発における収益向上の一助となれば幸いです!
参考
