Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Google Admob with Unity3D

TwitterFacebookRedditLinkedInHacker News

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.

Import 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:

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

public class Admob : MonoBehaviour {

    private BannerView bannerView;

    void Start()
    {
        #if UNITY_EDITOR
            string adUnitId = "unused";
        #elif UNITY_ANDROID
            string adUnitId = "INSERT_YOUR_ANDROID_AD_UNIT_HERE";
        #elif UNITY_IPHONE
            string adUnitId = "INSERT_YOUR_IOS_AD_UNIT_HERE";
        #else
            string adUnitId = "unexpected_platform";
        #endif

        #if UNITY_ANDROID || UNITY_IPHONE
            bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);
            bannerView.AdLoaded += HandleAdLoaded;
            bannerView.AdFailedToLoad += HandleAdFailedToLoad;
            bannerView.AdOpened += HandleAdOpened;
            bannerView.AdClosing += HandleAdClosing;
            bannerView.AdClosed += HandleAdClosed;
            bannerView.AdLeftApplication += HandleAdLeftApplication;
            RequestBanner();
        #endif
    }

    void RequestBanner() {
        AdRequest request = new AdRequest.Builder()
            .AddTestDevice(AdRequest.TestDeviceSimulator)
            .AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
            .AddKeyword("game")
            .AddKeyword("arcade")
            .SetGender(Gender.Male)
            .SetBirthday(new DateTime(1985, 1, 1))
            .TagForChildDirectedTreatment(false)
            .AddExtra("color_bg", "9B30FF")
            .Build();
        bannerView.LoadAd(request);
    }

    void ShowBanner() {
        bannerView.Show();
    }

    void HideBanner() {
        bannerView.Hide();
    }

    #region Banner callback handlers

    public void HandleAdLoaded()
    {
        print("HandleAdLoaded event received.");
    }

    public void HandleAdFailedToLoad(string message)
    {
        print("HandleFailedToReceiveAd event received with message: " + message);
    }

    public void HandleAdOpened()
    {
        print("HandleAdOpened event received");
    }

    void HandleAdClosing ()
    {
        print("HandleAdClosing event received");
    }

    public void HandleAdClosed()
    {
        print("HandleAdClosed event received");
    }

    public void HandleAdLeftApplication()
    {
        print("HandleAdLeftApplication event received");
    }

    #endregion
}

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

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