โฆษณาคั่นระหว่างหน้า

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

คู่มือนี้อธิบายวิธีผสานรวมโฆษณาคั่นระหว่างหน้าเข้ากับแอป Unity

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

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

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

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

เริ่มต้น 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. แสดงโฆษณาคั่นระหว่างหน้า
  3. ฟังเหตุการณ์โฆษณาคั่นระหว่างหน้า
  4. ล้างโฆษณาคั่นระหว่างหน้า
  5. โหลดโฆษณาคั่นระหว่างหน้าถัดไปล่วงหน้า

โหลดโฆษณาคั่นระหว่างหน้า

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


  // This ad unit is configured to always serve test ads.
  private string _adUnitId = "/21775744923/example/interstitial";

  private InterstitialAd _interstitialAd;

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

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

      // create our request used to load the ad.
      var adRequest = new AdManagerAdRequest();

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

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

              _interstitialAd = ad;
          });
  }

แสดงโฆษณาคั่นระหว่างหน้า

หากต้องการแสดงโฆษณาคั่นระหว่างหน้าที่โหลดไว้ ให้เรียกเมธอด Show() ใน AdManagerInterstitialAd โฆษณาสามารถแสดง 1 ครั้งต่อ โหลด ใช้เมธอด CanShowAd() เพื่อยืนยันว่าโฆษณาพร้อมแสดง

/// <summary>
/// Shows the interstitial ad.
/// </summary>
public void ShowInterstitialAd()
{
    if (_interstitialAd != null && _interstitialAd.CanShowAd())
    {
        Debug.Log("Showing interstitial ad.");
        _interstitialAd.Show();
    }
    else
    {
        Debug.LogError("Interstitial ad is not ready yet.");
    }
}

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

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

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

ล้างโฆษณาคั่นระหว่างหน้า

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

_interstitialAd.Destroy();

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

โหลดโฆษณาคั่นระหว่างหน้าถัดไปล่วงหน้า

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

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

private void RegisterReloadHandler(InterstitialAd interstitialAd)
{
    // Raised when the ad closed full screen content.
    interstitialAd.OnAdFullScreenContentClosed += ()
    {
        Debug.Log("Interstitial Ad full screen content closed.");

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

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

เหตุการณ์ของแอป

กิจกรรมของแอปช่วยให้คุณสร้างโฆษณาที่ส่งข้อความไปยังโค้ดของแอปได้ แอป สามารถดำเนินการโดยอิงจากข้อความเหล่านี้ได้

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

namespace GoogleMobileAds.Api.AdManager;

/// The App event message sent from the ad.
public class AppEvent
{
    // Name of the app event.
    string Name;
    // Argument passed from the app event.
    string Value;
}

OnAppEventReceived เพิ่มขึ้นเมื่อเกิดเหตุการณ์ของแอปในโฆษณา นี่คือ ตัวอย่างวิธีจัดการเหตุการณ์นี้ในโค้ดของคุณ

_interstitialAd.OnAppEventReceived += (AppEvent args) =>
{
    Debug.Log($"Received app event from the ad: {args.Name}, {args.Value}.");
};

นี่เป็นตัวอย่างที่แสดงวิธีเปลี่ยนสีพื้นหลังของแอป ขึ้นอยู่กับเหตุการณ์ของแอปที่มีชื่อสี ดังนี้

_interstitialAd.OnAppEventReceived += (AppEvent args) =>
{
  if (args.Name == "color")
  {
    Color color;
    if (ColorUtility.TryParseColor(arg.Value, out color))
    {
      gameObject.GetComponent<Renderer>().material.color = color;
    }
  }
};

และนี่คือครีเอทีฟโฆษณาที่เกี่ยวข้องที่ส่งเหตุการณ์ของแอปสี

<html>
<head>
  <script src="//www.gstatic.com/afma/api/v1/google_mobile_app_ads.js"></script>
  <script>
    document.addEventListener("DOMContentLoaded", function() {
      // Send a color=green event when ad loads.
      admob.events.dispatchAppEvent("color", "green");

      document.getElementById("ad").addEventListener("click", function() {
        // Send a color=blue event when ad is clicked.
        admob.events.dispatchAppEvent("color", "blue");
      });
    });
  </script>
  <style>
    #ad {
      width: 320px;
      height: 50px;
      top: 0px;
      left: 0px;
      font-size: 24pt;
      font-weight: bold;
      position: absolute;
      background: black;
      color: white;
      text-align: center;
    }
  </style>
</head>
<body>
  <div id="ad">Carpe diem!</div>
</body>
</html>

แนวทางปฏิบัติแนะนำ

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

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