Implementare gli annunci Picture in picture (beta)

Select platform: Android iOS Unity

Picture in picture

Gli annunci Picture in picture (PIP) vengono visualizzati in una finestra mobile che rimane sopra i contenuti sullo schermo, come articoli, feed o gameplay. Questo formato consente agli utenti di interagire con la tua app mentre l'annuncio rimane visibile. Scegli questo formato per mostrare annunci display che non occupano l'intero schermo.

Questa guida spiega come richiedere e visualizzare gli annunci Picture in picture nella tua app utilizzando Google Mobile Ads Unity Plugin.

Prima di iniziare

Prima di continuare, completa queste operazioni:

Carica un annuncio

Per caricare un PictureInPictureAd oggetto, crea una richiesta di annunci e chiama il metodo di caricamento:

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

Sostituisci _adUnitId con l'ID unità pubblicitaria.

Mostra l'annuncio

Per visualizzare l'annuncio Picture in picture sullo schermo, chiama il metodo show. L'esempio seguente mostra l'annuncio nella posizione predefinita:

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

Imposta la posizione

Per impostazione predefinita, Google Mobile Ads Unity Plugin mostra un annuncio Picture in picture nell'angolo in basso a destra dello schermo alla prima visualizzazione o nell'ultima posizione nota se è stato visualizzato in precedenza. Per personalizzare la posizione dell'annuncio, specifica una posizione quando lo mostri. L'esempio seguente imposta la posizione sopra i contenuti nell'angolo in alto a sinistra dello schermo:

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

Per tutte le posizioni disponibili, consulta PictureInPictureAdPosition.

Ascolta gli eventi degli annunci

Per gestire gli eventi del ciclo di vita degli annunci Picture in picture, registrati per gli eventi degli annunci prima di visualizzare l'annuncio. Questi report Eventi segnalano le interazioni con l'annuncio, come clic e impressioni. Questi eventi segnalano anche eventi specifici di Picture in picture, ad esempio quando l'annuncio viene mostrato o nascosto:

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

Nascondi l'annuncio

Per rimuovere l'annuncio floating dallo schermo, chiama il metodo hide. Questo metodo richiama l'evento di annuncio nascosto:

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

Esegui la pulizia delle risorse degli annunci

Per evitare perdite di memoria, elimina il riferimento all'oggetto annuncio quando l'app termina di utilizzare l'annuncio. Ad esempio, quando l'app non visualizza o interagisce più con l'annuncio:

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