โฆษณาคั่นระหว่างหน้าที่มีการให้รางวัล (เบต้า)

โฆษณาคั่นระหว่างหน้าที่มีการให้รางวัลเป็น รูปแบบโฆษณาที่เสนอสิ่งจูงใจซึ่งให้คุณเสนอรางวัลสำหรับโฆษณาที่แสดง โดยอัตโนมัติระหว่างการเปลี่ยนหน้าตามปกติของแอป สิ่งที่แตกต่างจากโฆษณาที่มีการให้รางวัลคือ ที่จำเป็นต่อการเลือกใช้เพื่อดูโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัล

ข้อกำหนดเบื้องต้น

ทดสอบด้วยโฆษณาทดสอบเสมอ

โค้ดตัวอย่างต่อไปนี้มีรหัสหน่วยโฆษณาที่คุณใช้เพื่อขอได้ โฆษณาทดสอบ โดยได้รับการกำหนดค่าเป็นพิเศษให้ส่งคืนโฆษณาทดสอบ โฆษณาในเวอร์ชันที่ใช้งานจริงสำหรับทุกคำขอ ทำให้ใช้งานได้อย่างปลอดภัย

อย่างไรก็ตาม หลังจากที่คุณลงทะเบียนแอปใน เว็บอินเทอร์เฟซของ AdMob และสร้างหน่วยโฆษณาของคุณเอง รหัสสำหรับใช้ในแอป ให้กำหนดค่าอุปกรณ์เป็นการทดสอบให้ชัดเจน อุปกรณ์ในระหว่าง ที่กำลังพัฒนา

Android

ca-app-pub-3940256099942544/5354046379

iOS

ca-app-pub-3940256099942544/6978759866

เริ่มต้น SDK โฆษณาในอุปกรณ์เคลื่อนที่

ก่อนที่จะโหลดโฆษณา ให้แอปของคุณเริ่มต้น SDK โฆษณาในอุปกรณ์เคลื่อนที่โดยการเรียก MobileAds.Initialize(). ขั้นตอนนี้ต้องทำเพียงครั้งเดียว และควรทำเมื่อเปิดแอป

using GoogleMobileAds;
using GoogleMobileAds.Api;

public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    public void Start()
    {
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize((InitializationStatus initStatus) =>
        {
            // This callback is called once the MobileAds SDK is initialized.
        });
    }
}

หากคุณกำลังใช้สื่อกลาง ให้รอจนกว่าจะมีการเรียกกลับก่อนที่จะโหลดโฆษณาเป็น วิธีนี้จะทำให้อะแดปเตอร์สื่อกลางทั้งหมดเริ่มต้นทำงานแล้ว

การใช้งาน

ขั้นตอนหลักในการผสานรวมโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัลมีดังนี้

  1. โหลดโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัล
  2. [ไม่บังคับ] ตรวจสอบ Callback ของการยืนยันฝั่งเซิร์ฟเวอร์ (SSV)
  3. แสดงโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัลที่มี Callback รางวัล
  4. ฟังเหตุการณ์โฆษณาคั่นระหว่างหน้าที่มีการให้รางวัล
  5. ล้างโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัล
  6. โหลดโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัลรายการถัดไปล่วงหน้า

โหลดโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัล

โหลดโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัลได้โดยใช้ Load() แบบคงที่ ในคลาส RewardedInterstitialAd วิธีโหลดต้องมีโฆษณา รหัสหน่วย ออบเจ็กต์ AdRequest และการเติมค่า ซึ่งจะเรียกใช้เมื่อการโหลดโฆษณาสำเร็จหรือล้มเหลว โหลดแล้ว มีการระบุออบเจ็กต์ RewardedInterstitialAd เป็นพารามิเตอร์ในการดำเนินการให้เสร็จสมบูรณ์ เครื่องจัดการ ตัวอย่างด้านล่างแสดงวิธีโหลด RewardedInterstitialAd


  // These ad units are configured to always serve test ads.
#if UNITY_ANDROID
  private string _adUnitId = "ca-app-pub-3940256099942544/5354046379";
