ピクチャー イン ピクチャー広告(ベータ版)を実装する

プラットフォームを選択: Android iOS Unity

ピクチャー イン ピクチャー

ピクチャー イン ピクチャー(PiP)広告は、記事、フィード、ゲームプレイなど、画面上のコンテンツの上に重ねて表示されるフローティング ウィンドウに表示されます。このフォーマットでは、広告が表示されていても、ユーザーはアプリを操作できます。画面全体に表示されないディスプレイ広告の場合は、このフォーマットを選択してください。

このガイドでは、Google Mobile Ads Unity Plugin を使用してアプリでピクチャー イン ピクチャー広告をリクエストして表示する方法について説明します。

始める前に

先に進む前に、次のことを実施してください。

  • テスト広告を有効にして、次のテスト広告ユニット ID を使用します。

    /21775744923/example/picture-in-picture
    

広告を読み込む

PictureInPictureAd オブジェクトを読み込むには、広告リクエストを作成して load メソッドを呼び出します。

public void LoadPictureInPictureAd()
{
    // Clean up any existing ad before loading a new one.
    if (_pictureInPictureAd != null)
    {
        _pictureInPictureAd.Destroy();
        _pictureInPictureAd = null;
    }

    // Create our request used to load the ad.
    var adRequest = new AdManagerAdRequest();

    // Send the request to load the ad.
    PictureInPictureAd.Load(_adUnitId, adRequest,
        (PictureInPictureAd ad, LoadAdError error) =>
        {
            if (error != null || ad == null)
            {
                Debug.LogWarning(
                    "Picture-in-picture ad failed to load: " + error);
                return;
            }

            // Capture the PictureInPictureAd reference for later use.
            _pictureInPictureAd = ad;
        });
}

_adUnitId は、実際の広告ユニット ID に置き換えてください。

広告を表示する

ピクチャー イン ピクチャー広告を画面に表示するには、show メソッドを呼び出します。次の例では、広告をデフォルトの位置に表示します。

public void ShowPictureInPictureAd()
{
    // Use the ad reference saved from the Load callback.
    if (_pictureInPictureAd != null)
    {
        _pictureInPictureAd.Show();
    }
    else
    {
        Debug.Log("No ad to show.");
    }
}

位置を設定する

デフォルトでは、Google Mobile Ads Unity Plugin は、最初に表示するときは画面の右下、以前に表示されたことがある場合は最後に確認された位置にピクチャー イン ピクチャー広告を表示します。広告の表示位置をカスタマイズするには、広告を表示する際に位置を指定します。次の例では、コンテンツに重ねた、画面の左上の位置を設定しています。

public void ShowPictureInPictureAdTopLeft()
{
    // Use the ad reference saved from the Load callback.
    if (_pictureInPictureAd != null)
    {
        _pictureInPictureAd.Show(PictureInPictureAdPosition.TopLeft);
    }
    else
    {
        Debug.Log("No ad to show.");
    }
}

使用可能なすべての位置については、PictureInPictureAdPosition をご覧ください。

広告イベントをリッスンする

ピクチャー イン ピクチャー広告のライフサイクル イベントを処理するには、広告を表示する前に広告イベントに登録します。これらのイベント レポートは、クリックやインプレッションなどの広告インタラクションをレポートします。また、広告の表示や非表示など、ピクチャー イン ピクチャー固有のイベントもレポートします。

private void RegisterEventHandlers(PictureInPictureAd ad)
{
    // Raised when the ad is estimated to have earned money.
    ad.OnAdPaid += (AdValue adValue) =>
    {
        Debug.Log($"Picture-in-picture ad paid: {adValue.Value} {adValue.CurrencyCode}");
    };
    // Raised when an impression is recorded for an ad.
    ad.OnAdImpressionRecorded += () =>
    {
        Debug.Log("Picture-in-picture ad recorded an impression.");
    };
    // Raised when a click is recorded for an ad.
    ad.OnAdClicked += () =>
    {
        Debug.Log("Picture-in-picture ad recorded a click.");
    };
    // Raised when the picture-in-picture ad is displayed on the screen.
    ad.OnAdShown += () =>
    {
        Debug.Log("Picture-in-picture ad shown.");
    };
    // Raised when the picture-in-picture ad is hidden from the screen.
    ad.OnAdHidden += () =>
    {
        Debug.Log("Picture-in-picture ad hidden.");
    };
    // Raised when an ad opened full screen content.
    ad.OnAdFullScreenContentOpened += () =>
    {
        Debug.Log("Picture-in-picture ad opened full screen content.");
    };
    // Raised when the ad closed full screen content.
    ad.OnAdFullScreenContentClosed += () =>
    {
        Debug.Log("Picture-in-picture ad closed full screen content.");
    };
    // Raised when the ad failed to open full screen content.
    ad.OnAdFullScreenContentFailed += (AdError error) =>
    {
        Debug.LogWarning(
            "Picture-in-picture ad failed to open full screen " +
            "content: " + error);
    };
}

広告を非表示にする

画面からフローティング広告を削除するには、hide メソッドを呼び出します。このメソッドは、広告非表示イベントを呼び出します。

public void HidePictureInPictureAd()
{
    // Use the ad reference saved from the Load callback.
    if (_pictureInPictureAd != null)
    {
        _pictureInPictureAd.Hide();
    }
    else
    {
        Debug.Log("No ad to hide.");
    }
}

広告のリソースをクリーンアップする

メモリリークを回避するには、アプリが広告を使用し終えたときに広告オブジェクトへの参照を削除します。たとえば、アプリで広告の表示や操作が行われなくなったときです。

public void DestroyPictureInPictureAd()
{
    // Use the ad reference saved from the Load callback.
    if (_pictureInPictureAd != null)
    {
        _pictureInPictureAd.Destroy();
        _pictureInPictureAd = null;
    }
}