Posted:

When developing with AdMob in Unity, it is a common practice to make your banner ads persist across multiple scenes. This blog post will highlight best practices to accomplish this with the Google Mobile Ads Unity Plugin.

The most straightforward approach is to link the lifecycle of ads to that of the scenes they’re displayed in. When transitioning from one scene to another, existing ads are destroyed before leaving the first scene. New ads can then be created and displayed in the next scene.

The downside of this approach is that every scene transition would result in a new banner request. This may not be desirable if scene transitions are frequent and occur quickly.

An alternative is to use a GameObject as a wrapper for banners or interstitials. By default, each GameObject in a scene will be destroyed once the new scene is loaded (unless you use additive scene loading). However, you can make a GameObject survive across scenes by marking it with DontDestroyOnLoad. You can then use the GameObject.Find method to obtain references to the wrapper GameObject from scripts in other scenes.

Here is an example of how to use a GameObject to wrap banner ads:

// FirstSceneScript.cs
void Start() {       
  // Create a wrapper GameObject to hold the banner.
  GameObject myGameObject = new GameObject("myBannerAdObject");
  myGameObject.AddComponent<BannerWrapper>();
  // Mark the GameObject not to be destroyed when new scenes load.
  DontDestroyOnLoad(myGameObject);
}
The BannerWrapper GameObject is a wrapper for a BannerView.
// BannerWrapper.cs
using System;

using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;

public class BannerWrapper : MonoBehaviour {
    
    public BannerView bannerView;
    
    void Start()
    {
        bannerView = new BannerView(
                "your_ad_unit_id", AdSize.SmartBanner, AdPosition.Bottom);
        AdRequest request = new AdRequest.Builder().Build();
        bannerView.LoadAd (request);
        
        bannerView.Show();
    }
}

In SecondSceneScript.cs, which is attached to the second scene, you can find a GameObject by name, get the BannerWrapper component, and access the BannerView:

// SecondSceneScript.cs
void Start () {
    GameObject myGameObject = GameObject.Find("myBannerAdObject");
    BannerWrapper bannerWrapper = myGameObject.GetComponent();
    bannerWrapper.bannerView.Hide();
}

By managing your ads efficiently and seamlessly across scenes, you are sure to provide a better ad experience for your users. If you have any questions about Unity integration, you can reach us on our forum. You can also find our quick-start guide linked here. Remember that you can also find us on Google+, where we have updates on all of our Google Ads developer products.

Posted:

We’re excited to announce the v2.3.0 release of the Google Mobile Ads Unity Plugin! The new release brings support for AdMob in-app purchase ads to the Unity game engine. You can grab the updated Unity package on GitHub.

In-app purchase ads in Unity with AdMob

In-app purchase (IAP) ads are interstitial ads that display offers for your in-app products. They allow users to make purchases directly from within your app as part of your normal ad flow.

Note: The plugin currently only supports IAP ads on Android. iOS support is not yet available.

Before integrating IAP ads into your app, make sure you’ve set up an IAP house ad campaign and created an IAP house ad. You should also install the plugin as explained in the AdMob Unity Quick Start guide.

Once you’ve set up your campaign, there are five steps to integrate IAP ads:

  1. In the AndroidManifest.xml in Assets/Plugins/Android/GoogleMobileAds/Plugin, uncomment the following line to enable billing permissions:
    <!--<uses-permission android:name="com.android.vending.BILLING"/> -->
  2. Create a class that implements the IInAppPurchaseHandler interface. See GoogleMobileAdsDemoScript.cs for an implementation example. You need to define the following methods:
    1. OnInAppPurchaseFinished -- here you credit the user with the purchase, and then call result.FinishPurchase() to finish the transaction.
    2. IsValidPurchase -- check the SKU against valid SKUs and return true if this purchase is valid.
    3. AndroidPublicKey { get; } -- return the public key for your Android app, which you obtain from the Google Play console.
  3. Pass in the above implementation of IInAppPurchaseHandler to InterstitialAd.SetInAppPurchaseHandler.
  4. Make sure you request an in-house IAP ad by setting the correct adUnitId when creating the InterstitialAd. See In-App Purchase Overview for detailed instructions on how to set up IAP house ads in your AdMob account.
  5. Add the Conversion Tracking and Remarketing SDK to the Plugins/Android directory.

That’s it!

An example IAP ad.

If you have any questions, please drop by our forum.

Posted:

We have launched the Google Mobile Ads Unity Plugin v2.2.1. The updated v2.2.1 Unity package is available for download on GitHub here.

Multiple ad positions

Google Mobile Ads Unity Plugin v2.2.1 introduces support for additional banner position locations. The full list of banner positions is as follows:

  • Top
  • Bottom
  • TopLeft
  • TopRight
  • BottomLeft
  • BottomRight

The additional positions are specified by setting the AdPosition value when instantiating a bannerView:

//Create a banner at the top-right of the screen.
BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.TopRight);

iOS Ads SDK 7.0.0 Compatibility

With the v7.0.0 release, the iOS Ads SDK became a module framework and Google Mobile Ads Unity Plugin v2.2.1 complies with this change. For modules to work, you must enable them in the project build settings. Search for "modules", and set Enable Modules to YES. The Link Frameworks Automatically option should be set to YES as well.

Unity 5.0 and ARC

Unity 5.0 has moved out of beta and brings with it support for Automatic Reference Counting (ARC) for iOS. v2.2.1 of the Unity plugin takes advantage of ARC with no additional changes in project settings or code.

