广告事件
要进一步自定义您的广告行为,您可以在广告生命周期内加入许多事件,如加载、打开、关闭等等。您可以通过为相应的EventHandler注册代理来监听这些事件。其中*重要的事件是OnAdRewarded,当用户因观看视频而应获得奖励时会调用该事件。您还可以实现其他广告事件,如下所示。
using GoogleMobileAds.Api;
...
public class GoogleMobileAdsDemoscript:MonoBehaviour
{
private RewardbasedVideoAd rewardbasedVideo;
...
public void Start()
{
//Get singleton reward based video ad reference.
this.rewardbasedVideo=RewardbasedVideoAd.Instance;
//Called when an ad request has successfully loaded.
rewardbasedVideo.OnAdLoaded+=HandleRewardbasedVideoLoaded;
//Called when an ad request failed to load.
rewardbasedVideo.OnAdFailedToLoad+=HandleRewardbasedVideoFailedToLoad;
//Called when an ad is shown.
rewardbasedVideo.OnAdOpening+=HandleRewardbasedVideoOpened;
//Called when the ad starts to play.
rewardbasedVideo.OnAdStarted+=HandleRewardbasedVideoStarted;
//Called when the user should be rewarded for watching a video.
rewardbasedVideo.OnAdRewarded+=HandleRewardbasedVideoRewarded;
//Called when the ad is closed.
rewardbasedVideo.OnAdClosed+=HandleRewardbasedVideoClosed;
//Called when the ad click caused the user to leave the application.
rewardbasedVideo.OnAdLeavingApplication+=HandleRewardbasedVideoLeftApplication;
this.RequestRewardbasedVideo();
}
private void RequestRewardbasedVideo()
{
#if UNITY_ANDROID
string adUnitId="ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
string adUnitId="ca-app-pub-3940256099942544/1712485313";
#else
string adUnitId="unexpected_platform";
#endif
//Create an empty ad request.
AdRequest request=new AdRequest.Builder().Build();
//Load the rewarded video ad with the request.
this.rewardbasedVideo.LoadAd(request,adUnitId);
}
public void HandleRewardbasedVideoLoaded(object sender,EventArgs args)
{
MonoBehaviour.print("HandleRewardbasedVideoLoaded event received");
}
public void HandleRewardbasedVideoFailedToLoad(object sender,AdFailedToLoadEventArgs args)
{
MonoBehaviour.print(
"HandleRewardbasedVideoFailedToLoad event received with message:"
+args.Message);
}
public void HandleRewardbasedVideoOpened(object sender,EventArgs args)
{
MonoBehaviour.print("HandleRewardbasedVideoOpened event received");
}
public void HandleRewardbasedVideoStarted(object sender,EventArgs args)
{
MonoBehaviour.print("HandleRewardbasedVideoStarted event received");
}
public void HandleRewardbasedVideoClosed(object sender,EventArgs args)
{
MonoBehaviour.print("HandleRewardbasedVideoClosed event received");
}
public void HandleRewardbasedVideoRewarded(object sender,Reward args)
{
string type=args.Type;
double amount=args.Amount;
MonoBehaviour.print(
"HandleRewardbasedVideoRewarded event received for"
+amount.ToString()+""+type);
}
public void HandleRewardbasedVideoLeftApplication(object sender,EventArgs args)
{
MonoBehaviour.print("HandleRewardbasedVideoLeftApplication event received");
}
}