Tuesday, March 31, 2015

How to integrate Admob into Unity 5 iOS build

I am using MacOS Mavericks, Unity 5 Personal Edition, and XCode 6.1
For integrating Admob into Unity Android, see this post.

Download Unity plugin from here:

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

At time of writing the latest  is Google Mobile Ads Unity Plugin v2.2

Download it and import to unity after creating a Plugins folder in Assets.

Insert the necessary code to call your Admob Banner and Interstitial ads. For me I wrote a custom script called AdsController.cs:

using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;
using System;  // For EventArgs

public class AdsController : MonoBehaviour {
   
    InterstitialAd interstitial;
    BannerView bannerView;

    void Start () {

        
//------ Banner Ad -------
        // Create a 320x50 banner at the top of the screen.
        // Put your Admob banner ad id here
        bannerView = new BannerView(
            "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx"AdSize.SmartBannerAdPosition.Top);
        // Create ad request
        AdRequest request = new AdRequest.Builder().Build();
        // Load the banner with the request.
        bannerView.LoadAd(request);
        
        bannerView.Show();

        
//---- Interstitial Ad -----
        // Initialize an InterstitialAd.
        // Put your admob interstitial ad id here:
        interstitial = new InterstitialAd("
ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx");

        //Add callback for when ad is loaded
        interstitial.AdLoaded += HandleAdLoaded;

        // Create an ad request.
        AdRequest requestInterstitial = new AdRequest.Builder().Build();
        // Load the interstitial with the request.
        interstitial.LoadAd(requestInterstitial);
    }
    
   

    public void HandleAdLoaded(object senderEventArgs args) {
        
        interstitial.Show ();
    }

  
    void OnDestroy(){
        if (interstitial!=null) {
            interstitial.AdLoaded -= HandleAdLoaded;
            interstitial.Destroy ();
        }
        if(bannerView!=null){
            bannerView.Destroy ();
        }
    }

}


Put the script above in Game Over scene, or, Level Cleared scene. So that when the scene loads, the banner and interstitial ads get called and shown at the same time.

Next, build the Unity iOS XCode project with the player setttings shown here (on right side of screen):



Download the iOS Google Mobile Ads SDK v7 from:


Unzip the file and you will get:

GoogleMobileAds.framework

Open the exported Unity project in Xcode6 and add the GoogleMobileAds.framework to your project:



Enable modules in Xcode Build Settings:


Plug your iPad/iPhone in and run.


1 comment:

  1. all in on plugin ,https://github.com/unity-plugins/Unity-Admob

    ReplyDelete