<br />

![Picture-in-picture](https://developers.google.com/static/admob/images/format-picture-in-picture.svg)

Picture-in-picture (PiP) ads display in a floating window that stays on top of
content on the screen, such as articles, feeds, or gameplay. This format lets
users interact with your app while the ad remains visible. To display ads that
don't take over the entire screen, choose this format.

Learn more [about picture-in-picture
ads](https://support.google.com/admob/answer/17155813).


This guide covers requesting and displaying picture-in-picture ads in your app
using Google Mobile Ads Unity Plugin.

## Before you begin

Before you continue, do the following:

- [Set up Google Mobile Ads Unity Plugin](https://developers.google.com/admob/unity/quick-start.md.txt).

- Install Google Mobile Ads Unity Plugin version `11.6.0` or later.

<!-- -->

- For Android apps, [install GMA Next-Gen SDK](https://developers.google.com/admob/unity/android-next.md.txt).

<!-- -->

- [Enable test ads](https://developers.google.com/admob/unity/test-ads.md.txt) and use the following test ad unit IDs:

  ### Android

      ca-app-pub-3940256099942544/9657123429

  ### iOS

      ca-app-pub-3940256099942544/8810945611

  <br />

## Load an ad

To load a
[`PictureInPictureAd`](https://developers.google.com/admob/unity/reference/class/google-mobile-ads/api/picture-in-picture-ad.md.txt)
object, create an ad request and call the `load` method:


```c#
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 AdRequest();

    // 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;
        });
}
```

Replace <var translate="no">_adUnitId</var> with your ad unit ID.

## Show the ad

To display the picture-in-picture ad on screen, call the `show` method. The
following example displays the ad in the default position:


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

<br />

### Set the position

By default, Google Mobile Ads Unity Plugin displays a picture-in-picture ad in the bottom-right
corner of the screen when first shown, or at the last known position if
previously shown. To customize where the ad appears, specify a position when
showing the ad. The following example sets the position on top of content in the
top-left corner of the screen:


```c#
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.");
    }
}
```

<br />

For all available positions, see
[`PictureInPictureAdPosition`](https://developers.google.com/admob/unity/reference/namespace/google-mobile-ads/api.md.txt#pictureinpictureadposition).


## Listen to ad events

To handle picture-in-picture ad lifecycle events, register for ad events before
displaying your ad. These events report ad interactions, such as clicks and
impressions. These events also report picture-in-picture-specific events, such
as when the ad shows or hides:


```c#
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);
    };
}
```

<br />

## Hide the ad

To remove the floating ad from the screen, call the `hide` method. This method
invokes the ad hidden event:


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

<br />

## Clean up ad resources

To avoid memory leaks, drop your reference to the ad object when your app
finishes using the ad. For example, when your app no longer displays or
interacts with the ad again:


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

<br />

<br />