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.


Monday, March 9, 2015

How to integrate Admob into Unity 5 iOS build - deprecated

This method is deprecated.  See latest method in this article.

I am using MacOS Mavericks, Unity 5 Personal Edition, and XCode 6.1

Download Unity plugin from here:

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

At time of writing the latest (outdated version) is Google Mobile Ads Unity Plugin v2.1

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

Next, is to update the plugin v2.1.

Open a terminal and type:

git clone https://github.com/googleads/googleads-mobile-plugins

After downloading the latest plugin using git, enter the folder called:

googleads-mobile-plugin/unity/source/Assets/Plugins/iOS

and copy the files:


and paste it to overwrite all the files in your Unity Plugins iOS folder:


This will update all the iOS Admob Plugins to the latest version so that it can work with the latest AdmobSDK v7.


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:



Then, add the compiler flag:   -fno-objc-arc

to each of the files shown below under Build Phases:



The above files (from Admob plugin) does not use ARC, but Unity 5 iOS is now ARC-based. So we need to tell XCode not to use ARC for the Admob files above. See this discussion and this one. And also this.

Plug your iPad/iPhone in and run.