ใช้โฆษณาการแสดงภาพซ้อนภาพ (เบต้า)

เลือกแพลตฟอร์ม: Android iOS Unity

การแสดงภาพซ้อนภาพ

โฆษณาการแสดงภาพซ้อนภาพ (PiP) จะแสดงในหน้าต่างลอยที่อยู่ด้านบนสุดของเนื้อหาบนหน้าจอ เช่น บทความ ฟีด หรือการเล่นเกม รูปแบบนี้ช่วยให้ผู้ใช้โต้ตอบกับแอปได้ในขณะที่โฆษณายังคงปรากฏให้เห็น หากต้องการแสดงโฆษณา Display ที่ไม่กินพื้นที่ทั้งหน้าจอ ให้เลือกรูปแบบนี้ ดูข้อมูลเพิ่มเติม เกี่ยวกับโฆษณาการแสดงภาพซ้อนภาพ โฆษณา

คู่มือนี้ครอบคลุมการขอและแสดงโฆษณาการแสดงภาพซ้อนภาพในแอป โดยใช้ Google Mobile Ads Unity Plugin

ก่อนเริ่มต้น

โปรดทำดังนี้ก่อนดำเนินการต่อ

โหลดโฆษณา

หากต้องการโหลดออบเจ็กต์ 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 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;
        });
}

แทนที่ _adUnitId ด้วยรหัสหน่วยโฆษณา

แสดงโฆษณา

หากต้องการแสดงโฆษณาการแสดงภาพซ้อนภาพบนหน้าจอ ให้เรียกใช้เมธอด 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 ดังนี้ เมธอดนี้จะเรียกใช้เหตุการณ์ ad hidden ดังนี้

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