.:: Alex Erne :: PHP/MySQL & Unity3D/C# projects ::.
Recent news
Categories
Like me on Facebook
* advertisement *
  13 July 2016 - Implementing AdMob in your Unity project :: Posted in Unity3D

I've recently recieved an eMail from Google, stating that the Android Google Mobile Ads SDK will become obsolete coming September 15th. The AdMob SDK versions 6.4.1 and lower will simply stop to work from that data, and if you have used it in your projects (like I did with TetraGems), you will need to upgrade the AdMob handler to version 7.0.0 or higher. Sadly though, there's NO stand alone AdMob SDK anymore and it's been fully integrated into the Google Play SDK.

It's the Google Play SDK that does give some problems though. With the current latest version of the Google Play Services (rev. 31 in the Android SDK Manager) kinda missing in the package, you have to grab the SDK elsewhere. I've found a copy (rev. 28) on the Unity forums and will share it through my dropbox for your conveneince. The contents of the Google Play Services (rev. 28) needs to be copied into your Android SDK installation directory (C:Program Files (x86)Androidandroid-sdkextrasgoogle by default).

Next up you need to check a couple of packages that MUST BE INSTALLED in your Android SDK Manager. These packages are:
  - Android SDK Build-Tools rev. 23.0.3. I've read reports that rev. 24 is broken, and I couldn't get it to work either.
  - Android Support Repository
  - Android Support Library (Obsolete). I'm not 100% certain this one has to be installed, but the Google representatives keep hammering on the forums that it has to be there - odd though for an obsolete package...
  - Google Play services, even while it's not actually installed right now...
  - Google Repository

With the Android SDK Manager now setup correctly, it's time to download the Unity plugin for the Google Mobile Ads SDK from GitHub. I realize that there are enough alternatives on the Unity Asset Store, but I'm a bit reluctant to use those because you have no clue what's been done to those packages. With that, I've tried 2 free ones from the Asset Store and I could not get those to work, while the paid ones are just plain stupid when you can code it yourself!  Having the plugin, you now need to add this one to your current project (either double-click the GoogleMobileAds_305.unitypackage file or use Unity3D's Import Asset function).

So far that's all you have to do. Now all you need to do is add a bit of simple code to get the AdMob plugin to actually work and drop the advertisement on your app. And the code required is very small:

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

public class Create_AdMob : MonoBehaviour {
  #if UNITY_ANDROID
  string adUnitId = "ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXX";
  #elif UNITY_IPHONE
  string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
  #else
  string adUnitId = "unexpected_platform";
  #endif
  public static BannerView bannerView;

  // Use this for initialization
  void Start () {
    // Create a 320x50 banner at the top of the screen.
    bannerView=new BannerView(adUnitId, AdSize.SmartBanner , AdPosition.Top);
    RequestBanner();
  }

  private void RequestBanner() {
    // Create an empty ad request.
    AdRequest request = new AdRequest.Builder().Build();
    // Load the banner with the request.
    bannerView.LoadAd(request);
  }
}

Now all there's left is create an EMPTY GAMEOBJECT in your scene and attach the Create_AdMob.cs script to it and you're ready to go!

A couple of notes though....
1) I haven't tested this on iOS since I'm not an Apple fan and don't own ANY (mobile) Apple devices. Please leave feedback if it works as well under iOS as it does under Android.
2) Obiously, the string asUnitId should hold your app's advertisement ID.
3) Do keep in mind that it's against Google's agreement to click your own ads! If you want to test your ads, replace the AdRequest request into

// Test device
AdRequest request = new AdRequest.Builder()
  .AddTestDevice(AdRequest.TestDeviceSimulator)
  .AddTestDevice(SystemInfo.deviceUniqueIdentifier)
  .Build();

4) I've made bannerView public and static so that you can call it from any other routine and use Create_AdMob.bannerView.Show() and Create_AdMob.bannerView.Hide() so you can toggle visibility of the displayed advertisement.

There's a lot more to this plugin and all is described in it's official documentation. I just figured to describe the easiest way to make your advertisement work with your apps...


Post A Comment!
Your comment:
Title

Your name

Image text
* advertisement *
* advertisement *

This website is Copyright © 2009 - 2016 Alex Erné
PHP/MySQL coding by Alex Erné Copyright © 2009 - 2016 Alex Erné
...all rights reserved...

AE games™ and the AE games logo are Trademarks owned by Alex Erné
TetraGems™, Burst-a-Bubble™ and the Burst-a-Bubble logo are Trademarks owned by Alex Erné