Questo documento è stato aggiornato.
La traduzione in Italiano non è ancora completa.
Aggiornamento inglese: 3 mag 2021
Aggiornamento Italiano: 21 set 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.

Prima di procedere, assicurati di aver completato la Guida introduttiva Audience Network e le Guide introduttive Unity.

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!");
    }
}

Passaggi successivi

Segui le nostre guide per l'integrazione di diversi formati pubblicitari nell'app Unity:

Quando è tutto pronto per la pubblicazione della tua app e per la monetizzazione, invia l'app per l'analisi dopo averne verificato la conformità con le normative di Audience Network e gli Standard della community di Facebook.