初心者のUNITY開発ブログ

unity開発する時、メモーとしてブログを利用しています。

AdMob利用しよう

AdMOBをUNITYに取り入れる

 

 1.「Unity用AdMob Plugin」DLしましょう

 

 https://github.com/googleads/googleads-mobile-unity/releases

GoogleMobileAds.unitypackage」というパッケージファイルをDLし、

Unityプロジェクトにインポートしましょう。 

f:id:akworks001:20180503095039p:plain

 

f:id:akworks001:20180503095819p:plain

f:id:akworks001:20180503095939p:plain

[import]をクリックして、インポートをしましょう

 

2 [GoogleMobileAds.framework]DLしましょう

https://developers.google.com/admob/ios/download

 

f:id:akworks001:20180503093543p:plain

 DLしたframeworkをunity project-Plugins-iOSに入れる

f:id:akworks001:20180503101113p:plain

GoogleMovileAds.frameworkを選択し、inspectorが表示され、

 Platform settingsーFramework dependenciesの項目にチェックを入れる

  • AdSupport
  • AudioToolbox
  • AVFoundation
  • CoreGraphics
  • CoreTelephony
  • EventKit
  • EventKitUI
  • MessageUI
  • StoreKit
  • SystemConfiguration

f:id:akworks001:20180503101515p:plain

追加されたGOOGLEMOBILEADSの中にEDITORーGoogleMobileAdsDependencies.xml

ファイルをクリックして

f:id:akworks001:20180505101403p:plain

 

bitcodeEnabled="true"  に変更する。

f:id:akworks001:20180505103536p:plain

これで、UNITYからビルドして、frameworkが設定完了。

3. 広告スクリプトを作りましょう

 

using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;
 
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;
 
    bool is_close_interstitial = false;
 
    // Use this for initialization
    void Awake () {
        // 起動時にインタースティシャル広告をロードしておく
        RequestInterstitial ();
        // バナー広告を表示
        RequestBanner ();
    }
 
    // Update is called once per frame
    void Update () {
 
    }
 
    public void RequestBanner()
    {
        #if UNITY_ANDROID
        string adUnitId = Android_Banner;
        #elif UNITY_IPHONE
        string adUnitId = ios_Banner;
        #else
        string adUnitId = "unexpected_platform";
        #endif
 
        // Create a 320x50 banner at the top of the screen.
        BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the banner with the request.
        bannerView.LoadAd(request);
    }
 
    public void RequestInterstitial()
    {
        #if UNITY_ANDROID
        string adUnitId = Android_Interstitial;
        #elif UNITY_IPHONE
        string adUnitId = ios_Interstitial;
        #else
        string adUnitId = "unexpected_platform";
        #endif
 
        if (is_close_interstitial == true) {
            interstitial.Destroy ();
        }
 
        // Initialize an InterstitialAd.
        interstitial = new InterstitialAd (adUnitId);
        // Create an empty ad request.
        request = new AdRequest.Builder ().Build ();
        // Load the interstitial with the request.
        interstitial.LoadAd (request);
 
        interstitial.OnAdClosed += HandleAdClosed;
        interstitial.OnAdFailedToLoad += HandleAdReLoad;
 
        is_close_interstitial = false;
    }
 
    // インタースティシャル広告を閉じた時に走る
    void HandleAdClosed(object sender, System.EventArgs e)
    {
        is_close_interstitial = true;
 
        RequestInterstitial();
    }
    // 広告のロードに失敗したときに走る
    void HandleAdReLoad(object sender, System.EventArgs e)
    {
        is_close_interstitial = true;
 
        StartCoroutine(_waitConnect());
    }
 
    // 次のロードまで30秒待つ
    IEnumerator _waitConnect()
    {
        while (true)
        {
            yield return new WaitForSeconds(30.0f);
 
            // ネットに接続できるときだけリロード
            if (Application.internetReachability != NetworkReachability.NotReachable)
            {
                RequestInterstitial();
                break;
            }
        }
    }
 
    public void interstitialShow()
    {
        if (interstitial.IsLoaded())
        {
            interstitial.Show();
        }
    }
}
 
4.BUILDして、エラ〜発生
 

f:id:akworks001:20180503135324p:plain

 
 

f:id:akworks001:20180503142928p:plain

 

vi ~/.bash_profile

f:id:akworks001:20180503151459p:plain

 

最後 source ~/.bash_profile

 

f:id:akworks001:20180503151656p:plain

 

UNITY 再度BUILDして、エラーなくなりました。

 

5.TERMINAL で   pod install --repo-update

 

f:id:akworks001:20180503152616p:plain

 

6. XcodeでBUILDしてみましょう