Se actualizó este documento.
La traducción en español no está disponible todavía.
Actualización del documento en inglés: 3 may. 2021
Actualización del documento en español: 21 sep. 2018

Add Interstitial Ads to a Unity App

The Audience Network allows you to monetize your Android and iOS apps with Facebook ads. Interstitial ads are a full ad screen experience that can be used at transition points in the flow of an app, for instance between activities or during the pause between levels in a game. The creative content could be of images, videos or carousels. This guide explains how to add interstitial ads to your app.

Asegúrate de haber completado las guías Primeros pasos de Audience Network y Primeros pasos de Unity antes de seguir.

Interstitial Ad Steps

Step 1: Create an Interstitial Ad Object

Step 2: Adding Callback Events

Step 3: Load an Ad

Step 4: Showing the Ad

Step 1: Create an Interstitial Ad Object

The first step toward displaying an interstitial ad is to create an InterstitialAd object in a C# script attached to a GameObject.

...
using AudienceNetwork;
...

public class InterstitialAdTest : MonoBehaviour
{
    ...
    private InterstitialAd interstitialAd;
    private bool isLoaded;
    ...

    public void LoadInterstitial()
    {
        this.interstitialAd = new InterstitialAd("YOUR_PLACEMENT_ID");
        this.interstitialAd.Register(this.gameObject);

        // Set delegates to get notified on changes or when the user interacts with the ad.
        this.interstitialAd.InterstitialAdDidLoad = (delegate() {
            Debug.Log("Interstitial ad loaded.");
            this.isLoaded = true;
        });
        interstitialAd.InterstitialAdDidFailWithError = (delegate(string error) {
            Debug.Log("Interstitial ad failed to load with error: " + error);
        });
        interstitialAd.InterstitialAdWillLogImpression = (delegate() {
            Debug.Log("Interstitial ad logged impression.");
        });
        interstitialAd.InterstitialAdDidClick = (delegate() {
            Debug.Log("Interstitial ad clicked.");
        });

        this.interstitialAd.interstitialAdDidClose = (delegate() {
            Debug.Log("Interstitial ad did close.");
            if (this.interstitialAd != null) {
                this.interstitialAd.Dispose();
            }
        });

        // Initiate the request to load the ad.
        this.interstitialAd.LoadAd();
    }
    ...
}

The constructor for an InterstitialAd has the following parameters:

  • placementId - The Audience Network placement ID for this interstitial ad unit.

Step 2: Adding Callback Events

Next, you can implement a few callbacks to subscribe to the life cycle events of the ad. Listen for these events by registering a delegate for the event, as shown below in the example:

...
// Set delegates to get notified on changes or when the user interacts with the ad.
this.interstitialAd.InterstitialAdDidLoad = (delegate() {
    Debug.Log("Interstitial ad loaded.");
    this.isLoaded = true;
});
interstitialAd.InterstitialAdDidFailWithError = (delegate(string error) {
    Debug.Log("Interstitial ad failed to load with error: " + error);
});
interstitialAd.InterstitialAdWillLogImpression = (delegate() {
    Debug.Log("Interstitial ad logged impression.");
});
interstitialAd.InterstitialAdDidClick = (delegate() {
    Debug.Log("Interstitial ad clicked.");
});

this.interstitialAd.interstitialAdDidClose = (delegate() {
    Debug.Log("Interstitial ad did close.");
    if (this.interstitialAd != null) {
        this.interstitialAd.Dispose();
    }
});
...

Callback for Ad Activity Destroyed in Unity Android

This is only relevant to Android.

Currently Unity games for Android only supports the main Unity Activity to have launchMode of singleTask. See Unity document for the Android manifest and the Android document for activity.

Because we use Activity to show Interstitial and Rewarded Video ads, the ad activity could be destroyed without being properly closed when a user backgrounds an app and then relaunch it using the icon - and not the app switcher. You can use the following callbacks and check if the ad was closed by the user:

For Interstitial:

        this.interstitialAd.interstitialAdDidClose = (delegate() {
            Debug.Log("Interstitial ad did close.");
            this.didClose = true;
            if (this.interstitialAd != null) {
                this.interstitialAd.Dispose();
            }
        });

#if UNITY_ANDROID
        /*
         * Only relevant to Android.
         * This callback will only be triggered if the Interstitial activity has
         * been destroyed without being properly closed. This can happen if an
         * app with launchMode:singleTask (such as a Unity game) goes to
         * background and is then relaunched by tapping the icon.
         */
        this.interstitialAd.interstitialAdActivityDestroyed = (delegate() {
            if (!this.didClose) {
                Debug.Log("Interstitial activity destroyed without being closed first.");
                Debug.Log("Game should resume.");
            }
        });
#endif

For Rewarded Video:

        this.rewardedVideoAd.rewardedVideoAdDidClose = (delegate() {
            Debug.Log("Rewarded video ad did close.");
            this.didClose = true;
            if (this.rewardedVideoAd != null) {
                this.rewardedVideoAd.Dispose();
            }
        });

#if UNITY_ANDROID
        /*
         * Only relevant to Android.
         * This callback will only be triggered if the Rewarded Video activity
         * has been destroyed without being properly closed. This can happen if
         * an app with launchMode:singleTask (such as a Unity game) goes to
         * background and is then relaunched by tapping the icon.
         */
        this.rewardedVideoAd.rewardedVideoAdActivityDestroyed = (delegate() {
            if (!this.didClose) {
                Debug.Log("Rewarded video activity destroyed without being closed first.");
                Debug.Log("Game should resume. User should not get a reward.");
            }
        });
#endif

Step 3: Load an Ad

Once the InterstitialAd is instantiated, the next step is to load an ad. That's done with the loadAd() method in the InterstitialAd class.

In the example shown above, here's how to load an ad:

...
this.interstitialAd.LoadAd();
...

Step 4: Showing the Ad

Finally, after the interstitial ad is loaded, you can call the Show method to render the interstitial ad on the screen. For example you can have a function for ShowInterstitial, and call this function when it's time to show the interstitial ad:

// Show button
public void ShowInterstitial()
{
    if (this.isLoaded) {
        this.interstitialAd.Show();
        this.isLoaded = false;

    } else {
        Debug.Log("Interstitial Ad not loaded!");
    }
}

Próximos pasos

Usa nuestras guías para integrar distintos formatos de anuncios en tu aplicación de Unity:

Cuando estés listo para lanzar tu aplicación y monetizarla, envíala para someterla a revisión, después de asegurarte de que cumpla las políticas de Audience Network y las Normas comunitarias de Facebook.