4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UnityでAdMobを実装する

Last updated at Posted at 2017-01-08

前提

各ツール・プラグイン等

  • Unity 5.5.0f3
  • Xcode 8.2.1
  • AdMob 3.1.3

プラットフォーム

  • 開発機
    • macOS (MacBookPro)
      • Windowsの場合は大差はないが、適宜読み替えること
  • リリース
    • Android
    • iOS

内容

Unityでの開発

  1. AdMobを利用して、アプリ作成

    AdMobManager.cs
    using UnityEngine;
    using System.Collections;
    using GoogleMobileAds.Api;
    using System;
    
    public class AdMobManager : MonoBehaviour
    {
        public string Android_Banner;
        public string Android_Interstitial;
        public string ios_Banner;
        public string ios_Interstitial;
    
        private InterstitialAd interstitial;
        private AdRequest request;
        private static AdMobManager instance;
    
        public delegate void CallBack();
    
        private CallBack callback;
    
        void Start()
        {
                instance = this;
    
                RequestInterstitial();
    
                RequestBanner();
        }
    
        public void RequestBanner()
        {
    #if UNITY_ANDROID
                string adUnitId = Android_Banner;
    #elif UNITY_IOS
                string adUnitId = ios_Banner;
    #endif
    
                BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
                AdRequest request = new AdRequest.Builder()
                        //.AddTestDevice(AdRequest.TestDeviceSimulator)
                        .Build();
    
                bannerView.LoadAd(request);
        }
    
        public void RequestInterstitial()
        {
    #if UNITY_ANDROID
                string adUnitId = Android_Interstitial;
    #elif UNITY_IOS
                string adUnitId = ios_Interstitial;
    #endif
    
                interstitial = new InterstitialAd(adUnitId);
                interstitial.OnAdClosed += delegate (object sender, EventArgs args)
                {
                        interstitial.Destroy();
                        RequestInterstitial();
    
    #if UNITY_IOS
                        if (callback != null)
                        {
                                callback();
                        }
    #endif
                };
    
                request = new AdRequest.Builder()
                        //.AddTestDevice(AdRequest.TestDeviceSimulator)
                        .Build();
    
                interstitial.LoadAd(request);
        }
    
        public static void DisplayInterstitial(CallBack cb = null)
        {
                instance.displayInterstitial(cb);
        }
    
        private void displayInterstitial(CallBack cb = null)
        {
                callback = cb;
    
                if (interstitial.IsLoaded())
                {
                        interstitial.Show();
    #if !UNITY_IOS || UNITY_EDITOR
                        if (callback != null)
                        {
                                callback();
                        }
    #endif
                }
                else
                {
                        if (callback != null)
                        {
                                callback();
                        }
                }
        }
    }
    
  2. Unityビルド

    • Androidの場合はAPKをそのまま端末にインストール
    • iOSの場合はプロジェクトが作成されるので、以下手順へ

Xcodeでの開発

  1. 作成されたプロジェクトをXcodeで開いて各種設定を調整
    1. provisioning profileを設定(事前に作成しておく
  2. Xcodeビルド

以上。

補足

AdMobの詳細設定については、記事も多いため省略。

4
6
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
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?