Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load admob ads into a unity 5 project?

Basically I'm trying to load a banner ad into my Unity 5 project and export to iOS.

Here is the code i'm calling inside of unity which is attached to a game object:

using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;
using System;

public class AdController : 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", AdSize.SmartBanner, AdPosition.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-xxxxxxxxxxxxxxx");

        //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 sender, EventArgs args) {

        interstitial.Show ();
    }


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

}

I'm using:

  • Unity 5.0.1f1
  • Xcode 6.3
  • Google Unity Plugin 2.2.1
  • Google Ads SDK 7.2.1

Has anyone got this to serve ads? Note: I did replace the xxxxx with the correct Ad Unit ID.

like image 884
Edward Avatar asked Nov 09 '22 14:11

Edward


1 Answers

There is an project on GitHub:

Unity Admob Plugin

it easy to use,and I have success with this.

using admob;
...
Admob.Instance().initAdmob("admob banner id", "admob interstitial id");//admob id with format ca-app-pub-2796046890663330/756767388
Admob.Instance().showBannerRelative(AdSize.Banner, AdPosition.BOTTOM_CENTER, 0);
like image 77
jobs Avatar answered Nov 14 '22 21:11

jobs