facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(サンプル再生)
 
(同じ利用者による、間の13版が非表示)
行1: 行1:
==サンプル再生==
+
このページは2019年頃の、古いAPIなので、以下が推奨
 +
 
 +
[[Unity/GoogleMobileAds/RewardedVideo8]] [ショートカット]
 +
 
 +
==サンプル再生方法==
 
以下を対応した後
 
以下を対応した後
  
[[unity/GoogleMobileAds組込/基本]] [ショートカット]
+
[[unity/GoogleMobileAds/基本]] [ショートカット]
  
 
以下公式の組み方を追加
 
以下公式の組み方を追加
行11: 行15:
 
  using System;
 
  using System;
 
を追加すれば直る
 
を追加すれば直る
 +
 +
==サンプルコード==
 +
#AssetsにCreate/C#でGoogleMobileAdsDemoScriptを追加し以下コード追加
 +
#HierarchyにCanvasを追加し、Button追加で"ShowButton"を追加
 +
 +
using System.Collections;
 +
using System.Collections.Generic;
 +
using UnityEngine;
 +
using UnityEngine.UI;
 +
using GoogleMobileAds.Api;
 +
using System;
 +
public class GoogleMobileAdsDemoScript : MonoBehaviour
 +
{
 +
    private static bool isRegistedRewardVideoEventHandler = false;
 +
    private RewardBasedVideoAd rewardBasedVideo;
 +
    public void Start()
 +
    {
 +
        // Get singleton reward based video ad reference.
 +
        this.rewardBasedVideo = RewardBasedVideoAd.Instance;
 +
        if (!isRegistedRewardVideoEventHandler) {
 +
            // 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;
 +
            isRegistedRewardVideoEventHandler = true;
 +
        }
 +
        if (rewardBasedVideo.IsLoaded())
 +
        {
 +
            MonoBehaviour.print("HandleRewardBasedVideoLoaded IsLoaded");
 +
        } else {
 +
            this.RequestRewardBasedVideo();
 +
        }
 +
        GameObject.Find("ShowButton").GetComponent<Button>().onClick.AddListener(OnClickShow);
 +
    }
 +
    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");
 +
        // 次回用事前loadはここですると良いかも(this.RequestRewardBasedVideo();)
 +
    }
 +
    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");
 +
    }
 +
    void OnClickShow()
 +
    {
 +
        if (rewardBasedVideo.IsLoaded())
 +
        {
 +
            rewardBasedVideo.Show();
 +
        }
 +
        else
 +
        {
 +
            MonoBehaviour.print("HandleRewardBasedVideoShow Not loaded yet");
 +
        }
 +
    }
 +
}
 +
 +
==エラー対応方法==
 +
Loaded他イベント内で、広告表示画面内のプロパティなどを操作すると、
 +
別の画面に移動した際にエラーが起こる可能性があるので気をつける
 +
 +
このようにSceneなどを指定して操作すればよい。
 +
public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
 +
{
 +
  if (Application.loadedLevelName.Equals("TitleScene")) {
 +
    sceneProperty1 = 1;
 +
  }
 +
}

2024年6月18日 (火) 13:47時点における最新版

このページは2019年頃の、古いAPIなので、以下が推奨

Unity/GoogleMobileAds/RewardedVideo8 [ショートカット]

サンプル再生方法

以下を対応した後

unity/GoogleMobileAds/基本 [ショートカット]

以下公式の組み方を追加

https://developers.google.com/admob/unity/rewarded-video

HandleRewardBasedVideoLoadedやEventArgsが読み込めない場合は、

using System;

を追加すれば直る

サンプルコード

  1. AssetsにCreate/C#でGoogleMobileAdsDemoScriptを追加し以下コード追加
  2. HierarchyにCanvasを追加し、Button追加で"ShowButton"を追加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using GoogleMobileAds.Api;
using System;
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
   private static bool isRegistedRewardVideoEventHandler = false;
   private RewardBasedVideoAd rewardBasedVideo;
   public void Start()
   {
       // Get singleton reward based video ad reference.
       this.rewardBasedVideo = RewardBasedVideoAd.Instance;
       if (!isRegistedRewardVideoEventHandler) {
           // 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;
           isRegistedRewardVideoEventHandler = true;
       }
       if (rewardBasedVideo.IsLoaded())
       {
           MonoBehaviour.print("HandleRewardBasedVideoLoaded IsLoaded");
       } else {
           this.RequestRewardBasedVideo();
       }
       GameObject.Find("ShowButton").GetComponent<Button>().onClick.AddListener(OnClickShow);
   }
   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");
       // 次回用事前loadはここですると良いかも(this.RequestRewardBasedVideo();)
   }
   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");
   }
   void OnClickShow()
   {
       if (rewardBasedVideo.IsLoaded())
       {
           rewardBasedVideo.Show();
       }
       else
       {
           MonoBehaviour.print("HandleRewardBasedVideoShow Not loaded yet");
       }
   }
}

エラー対応方法

Loaded他イベント内で、広告表示画面内のプロパティなどを操作すると、 別の画面に移動した際にエラーが起こる可能性があるので気をつける

このようにSceneなどを指定して操作すればよい。

public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
{
  if (Application.loadedLevelName.Equals("TitleScene")) {
    sceneProperty1 = 1;
  }
}