#elif UNITY_IPHONE
  private string _adUnitId = "ca-app-pub-3940256099942544/6978759866";
#else
  private string _adUnitId = "unused";
#endif

  private RewardedInterstitialAd _rewardedInterstitialAd;

  /// <summary>
  /// Loads the rewarded interstitial ad.
  /// </summary>
  public void LoadRewardedInterstitialAd()
  {
      // Clean up the old ad before loading a new one.
      if (_rewardedInterstitialAd != null)
      {
            _rewardedInterstitialAd.Destroy();
            _rewardedInterstitialAd = null;
      }

      Debug.Log("Loading the rewarded interstitial ad.");

      // create our request used to load the ad.
      var adRequest = new AdRequest();
      adRequest.Keywords.Add("unity-admob-sample");

      // send the request to load the ad.
      RewardedInterstitialAd.Load(_adUnitId, adRequest,
          (RewardedInterstitialAd ad, LoadAdError error) =>
          {
              // if error is not null, the load request failed.
              if (error != null || ad == null)
              {
                  Debug.LogError("rewarded interstitial ad failed to load an ad " +
                                 "with error : " + error);
                  return;
              }

              Debug.Log("Rewarded interstitial ad loaded with response : "
                        + ad.GetResponseInfo());

              _rewardedInterstitialAd = ad;
          });
  }

[ไม่บังคับ] ตรวจสอบ Callback ของการยืนยันฝั่งเซิร์ฟเวอร์ (SSV)

แอปที่ต้องใช้ข้อมูลเพิ่มเติมในการยืนยันฝั่งเซิร์ฟเวอร์ Callback ควรใช้ฟีเจอร์ข้อมูลที่กำหนดเองของโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัล ระบบจะส่งค่าสตริงที่ตั้งค่าไว้ในออบเจ็กต์โฆษณาที่มีการให้รางวัลไปยัง custom_data พารามิเตอร์การค้นหาของ Callback SSV หากไม่ได้ตั้งค่าข้อมูลที่กำหนดเองไว้ ค่าพารามิเตอร์การค้นหา custom_data จะไม่รวมอยู่ใน Callback ของ SSV

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีตั้งค่าตัวเลือก SSV หลังจาก โหลดโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัลแล้ว

// send the request to load the ad.
RewardedInterstitialAd.Load(_adUnitId,
                            adRequest,
                            (RewardedInterstitialAd ad, LoadAdError error) =>
    {
        // If the operation failed, an error is returned.
        if (error != null || ad == null)
        {
            Debug.LogError("Rewarded interstitial ad failed to load an ad " +
                           " with error : " + error);
            return;
        }

        // If the operation completed successfully, no error is returned.
        Debug.Log("Rewarded interstitial ad loaded with response : " +
                   ad.GetResponseInfo());
        
        // Create and pass the SSV options to the rewarded ad.
        var options = new ServerSideVerificationOptions
                              .Builder()
                              .SetCustomData("SAMPLE_CUSTOM_DATA_STRING")
                              .Build()
        ad.SetServerSideVerificationOptions(options);
        
});

หากต้องการตั้งค่าสตริงรางวัลที่กำหนดเอง คุณต้องดำเนินการก่อนที่จะแสดง โฆษณาของคุณ

แสดงโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัลที่มี Callback รางวัล

เมื่อนำเสนอโฆษณาของคุณ คุณต้องให้การเรียกกลับเพื่อจัดการรางวัลสำหรับ ผู้ใช้รายนั้น โฆษณาจะแสดงได้ 1 ครั้งต่อการโหลดเท่านั้น ใช้เมธอด CanShowAd() เพื่อ ยืนยันว่าโฆษณาพร้อมที่จะแสดงแล้ว

โค้ดต่อไปนี้เป็นวิธีที่ดีที่สุดในการแสดงรางวัลที่มีการให้รางวัล โฆษณาคั่นระหว่างหน้า

public void ShowRewardedInterstitialAd()
{
    const string rewardMsg =
        "Rewarded interstitial ad rewarded the user. Type: {0}, amount: {1}.";

    if (rewardedInterstitialAd != null && rewardedInterstitialAd.CanShowAd())
    {
        rewardedInterstitialAd.Show((Reward reward) =>
        {
            // TODO: Reward the user.
            Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount));
        });
    }
}

