橫幅廣告

選取平台: Android iOS Unity Flutter

橫幅檢視畫面是佔用螢幕空間的矩形圖片或文字廣告。 使用者與應用程式互動時,這類廣告會固定顯示在畫面上,且經過一段時間會自動重新整理。如果您剛開始接觸行動廣告,建議先從這些廣告活動著手。個案研究

本指南說明如何在 Unity 應用程式中整合橫幅檢視區塊,除了程式碼片段和操作說明外,還提供橫幅大小設定資訊,以及其他資源的連結。

必要條件

一律使用測試廣告進行測試

下列程式碼範例包含廣告單元 ID,可用於要求測試廣告。這個版本經過特別設定,可針對每個請求傳回測試廣告,而非正式版廣告,因此可安心使用。

不過,在 AdMob 網頁介面中註冊應用程式,並建立要在應用程式中使用的廣告單元 ID 後,請在開發期間將裝置明確設為測試裝置

Android

ca-app-pub-3940256099942544/6300978111

iOS

ca-app-pub-3940256099942544/2934735716

初始化 Mobile Ads SDK

應用程式必須先呼叫 MobileAds.Initialize(),初始化 Mobile Ads SDK,才能載入廣告。這項操作只需執行一次,最好是在應用程式啟動時執行。

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

如果您使用中介服務,請等到回呼發生後再載入廣告,確保所有中介服務轉接程式都已初始化。

BannerView 範例

下方的程式碼範例詳細說明如何使用橫幅檢視區塊。在這個範例中,您會建立橫幅檢視區塊的例項、使用 AdRequest 將廣告載入橫幅檢視區塊,然後處理生命週期事件,擴充橫幅檢視區塊的功能。

建立橫幅檢視畫面

使用橫幅檢視區塊的第一步,是在附加至 GameObject 的 C# 指令碼中,建立橫幅檢視區塊的執行個體。


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

  BannerView _bannerView;

  /// <summary>
  /// Creates a 320x50 banner view at top of the screen.
  /// </summary>
  public void CreateBannerView()
  {
      Debug.Log("Creating banner view");

      // If we already have a banner, destroy the old one.
      if (_bannerView != null)
      {
          DestroyAd();
      }

      // Create a 320x50 banner at top of the screen
      _bannerView = new BannerView(_adUnitId, AdSize.Banner, AdPosition.Top);
  }

BannerView 的建構函式具有下列參數:

  • adUnitId:要從哪個廣告單元 ID 載入廣告 BannerView
  • AdSize:您想使用的廣告尺寸。詳情請參閱「橫幅大小」。
  • AdPosition:應放置橫幅檢視區塊的位置。AdPosition 列舉會列出有效的廣告位置值。

請注意,不同平台使用的廣告單元有所不同。您必須使用 iOS 廣告單元在 iOS 上提出廣告請求,並使用 Android 廣告單元在 Android 上提出請求。

(選用) 建立自訂位置的橫幅檢視區塊

如要比 AdPosition 值更精確地控制 BannerView 在螢幕上的放置位置,請使用以 x 和 y 座標做為參數的建構函式:

// Create a 320x50 banner views at coordinate (0,50) on screen.
_bannerView = new BannerView(_adUnitId, AdSize.Banner, 0, 50);

BannerView 的左上角位於傳遞至建構函式的 x 和 y 值,原點是畫面的左上角。

(選用) 建立自訂大小的橫幅檢視區塊

除了使用 AdSize 常數,您也可以為廣告指定自訂大小:

// Use the AdSize argument to set a custom size for the ad.
AdSize adSize = new AdSize(250, 250);
_bannerView = new BannerView(_adUnitId, adSize, AdPosition.Bottom);

載入橫幅廣告

如要載入廣告,請建立 AdRequest 並傳遞至 LoadAd() 方法。

/// <summary>
/// Creates the banner view and loads a banner ad.
/// </summary>
public void LoadAd()
{
    // create an instance of a banner view first.
    if(_bannerView == null)
    {
        CreateBannerView();
    }

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

    // send the request to load the ad.
    Debug.Log("Loading banner ad.");
    _bannerView.LoadAd(adRequest);
}

監聽橫幅檢視事件

如要自訂廣告的行為,您可以連結廣告生命週期中的多個事件,例如載入、開啟或關閉。如要監聽這些事件,請註冊委派:

/// <summary>
/// listen to events the banner view may raise.
/// </summary>
private void ListenToAdEvents()
{
    // Raised when an ad is loaded into the banner view.
    _bannerView.OnBannerAdLoaded += () =>
    {
        Debug.Log("Banner view loaded an ad with response : "
            + _bannerView.GetResponseInfo());
    };
    // Raised when an ad fails to load into the banner view.
    _bannerView.OnBannerAdLoadFailed += (LoadAdError error) =>
    {
        Debug.LogError("Banner view failed to load an ad with error : "
            + error);
    };
    // Raised when the ad is estimated to have earned money.
    _bannerView.OnAdPaid += (AdValue adValue) =>
    {
        Debug.Log(String.Format("Banner view paid {0} {1}.",
            adValue.Value,
            adValue.CurrencyCode));
    };
    // Raised when an impression is recorded for an ad.
    _bannerView.OnAdImpressionRecorded += () =>
    {
        Debug.Log("Banner view recorded an impression.");
    };
    // Raised when a click is recorded for an ad.
    _bannerView.OnAdClicked += () =>
    {
        Debug.Log("Banner view was clicked.");
    };
    // Raised when an ad opened full screen content.
    _bannerView.OnAdFullScreenContentOpened += () =>
    {
        Debug.Log("Banner view full screen content opened.");
    };
    // Raised when the ad closed full screen content.
    _bannerView.OnAdFullScreenContentClosed += () =>
    {
        Debug.Log("Banner view full screen content closed.");
    };
}

銷毀橫幅檢視畫面

使用橫幅檢視畫面完畢後,請務必呼叫 Destroy() 來釋出資源。

/// <summary>
/// Destroys the banner view.
/// </summary>
public void DestroyAd()
{
    if (_bannerView != null)
    {
        Debug.Log("Destroying banner view.");
        _bannerView.Destroy();
        _bannerView = null;
    }
}

大功告成!您的應用程式現在可以顯示橫幅廣告。

重新整理廣告

如果您已將廣告單元設為重新整理,廣告載入失敗時,就不必再次要求廣告。Google Mobile Ads SDK 會遵守您在 AdMob 使用者介面中指定的任何重新整理頻率。如果尚未啟用重新整理功能,請發出新的要求。如要進一步瞭解廣告單元重新整理功能,例如設定重新整理頻率,請參閱「使用橫幅廣告自動重新整理功能」。

下表列出標準橫幅尺寸。

以 dp 為單位的尺寸 (寬 x 高) 說明 可用性 AdSize 常數
320x50 標準橫幅廣告 手機和平板電腦 BANNER
320x100 大型橫幅 手機和平板電腦 LARGE_BANNER
300x250 IAB 中矩形廣告 手機和平板電腦 MEDIUM_RECTANGLE
468x60 IAB 全尺寸橫幅 平板電腦 FULL_BANNER
728x90 IAB 超級橫幅廣告 平板電腦 LEADERBOARD
提供的寬度 x 彈性高度 自動調整橫幅廣告 手機和平板電腦
螢幕寬度 x 32|50|90 智慧橫幅 手機和平板電腦 SMART_BANNER
進一步瞭解自動調整橫幅廣告,這項功能旨在取代智慧型橫幅廣告

其他資源