The source code and a sample app for the plugin are available on our GitHub repo. A changelog for this release is listed here. If you have any questions about Unity integration, you can reach us on our forum. Remember that you can also find us on Google+, where we have updates on all of our Google Ads developer products.

Posted:

Today, we’re delighted to announce the launch of the Google Mobile Ads Unity Plugin v2.1, now with interstitial ads. You can start playing around with the latest code by grabbing the v2.1 Unity package on GitHub.

Interstitial Ads

There are four steps required to integrate interstitial ads:

  1. Create a new InterstitialAd, specifying your interstitial ad unit ID
  2. Register for the ad events you care about
  3. Load the InterstitialAd with an AdRequest
  4. Show the InterstitialAd at an appropriate place in your app

The first three steps can be performed at the same time, and can even live in its own helper function:

public InterstitialAd CreateAndLoadInterstitial() {
  // Initialize an InterstitialAd.
  InterstitialAd interstitial = new InterstitialAd("MY_AD_UNIT_ID");
  // Register for ad events.
  interstitial.AdLoaded += delegate(object sender, EventArgs args) {};
  interstitial.AdFailedToLoad += delegate(object sender, AdFailToLoadEventArgs args) {};
  interstitial.AdOpened += delegate(object sender, EventArgs args) {};
  interstitial.AdClosing += delegate(object sender, EventArgs args) {};
  interstitial.AdClosed += delegate(object sender, EventArgs args) {};
  interstitial.AdLeftApplication += delegate(object sender, EventArgs args) {};
  // Load the InterstitialAd with an AdRequest.
  interstitial.LoadAd(new AdRequest.Builder().Build());
}

You should wait to show the interstitial until a good stopping point in your app, for example when the user finishes a level in your game. Remember to check that the interstitial has finished loading before you show it:

// Call this when a level finishes.
public void LevelFinished() {
  if (interstitial.isLoaded()) {
    interstitial.Show();
  }
}

Keep in mind that an interstitial is a one-time use object. Once the interstitial is closed, you can dispose of the object and prepare another one. This can be implemented directly in the AdClosed event.

interstitial.AdClosed += delegate(object sender, EventArgs args)
{
  interstitial.Destroy();
  interstitial = CreateAndLoadInterstitial();
};

Ad Events now use EventHandlers

In this release, we also changed our ad events to be of type EventHandler instead of Action for both BannerView and InterstitialAd. This means your callback methods now take an object representing the event sender, and an EventArgs:

interstitial.AdLoaded = delegate(object sender, EventArgs args)
{
  print(“Interstitial Loaded”);
};

The only event with special event args is AdFailedToLoad. It passes an instance of AdFailedToLoadEventArgs with a Message describing the error.

interstitial.AdFailedToLoad = delegate(object sender, AdFailedToLoadEventArgs args)
{
  print("Interstitial Failed to load: " + args.Message);
};

The source code as well as a sample app for the plugin is available on our googleads-mobile-plugins GitHub repo. If you have any questions about Unity integration, you can reach us on our forum. Remember that you can also find us on Google+, where we have updates on all of our Google Ads developer products.

Labels: mobile_ads_sdk, unity

Posted:

Calling all Unity app developers! We are excited to announce the launch of version 2.0 of the Google Mobile Ads Unity Plugin. The new version comes with a completely rewritten, but much more flexible, API. It includes the following new features:

  • A single package supporting both Android and iOS
  • Support for running apps in the Unity editor
  • Ability to create multiple banner instances
  • Ability to create banners of any size
  • Flexible ad request targeting
  • and much more!

Taking a closer look at how to integrate the plugin, a typical banner request in v2.0 looks like this:

BannerView bannerView = new BannerView(
    "YOUR_AD_UNIT_ID", AdSize.Banner, AdPosition.Top);
AdRequest request = new AdRequest.Builder().Build();
bannerView.LoadAd(request);

For custom banner sizes, simply pass in an AdSize object into the BannerView constructor:

AdSize adSize = new AdSize(250, 250);
BannerView bannerView = new BannerView(
    "YOUR_AD_UNIT_ID", adSize, AdPosition.Top);

Want to pass additional targeting parameters? No problem! Set your custom targeting when building the AdRequest:

AdRequest request = new AdRequest.Builder()
        .AddTestDevice(AdRequest.TestDeviceSimulator)
        .AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
        .AddKeyword("unity")
        .SetGender(Gender.Male)
        .SetBirthday(new DateTime(1985, 1, 1))
        .TagForChildDirectedTreatment(true)
        .Build();

Listening for ad events is also extremely straightforward. Register for the callbacks you care about:

bannerView.AdLoaded += HandleAdLoaded;
bannerView.AdFailedToLoad += HandleAdFailedToLoad;
bannerView.AdOpened += HandleAdOpened;
bannerView.AdClosing += HandleAdClosing;
bannerView.AdClosed += HandleAdClosed;
bannerView.AdLeftApplication += HandleAdLeftApplication;

…

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

You can also manage the lifecycle of each BannerView by calling show(), hide(), or destroy().

To get access to these awesome features, check out the source code. Also stay tuned for upcoming support for interstitial ads.

If you have any feature requests or bug reports against the plugin, track it! If you have questions about how to use the plugin, speak up! And if you just want the latest news on what’s going on in the wonderful world of Google Ads, circle us!