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

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

สิ่งที่ต้องดำเนินการก่อน

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

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

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

Android

iOS

เริ่มต้น 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)

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

เมื่อนำเสนอโฆษณา คุณต้องมี 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();
    };
}

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