「Unity/GoogleMobileAds/RewardedVideo7」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「 ==参考== 公式:https://developers.google.com/admob/unity/rewarded-ads?hl=ja」) |
(→参考) |
||
行1: | 行1: | ||
+ | ==サンプル== | ||
+ | <pre> | ||
+ | using GoogleMobileAds.Api; | ||
+ | public class GoogleMobileAdsDemoScript : MonoBehaviour | ||
+ | { | ||
+ | private RewardedAd rewardedAd; | ||
+ | public void Start() | ||
+ | { | ||
+ | CreateAndLoadRewardedAd(); | ||
+ | } | ||
+ | public void CreateAndLoadRewardedAd() | ||
+ | { | ||
+ | #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 | ||
+ | |||
+ | this.rewardedAd = new RewardedAd(adUnitId); | ||
+ | |||
+ | // Called when an ad request has successfully loaded. | ||
+ | this.rewardedAd.OnAdLoaded += HandleRewardedAdLoaded; | ||
+ | // Called when an ad request failed to load. | ||
+ | this.rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad; | ||
+ | // Called when an ad is shown. | ||
+ | this.rewardedAd.OnAdOpening += HandleRewardedAdOpening; | ||
+ | // Called when an ad request failed to show. | ||
+ | this.rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow; | ||
+ | // Called when the user should be rewarded for interacting with the ad. | ||
+ | this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward; | ||
+ | // Called when the ad is closed. | ||
+ | this.rewardedAd.OnAdClosed += HandleRewardedAdClosed; | ||
+ | |||
+ | // Create an empty ad request. | ||
+ | AdRequest request = new AdRequest.Builder().Build(); | ||
+ | // Load the rewarded ad with the request. | ||
+ | this.rewardedAd.LoadAd(request); | ||
+ | } | ||
+ | |||
+ | public void HandleRewardedAdLoaded(object sender, EventArgs args) | ||
+ | { | ||
+ | MonoBehaviour.print("HandleRewardedAdLoaded event received"); | ||
+ | } | ||
+ | |||
+ | public void HandleRewardedAdFailedToLoad(object sender, AdErrorEventArgs args) | ||
+ | { | ||
+ | MonoBehaviour.print( | ||
+ | "HandleRewardedAdFailedToLoad event received with message: " | ||
+ | + args.Message); | ||
+ | } | ||
+ | |||
+ | public void HandleRewardedAdOpening(object sender, EventArgs args) | ||
+ | { | ||
+ | MonoBehaviour.print("HandleRewardedAdOpening event received"); | ||
+ | } | ||
+ | |||
+ | public void HandleRewardedAdFailedToShow(object sender, AdErrorEventArgs args) | ||
+ | { | ||
+ | MonoBehaviour.print( | ||
+ | "HandleRewardedAdFailedToShow event received with message: " | ||
+ | + args.Message); | ||
+ | } | ||
+ | |||
+ | public void HandleRewardedAdClosed(object sender, EventArgs args) | ||
+ | { | ||
+ | MonoBehaviour.print("HandleRewardedAdClosed event received"); | ||
+ | // 事前ロードする場合 | ||
+ | CreateAndLoadRewardedAd() | ||
+ | } | ||
+ | |||
+ | public void HandleUserEarnedReward(object sender, Reward args) | ||
+ | { | ||
+ | string type = args.Type; | ||
+ | double amount = args.Amount; | ||
+ | MonoBehaviour.print( | ||
+ | "HandleRewardedAdRewarded event received for " | ||
+ | + amount.ToString() + " " + type); | ||
+ | } | ||
+ | private void ShowAd() | ||
+ | { | ||
+ | if (this.rewardedAd.IsLoaded()) { | ||
+ | this.rewardedAd.Show(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
==参考== | ==参考== | ||
公式:https://developers.google.com/admob/unity/rewarded-ads?hl=ja | 公式:https://developers.google.com/admob/unity/rewarded-ads?hl=ja |
2019年5月31日 (金) 18:59時点における版
サンプル
using GoogleMobileAds.Api; public class GoogleMobileAdsDemoScript : MonoBehaviour { private RewardedAd rewardedAd; public void Start() { CreateAndLoadRewardedAd(); } public void CreateAndLoadRewardedAd() { #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 this.rewardedAd = new RewardedAd(adUnitId); // Called when an ad request has successfully loaded. this.rewardedAd.OnAdLoaded += HandleRewardedAdLoaded; // Called when an ad request failed to load. this.rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad; // Called when an ad is shown. this.rewardedAd.OnAdOpening += HandleRewardedAdOpening; // Called when an ad request failed to show. this.rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow; // Called when the user should be rewarded for interacting with the ad. this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward; // Called when the ad is closed. this.rewardedAd.OnAdClosed += HandleRewardedAdClosed; // Create an empty ad request. AdRequest request = new AdRequest.Builder().Build(); // Load the rewarded ad with the request. this.rewardedAd.LoadAd(request); } public void HandleRewardedAdLoaded(object sender, EventArgs args) { MonoBehaviour.print("HandleRewardedAdLoaded event received"); } public void HandleRewardedAdFailedToLoad(object sender, AdErrorEventArgs args) { MonoBehaviour.print( "HandleRewardedAdFailedToLoad event received with message: " + args.Message); } public void HandleRewardedAdOpening(object sender, EventArgs args) { MonoBehaviour.print("HandleRewardedAdOpening event received"); } public void HandleRewardedAdFailedToShow(object sender, AdErrorEventArgs args) { MonoBehaviour.print( "HandleRewardedAdFailedToShow event received with message: " + args.Message); } public void HandleRewardedAdClosed(object sender, EventArgs args) { MonoBehaviour.print("HandleRewardedAdClosed event received"); // 事前ロードする場合 CreateAndLoadRewardedAd() } public void HandleUserEarnedReward(object sender, Reward args) { string type = args.Type; double amount = args.Amount; MonoBehaviour.print( "HandleRewardedAdRewarded event received for " + amount.ToString() + " " + type); } private void ShowAd() { if (this.rewardedAd.IsLoaded()) { this.rewardedAd.Show(); } } }
参考
公式:https://developers.google.com/admob/unity/rewarded-ads?hl=ja