這份文件已更新。
中文(台灣) 的翻譯尚未完成。
英文更新時間:2021年5月3日
中文(台灣) 更新時間:2018年9月21日

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.

繼續操作前,請務必詳閱 Audience Network 新手指南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!");
    }
}

後續步驟

若要在 Unity 應用程式中整合不同的廣告格式,請依照指南操作:

當您準備好讓應用程式正式上線並開始營利,請確保您的應用程式並未違反 Audience Network 政策Facebook 社群守則,接著便可以將應用程式送審