「Unity/GoogleMobileAds/RewardedVideo7」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→広告取得完了時に画面遷移するとアプリが落ちる対処法) |
(→広告取得完了時に画面遷移するとアプリが落ちる対処法) |
||
行115: | 行115: | ||
} | } | ||
</pre> | </pre> | ||
+ | |||
+ | ==エラー対応== | ||
+ | 以下エラーが出るとき | ||
+ | error CS0246: The type or namespace name 'EventArgs' could not be found (are you missing a using directive or an assembly reference?) | ||
+ | 以下を追加する | ||
+ | using System; |
2019年11月20日 (水) 15:37時点における版
サンプル
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/52xxxxxxx"; #elif UNITY_IPHONE string adUnitId = "ca-app-pub-3940256099942544/1712xxxxxxx"; #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"); // 2回目以降、事前ロードする場合 CreateAndLoadRewardedAd(); } // 動画が終わった時点でxで画面を閉じる前に呼ばれる 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
事前ロード
CreateAndLoadRewardedAd()をHandleRewardedAdClosedで実行すれば良い。
CreateAndLoadRewardedAd()内にrewardedAdをifでnull判定すると事前ロードができないので注意
広告取得完了時に画面遷移するとアプリが落ちる対処法
iosなら以下のようなエラーがでる時
libc++abi.dylib: terminating with uncaught exception of type Il2CppExceptionWrapper
TitleScript.csがTitleSceneのシーンにアタッチしてる時の場合の例
public void HandleRewardedAdLoaded(object sender, EventArgs args) { MonoBehaviour.print("HandleRewardedAdLoaded event received"); // csの名前じゃなくてアタッチしてるSceneの名前を入れる if (Application.loadedLevelName.Equals("TitleScene")) { if (rewardButton != null) { rewardButton.transform.position = new Vector3(2.32f, 2.40f, rewardButton.transform.position.z); } } }
エラー対応
以下エラーが出るとき
error CS0246: The type or namespace name 'EventArgs' could not be found (are you missing a using directive or an assembly reference?)
以下を追加する
using System;