facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(サンプル)
行68: 行68:
 
     {
 
     {
 
         MonoBehaviour.print("HandleRewardedAdClosed event received");
 
         MonoBehaviour.print("HandleRewardedAdClosed event received");
         // 事前ロードする場合
+
         // 2回目以降、事前ロードする場合
 
         CreateAndLoadRewardedAd()
 
         CreateAndLoadRewardedAd()
 
     }
 
     }

2019年5月31日 (金) 19:06時点における版

サンプル

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()
    }

    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