facebook twitter hatena line google mixi email

「Unity/GoogleMobileAds/RewardedVideo」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(事前ロード)
行96: 行96:
  
 
CreateAndLoadRewardedAd()内にrewardedAdをifでnull判定すると事前ロードができないので注意
 
CreateAndLoadRewardedAd()内にrewardedAdをifでnull判定すると事前ロードができないので注意
 +
 +
==広告取得完了時に画面遷移するとアプリが落ちる対処法==
 +
libc++abi.dylib: terminating with uncaught exception of type Il2CppExceptionWrapper
 +
TitleScene.csに設定する場合の例
 +
<pre>
 +
public void HandleRewardedAdLoaded(object sender, EventArgs args)
 +
{
 +
    MonoBehaviour.print("HandleRewardedAdLoaded event received");
 +
    if (Application.loadedLevelName.Equals("TitleScene")) {
 +
        if (rewardButton != null)
 +
        {
 +
            rewardButton.transform.position = new Vector3(2.32f, 2.40f, rewardButton.transform.position.z);
 +
        }
 +
    }
 +
}
 +
</pre>

2019年9月13日 (金) 00:25時点における版

サンプル

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判定すると事前ロードができないので注意

広告取得完了時に画面遷移するとアプリが落ちる対処法

libc++abi.dylib: terminating with uncaught exception of type Il2CppExceptionWrapper

TitleScene.csに設定する場合の例

public void HandleRewardedAdLoaded(object sender, EventArgs args)
{
    MonoBehaviour.print("HandleRewardedAdLoaded event received");
    if (Application.loadedLevelName.Equals("TitleScene")) {
        if (rewardButton != null)
        {
            rewardButton.transform.position = new Vector3(2.32f, 2.40f, rewardButton.transform.position.z);
        }
    }
}