應用程式開啟頁面廣告是一種特殊廣告格式,適合希望藉由應用程式載入畫面賺取收益的發布商。應用程式開啟頁面廣告會在使用者將應用程式切換至前景時出現,而且使用者隨時可以關閉。
應用程式開啟頁面廣告會自動保留一小塊畫面,向使用者顯示應用程式的品牌資訊。以下是應用程式開啟頁面廣告的範例:
必要條件
- 完成入門指南。
- Unity 外掛程式 7.1.0 以上版本。
一律使用測試廣告進行測試
下列程式碼範例包含廣告單元 ID,可用於要求測試廣告。這個版本經過特別設定,可針對每個請求傳回測試廣告,而非正式版廣告,因此可安心使用。
不過,在 AdMob 網頁介面中註冊應用程式,並建立要在應用程式中使用的廣告單元 ID 後,請在開發期間將裝置明確設為測試裝置。
Android
ca-app-pub-3940256099942544/9257395921
iOS
ca-app-pub-3940256099942544/5575463023
導入作業
整合應用程式開啟頁面廣告的主要步驟如下:
- 建立公用程式類別
- 載入應用程式開啟頁面廣告
- 監聽應用程式開啟頁面廣告事件
- 考慮廣告效期
- 監聽應用程式狀態事件
- 顯示應用程式開啟頁面廣告
- 清除應用程式開啟頁面廣告
- 預先載入下一個應用程式開啟頁面廣告
建立公用程式類別
建立名為 AppOpenAdController
的新類別,用於載入廣告。這個類別會控管執行個體變數,追蹤每個平台載入的廣告和廣告單元 ID。
using System;
using UnityEngine;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;
/// <summary>
/// Demonstrates how to use the Google Mobile Ads app open ad format.
/// </summary>
[AddComponentMenu("GoogleMobileAds/Samples/AppOpenAdController")]
public class AppOpenAdController : MonoBehaviour
{
// These ad units are configured to always serve test ads.
#if UNITY_ANDROID
private string _adUnitId = "ca-app-pub-3940256099942544/9257395921";
#elif UNITY_IPHONE
string _adUnitId = "ca-app-pub-3940256099942544/5575463023";
#else
private string _adUnitId = "unused";
#endif
public bool IsAdAvailable
{
get
{
return _appOpenAd != null;
}
}
public void Start()
{
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize((InitializationStatus initStatus) =>
{
// This callback is called once the MobileAds SDK is initialized.
});
}
/// <summary>
/// Loads the app open ad.
/// </summary>
public void LoadAppOpenAd()
{
}
/// <summary>
/// Shows the app open ad.
/// </summary>
public void ShowAppOpenAd()
{
}
}
載入應用程式開啟頁面廣告
如要載入應用程式開啟頁面廣告,請使用 AppOpenAd
類別的靜態 Load()
方法。載入方法需要廣告單元 ID、AdRequest
物件和完成處理常式,廣告載入成功或失敗時,系統會呼叫該常式。載入的 AppOpenAd
物件會以參數形式提供給完成處理常式。以下範例說明如何載入 AppOpenAd
。
// These ad units are configured to always serve test ads.
#if UNITY_ANDROID
private string _adUnitId = "ca-app-pub-3940256099942544/9257395921";
#elif UNITY_IPHONE
string _adUnitId = "ca-app-pub-3940256099942544/5575463023";
#else
private string _adUnitId = "unused";
#endif
private AppOpenAd appOpenAd;
/// <summary>
/// Loads the app open ad.
/// </summary>
public void LoadAppOpenAd()
{
// Clean up the old ad before loading a new one.
if (appOpenAd != null)
{
appOpenAd.Destroy();
appOpenAd = null;
}
Debug.Log("Loading the app open ad.");
// Create our request used to load the ad.
var adRequest = new AdRequest();
// send the request to load the ad.
AppOpenAd.Load(_adUnitId, adRequest,
(AppOpenAd ad, LoadAdError error) =>
{
// if error is not null, the load request failed.
if (error != null || ad == null)
{
Debug.LogError("app open ad failed to load an ad " +
"with error : " + error);
return;
}
Debug.Log("App open ad loaded with response : "
+ ad.GetResponseInfo());
appOpenAd = ad;
RegisterEventHandlers(ad);
});
}
監聽應用程式開啟頁面廣告事件
如要進一步自訂廣告的行為,您可以連結廣告生命週期中的多個事件:開啟、關閉等。如要監聽這些事件,請註冊委派項目,如下所示。
private void RegisterEventHandlers(AppOpenAd ad)
{
// Raised when the ad is estimated to have earned money.
ad.OnAdPaid += (AdValue adValue) =>
{
Debug.Log(String.Format("App open ad paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));
};
// Raised when an impression is recorded for an ad.
ad.OnAdImpressionRecorded += () =>
{
Debug.Log("App open ad recorded an impression.");
};
// Raised when a click is recorded for an ad.
ad.OnAdClicked += () =>
{
Debug.Log("App open ad was clicked.");
};
// Raised when an ad opened full screen content.
ad.OnAdFullScreenContentOpened += () =>
{
Debug.Log("App open ad full screen content opened.");
};
// Raised when the ad closed full screen content.
ad.OnAdFullScreenContentClosed += () =>
{
Debug.Log("App open ad full screen content closed.");
};
// Raised when the ad failed to open full screen content.
ad.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("App open ad failed to open full screen content " +
"with error : " + error);
};
}
考慮廣告效期
為避免顯示已失效的廣告,請在 AppOpenAdController
中新增方法,檢查廣告目前已載入多久。然後使用該方法檢查廣告是否仍有效。
應用程式開啟頁面廣告的逾時時間為 4 小時。將載入時間快取到 _expireTime
變數中。
// send the request to load the ad.
AppOpenAd.Load(_adUnitId, adRequest,
(AppOpenAd ad, LoadAdError error) =>
{
// If the operation failed, an error is returned.
if (error != null || ad == null)
{
Debug.LogError("App open ad failed to load an ad with error : " +
error);
return;
}
// If the operation completed successfully, no error is returned.
Debug.Log("App open ad loaded with response : " + ad.GetResponseInfo());
// App open ads can be preloaded for up to 4 hours.
_expireTime = DateTime.Now + TimeSpan.FromHours(4);
_appOpenAd = ad;
});
更新 IsAdAvailable
屬性,勾選 _expireTime
確認載入的廣告是否仍有效。
public bool IsAdAvailable
{
get
{
return _appOpenAd != null
&& _appOpenAd.IsLoaded()
&& DateTime.Now < _expireTime;
}
}
監聽應用程式狀態事件
使用 AppStateEventNotifier
監聽應用程式前景和背景事件。每當應用程式進入前景或背景時,這個類別就會引發 AppStateChanged
事件。
private void Awake()
{
// Use the AppStateEventNotifier to listen to application open/close events.
// This is used to launch the loaded ad when we open the app.
AppStateEventNotifier.AppStateChanged += OnAppStateChanged;
}
private void OnDestroy()
{
// Always unlisten to events when complete.
AppStateEventNotifier.AppStateChanged -= OnAppStateChanged;
}
處理 AppState.Foreground
狀態和 IsAdAvailable
時,如果 IsAdAvailable
為 true
,我們會呼叫 ShowAppOpenAd()
來顯示廣告。
private void OnAppStateChanged(AppState state)
{
Debug.Log("App State changed to : "+ state);
// if the app is Foregrounded and the ad is available, show it.
if (state == AppState.Foreground)
{
if (IsAdAvailable)
{
ShowAppOpenAd();
}
}
}
顯示應用程式開啟頁面廣告
如要顯示已載入的應用程式開啟頁面廣告,請對 AppOpenAd
例項呼叫 Show()
方法。每次載入只能顯示一次廣告。使用 CanShowAd()
方法,確認廣告是否已準備好放送。
/// <summary>
/// Shows the app open ad.
/// </summary>
public void ShowAppOpenAd()
{
if (appOpenAd != null && appOpenAd.CanShowAd())
{
Debug.Log("Showing app open ad.");
appOpenAd.Show();
}
else
{
Debug.LogError("App open ad is not ready yet.");
}
}
清除應用程式開啟頁面廣告
使用完 AppOpenAd
後,請務必先呼叫 Destroy()
方法,再捨棄對該物件的參照:
appOpenAd.Destroy();
這會通知外掛程式該物件不再使用,因此可回收其占用的記憶體。如果未呼叫這個方法,就會導致記憶體流失。
預先載入下一個應用程式開啟頁面廣告
AppOpenAd
是只能使用一次的物件。也就是說,應用程式開啟廣告顯示後,該物件就無法再次使用。如要要求開啟另一個應用程式廣告,您必須建立新的 AppOpenAd
物件。
如要準備應用程式開啟頁面廣告,以便在下一次曝光機會中顯示,請在系統發出 OnAdFullScreenContentClosed
或 OnAdFullScreenContentFailed
廣告事件後,預先載入應用程式開啟頁面廣告。
private void RegisterReloadHandler(AppOpenAd ad)
{
...
// Raised when the ad closed full screen content.
ad.OnAdFullScreenContentClosed += ()
{
Debug.Log("App open ad full screen content closed.");
// Reload the ad so that we can show another as soon as possible.
LoadAppOpenAd();
};
// Raised when the ad failed to open full screen content.
ad.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("App open ad failed to open full screen content " +
"with error : " + error);
// Reload the ad so that we can show another as soon as possible.
LoadAppOpenAd();
};
}
冷啟動和載入畫面
目前為止,文件都假設您只會在使用者將應用程式從記憶體中恢復到前景時,顯示應用程式開啟頁面廣告。如果應用程式啟動時未在記憶體中暫停,就會發生「冷啟動」。
舉例來說,使用者第一次開啟應用程式時,就是冷啟動。 冷啟動時,您不會有先前載入的應用程式開啟頁面廣告,因此無法立即顯示。如果您要求廣告後,廣告過一段時間才傳回,使用者可能會在看到與內容無關的廣告前,短暫使用您的應用程式。這會造成使用者體驗不佳,建議您不要採用。
如要在冷啟動時放送應用程式開啟頁面廣告,建議使用載入畫面載入遊戲或應用程式素材資源,並只在載入畫面中顯示廣告。如果應用程式已完成載入,並將使用者帶往應用程式的主要內容,請勿顯示廣告。
最佳做法
應用程式開啟頁面廣告可協助您在應用程式首次啟動和切換期間,透過應用程式載入畫面營利,但請務必遵守下列最佳做法,確保使用者享有良好的應用程式體驗。
- 等使用者用過應用程式幾次之後,再放送第一則應用程式開啟頁面廣告。
- 在使用者等待應用程式載入時,放送應用程式開啟頁面廣告。
- 如果載入畫面顯示在應用程式開啟頁面廣告的背景中,且載入畫面在廣告關閉前就完成載入,請在
OnAdDidDismissFullScreenContent
事件處理常式中關閉載入畫面。 - 在 iOS 平台上,
AppStateEventNotifier
會例項化AppStateEventClient GameObject
。事件觸發時需要這個GameObject
,因此請勿刪除。如果GameObject
遭到毀損,系統就會停止觸發事件。
其他資源
- HelloWorld 範例: 所有廣告格式的最簡單導入方式。