Google Admob with Unity3D

Monetization is desired by game developers and standard app developers alike. For the developers that would like to keep their work free to download, in app advertising is often a good choice. Admob by Google is convenient for ads because it is compatible with iOS and Android without changes to the Unity3D project.

Start by downloading the latest Admob Plugin and importing it into your Unity3D project. To do this, from the Assets menu item, choose Import Package and then choose Custom Package.

Choose to import the GoogleMobileAds.unitypackage file that was downloaded from Google’s Github repository and make sure to leave everything checked.

Create a new empty GameObject in your project. This object will be used to initialize your Admob ads so go ahead and name it Admob. In the Github repository there is a nice sample script to be used as a component on the object. Below is a slightly modified version of the sample script:

This version of the script will only show the ad if on an Android or iOS device. This means you don’t have to touch your project or code set if you plan to compile for Windows Phone or anything else.

Now this is where it gets a little complicated. You must add the Google Play Services library into the /Assets/Plugins/Android directory of your Unity3D project. The Google Play Services library can be found in /Android-SDK/extras/google/google_play_services/libproject. Copy the entire directory that resides in libproject. Building and running for Android should work without issues at this point.

For iOS you must do extra steps. Download the latest Admob iOS SDK and import the following files into the classes group of your Xcode project:

GADAdMobExtras.h

GADAdNetworkExtras.h

GADAdSize.h

GADBannerView.h

GADBannerViewDelegate.h

GADInterstitial.h

GADInterstitialDelegate.h

GADRequest.h

GADRequestError.h

Import the following files into the library group of your Xcode project:

libGoogleAdMobAds.a

The iOS project is not quite complete yet. You must add -ObjC to the Other Linker Flags of your application and add the following frameworks if they have not already been added:

AdSupport

AudioToolbox

AVFoundation

CoreGraphics

CoreTelephony

MessageUI

StoreKit

SystemConfiguration

Building the Xcode project at this point should be successful.

Resources

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.