使用 Audience Network,您的 Android 和 iOS 应用可以通过展示 Facebook 广告实现创收。激励视频广告是一种全屏体验,用户可选择观看视频广告以换取有价物,例如虚拟货币、应用内物品和独家内容等等。此广告体验是一段短片,且广告的结束画面会显示行动号召。用户完整观看视频后,您将收到一个回调,以向用户发放建议的奖励。
请确保先阅读 Audience Network 入门指南和 Unity 入门指南,然后再继续操作。
显示激励视频广告的第一步是在 C# 脚本中创建一个 RewardedVideoAd
对象,并附加到 GameObject
。
... using AudienceNetwork; ... public class RewardedVideoAdTest : MonoBehaviour { public void LoadRewardedVideo() { // Create the rewarded video unit with a placement ID (generate your own on the Facebook app settings). // Use different ID for each ad placement in your app. this.rewardedVideoAd = new RewardedVideoAd("YOUR_PLACEMENT_ID"); this.rewardedVideoAd.Register(this.gameObject); // Set delegates to get notified on changes or when the user interacts with the ad. this.rewardedVideoAd.RewardedVideoAdDidLoad = (delegate() { Debug.Log("RewardedVideo ad loaded."); this.isLoaded = true; }); this.rewardedVideoAd.RewardedVideoAdDidFailWithError = (delegate(string error) { Debug.Log("RewardedVideo ad failed to load with error: " + error); }); this.rewardedVideoAd.RewardedVideoAdWillLogImpression = (delegate() { Debug.Log("RewardedVideo ad logged impression."); }); this.rewardedVideoAd.RewardedVideoAdDidClick = (delegate() { Debug.Log("RewardedVideo ad clicked."); }); this.rewardedVideoAd.RewardedVideoAdDidClose = (delegate() { Debug.Log("Rewarded video ad did close."); if (this.rewardedVideoAd != null) { this.rewardedVideoAd.Dispose(); } }); // Initiate the request to load the ad. this.rewardedVideoAd.LoadAd(); } }
RewardedVideoAd
的构造函数有以下参数:
placementId
- 这个激励视频广告单元的 Audience Network 版位编号。然后,您可以执行几次回调,以订阅广告的生命周期事件。通过注册事件委派来聆听这些事件,如下列所示:
... // Set delegates to get notified on changes or when the user interacts with the ad. this.rewardedVideoAd.RewardedVideoAdDidLoad = (delegate() { Debug.Log("RewardedVideo ad loaded."); this.isLoaded = true; }); this.rewardedVideoAd.RewardedVideoAdDidFailWithError = (delegate(string error) { Debug.Log("RewardedVideo ad failed to load with error: " + error); }); this.rewardedVideoAd.RewardedVideoAdWillLogImpression = (delegate() { Debug.Log("RewardedVideo ad logged impression."); }); this.rewardedVideoAd.RewardedVideoAdDidClick = (delegate() { Debug.Log("RewardedVideo ad clicked."); }); this.rewardedVideoAd.RewardedVideoAdDidClose = (delegate() { Debug.Log("Rewarded video ad did close."); if (this.rewardedVideoAd != null) { this.rewardedVideoAd.Dispose(); } }); ...
相关内容只适用于 Android。
目前,Unity Android 游戏只支持主要的 Unity Activity
拥有 singleTask
的 launchMode
。请参阅 Unity Android 清单文档和 Android 活动文档。
由于我们使用 Activity
来显示 Interstitial
和 Rewarded Video
广告,如果用户在后台运行应用,然后使用图标(而非应用切换器)重新启动此应用,则广告活动可能会在没有正确关闭的情况下遭到终止。您可以使用下列回调,检查用户是否关闭了广告:
适用于插屏广告:
this.interstitialAd.interstitialAdDidClose = (delegate() { Debug.Log("Interstitial ad did close."); this.didClose = true; if (this.interstitialAd != null) { this.interstitialAd.Dispose(); } }); #if UNITY_ANDROID /* * Only relevant to Android. * This callback will only be triggered if the Interstitial activity has * been destroyed without being properly closed.This can happen if an * app with launchMode:singleTask (such as a Unity game) goes to * background and is then relaunched by tapping the icon. */ this.interstitialAd.interstitialAdActivityDestroyed = (delegate() { if (!this.didClose) { Debug.Log("Interstitial activity destroyed without being closed first."); Debug.Log("Game should resume."); } }); #endif
适用于奖励式视频广告:
this.rewardedVideoAd.rewardedVideoAdDidClose = (delegate() { Debug.Log("Rewarded video ad did close."); this.didClose = true; if (this.rewardedVideoAd != null) { this.rewardedVideoAd.Dispose(); } }); #if UNITY_ANDROID /* * Only relevant to Android. * This callback will only be triggered if the Rewarded Video activity * has been destroyed without being properly closed.This can happen if * an app with launchMode:singleTask (such as a Unity game) goes to * background and is then relaunched by tapping the icon. */ this.rewardedVideoAd.rewardedVideoAdActivityDestroyed = (delegate() { if (!this.didClose) { Debug.Log("Rewarded video activity destroyed without being closed first."); Debug.Log("Game should resume. User should not get a reward."); } }); #endif
在对 RewardedVideoAd
进行实例化之后,下一步骤就是加载广告。此操作需要通过调用 loadAd
方法来完成。
针对上述示例,以下说明如何对广告加载进行初始化:
... this.rewardedVideoAd.LoadAd(); ...
最后,广告加载完成后,您可以调用 Show
方法将激励视频广告呈现在屏幕上。例如,您可以为 ShowRewardedVideo
创建一个函数,并在显示广告时调用该函数:
public void ShowRewardedVideo() { if (this.isLoaded) { this.rewardedVideoAd.Show(); this.isLoaded = false; } else { Debug.Log("Ad not loaded. Click load to request an ad."); } }
这是可选项!您无需执行服务器端奖励验证即可使用奖励式视频广告。只有在您为了提高安全性,决定在自己的服务器上添加验证步骤,从而通过自己的服务器来验证奖励时才需要这么做。请向 Facebook 代表提供您的发行商端点,以便启用此功能。
如果您在服务器端管理自己的用户奖励,Facebook 可提供解决方案,帮助您使用验证技术安全地执行此项操作。我们的服务器将与指定的 https 端点通信,以验证每一次广告展示,并验证是否应发放奖励。
placementId
:Audience Network 版位编号RewardData
- RewardData
对象由代表用户编号的 UserId
以及代表真实游戏奖励的 Currency
组成。200 response
:请求有效,应发放奖励Non 200 response
:请求无效,不应发放奖励。RewardedVideoAdDidSucceed
— 仅在第 3 步收到 200 响应时触发。RewardedVideoAdDidFail
— 在第 3 步收到非 200 响应时触发。Facebook 服务器将向您的发行商端点发送的网址示例:https://www.your_end_point.com/?token=APP_SECRET&puid=USER_ID&pc=REWARD_ID&ptid=UNIQUE_TRANSACTION_ID
工作流程将如下所示:
初始化激励视频广告对象后,您需要将用户编号和奖励金额传入奖励式广告数据中,然后才能加载广告。这可以通过 RewardData
类来实现。用户编号和奖励金额均为字符串。例如:
public class RewardedVideoAdTest : MonoBehaviour { ... private bool isLoaded; private RewardedVideoAd rewardedVideoAd; public void LoadRewardedVideo() //Set the rewarded ad data RewardData rewardData = new RewardData(); rewardData.UserId = "USER_ID"; rewardData.Currency = "REWARD_ID"; // Instantiate RewardedVideoAd with reward data this.rewardedVideoAd = new RewardedVideoAd("YOUR_PLACEMENT_ID", rewardData); this.rewardedVideoAd.Register(this.gameObject); // Set delegates to get notified on changes or when the user interacts with the ad. this.rewardedVideoAd.RewardedVideoAdDidLoad = (delegate() { Debug.Log("RewardedVideo ad loaded."); this.isLoaded = true; }); this.rewardedVideoAd.RewardedVideoAdDidFailWithError = (delegate(string error) { Debug.Log("RewardedVideo ad failed to load with error: " + error); }); this.rewardedVideoAd.RewardedVideoAdWillLogImpression = (delegate() { Debug.Log("RewardedVideo ad logged impression."); }); this.rewardedVideoAd.RewardedVideoAdDidClick = (delegate() { Debug.Log("RewardedVideo ad clicked."); }); this.rewardedVideoAd.RewardedVideoAdDidClose = (delegate() { Debug.Log("Rewarded video ad did close."); if (this.rewardedVideoAd != null) { this.rewardedVideoAd.Dispose(); } }); // For S2S validation you need to register the following two callback this.rewardedVideoAd.RewardedVideoAdDidSucceed = (delegate() { Debug.Log("Rewarded video ad validated by server"); }); this.rewardedVideoAd.RewardedVideoAdDidFail = (delegate() { Debug.Log("Rewarded video ad not validated, or no response from server"); }); this.rewardedVideoAd.loadAd(); } ... }
为了通知您的应用程序奖励是否有效,除了 RewardedVideoAd
的其他回调,您还需要实施 RewardedVideoAdDidSucceed
和 RewardedVideoAdDidFail
回调。
加载广告后,您可以使用 Show
方法呈现广告,就像普通的 RewardedVideoAd
一样。
请注意,服务器验证回调可能会发生在用户关闭结束画面之后。在收到其中任意一个回调之前,您不应释放奖励式视频对象。
按照我们的指南操作,在 Unity 应用中集成不同的广告格式:
准备好让您的应用上线并开始创收后,请先确保应用符合 Audience Network 政策和 Facebook 社群标准,然后提交应用供审核。