ฟังเหตุการณ์โฆษณาคั่นระหว่างหน้าที่มีการให้รางวัล

หากต้องการปรับแต่งลักษณะการทำงานของโฆษณาเพิ่มเติม คุณสามารถดึงดูด ในวงจรของโฆษณา ฟังกิจกรรมเหล่านี้โดยการลงทะเบียนผู้รับมอบสิทธิ์ ดังที่แสดงด้านล่าง

private void RegisterEventHandlers(RewardedInterstitialAd ad)
{
    // Raised when the ad is estimated to have earned money.
    ad.OnAdPaid += (AdValue adValue) =>
    {
        Debug.Log(String.Format("Rewarded interstitial ad paid {0} {1}.",
            adValue.Value,
            adValue.CurrencyCode));
    };
    // Raised when an impression is recorded for an ad.
    ad.OnAdImpressionRecorded += () =>
    {
        Debug.Log("Rewarded interstitial ad recorded an impression.");
    };
    // Raised when a click is recorded for an ad.
    ad.OnAdClicked += () =>
    {
        Debug.Log("Rewarded interstitial ad was clicked.");
    };
    // Raised when an ad opened full screen content.
    ad.OnAdFullScreenContentOpened += () =>
    {
        Debug.Log("Rewarded interstitial ad full screen content opened.");
    };
    // Raised when the ad closed full screen content.
    ad.OnAdFullScreenContentClosed += () =>
    {
        Debug.Log("Rewarded interstitial ad full screen content closed.");
    };
    // Raised when the ad failed to open full screen content.
    ad.OnAdFullScreenContentFailed += (AdError error) =>
    {
        Debug.LogError("Rewarded interstitial ad failed to open " +
                       "full screen content with error : " + error);
    };
}

ล้างโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัล

เมื่อใช้ RewardedInterstitialAd เสร็จแล้ว อย่าลืมเรียก Destroy() ก่อนที่จะทิ้งการอ้างอิงถึงวิธีการนั้น:

_rewardedInterstitialAd.Destroy();

การดำเนินการนี้จะแจ้งปลั๊กอินว่าไม่ได้ใช้ออบเจ็กต์แล้ว รวมถึงหน่วยความจำของออบเจ็กต์ สามารถอ้างสิทธิ์คืนได้ หากไม่เรียกใช้เมธอดนี้ หน่วยความจำจะรั่วไหล

โหลดโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัลรายการถัดไปล่วงหน้า

RewardedInterstitialAd เป็นออบเจ็กต์แบบใช้ครั้งเดียว หมายความว่าเมื่อมีการได้รับรางวัล โฆษณาคั่นระหว่างหน้าแสดงขึ้น ออบเจ็กต์นั้นไม่สามารถใช้ได้อีก หากต้องการส่งคำขอใหม่ โฆษณาคั่นระหว่างหน้าที่มีการให้รางวัล คุณจะต้องโหลด RewardedInterstitialAd ใหม่ ออบเจ็กต์

วิธีเตรียมโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัลสำหรับโอกาสในการแสดงผลครั้งต่อไปมีดังนี้ โหลดโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัลล่วงหน้าเมื่อ OnAdFullScreenContentClosed หรือ เกิดเหตุการณ์โฆษณา OnAdFullScreenContentFailed เหตุการณ์

private void RegisterReloadHandler(RewardedInterstitialAd ad)
{
    // Raised when the ad closed full screen content.
    ad.OnAdFullScreenContentClosed += ()
    {
        Debug.Log("Rewarded interstitial ad full screen content closed.");

        // Reload the ad so that we can show another as soon as possible.
        LoadRewardedInterstitialAd();
    };
    // Raised when the ad failed to open full screen content.
    ad.OnAdFullScreenContentFailed += (AdError error) =>
    {
        Debug.LogError("Rewarded interstitial ad failed to open " +
                       "full screen content with error : " + error);

        // Reload the ad so that we can show another as soon as possible.
        LoadRewardedInterstitialAd();
    };
}

แหล่งข้อมูลเพิ่มเติม