Unityadsのリワード広告を流す C#Unity

今回は、unityadsのリワード広告を流すプログラムを紹介します。

また、このプログラムはボタンをクリックした時に広告が流れるようにしています。
そして下のプログラムは、ボタンのUIに入れてください。
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;

[RequireComponent (typeof (Button))]
public class RewardedAdsButton : MonoBehaviour, IUnityAdsListener {


private string gameId = "ここにゲームIDを書く";

Button myButton;
public string myPlacementId = "ここにプレースメントIDを書く";

void Start () {
myButton = GetComponent <Button> ();

// Set interactivity to be dependent on the Placement’s status:
myButton.interactable = Advertisement.IsReady (myPlacementId);

// Map the ShowRewardedVideo function to the button’s click listener:
if (myButton) myButton.onClick.AddListener (ShowRewardedVideo);

// Initialize the Ads listener and service:
Advertisement.AddListener (this);
Advertisement.Initialize (gameId, true);
}

void ShowRewardedVideo () {
Advertisement.Show (myPlacementId);
}

// Implement IUnityAdsListener interface methods:
public void OnUnityAdsReady (string placementId) {
if (placementId == myPlacementId) {
myButton.interactable = true;
}
}
public void OnUnityAdsDidFinish (string placementId, ShowResult showResult) {
if (showResult == ShowResult.Finished) {
//ここに与えたい報酬を書く
} else if (showResult == ShowResult.Skipped) {
} else if (showResult == ShowResult.Failed)
{
Debug.LogWarning ("The ad did not finish due to an error.");
}
}

public void OnUnityAdsDidError (string message) {
}

public void OnUnityAdsDidStart (string placementId) {
}
}
OnUnityAdsReadyのメゾットをクリック時に呼び出すことでリワード広告が流れます。
⚠︎確認時は、必ずテストモードをオンにしておくこと!