# Unity でのリワード広告の実装

> Unity プロジェクトにリワード広告を実装します。広告コンテンツをロードし、C# スクリプトで表示し、広告の視聴完了時のゲーム内報酬を付与します。

広告を視聴したプレイヤーに報酬を与えると、ユーザーエンゲージメントが向上し、収益の増加につながります。例えば、プレイヤーに与える報酬には、ゲーム内通貨、消耗品、追加のライフ、経験値の倍増などがあります。リワード広告を効果的にデザインする方法の詳細については、[収益化戦略](/monetization/getting-started/monetization-strategy.md) のドキュメントを参照してください。

> **Note:**
>
> 2026 年 4 月 1 日より、Advertisement Legacy パッケージを介した直接インテグレーションによって Unity Ads で収益化するアプリケーションでは、広告のパフォーマンスが低下する可能性があります。パフォーマンスの削減を回避するには、Unity Adsを入札者として統合する方式に切り替えます。
>
> Unity Ads ネットワークは、直接インテグレーションを使用するすべてのアプリケーションのサポートと提供を継続します。ただし、収益とパフォーマンスを最大化するために推奨されるベストプラクティスは、Unityの広告メディエーションプラットフォームであるLevelPlay用のAds Mediationパッケージ[をインストールする](/grow/levelplay/sdk/unity/package-integration.md.md)ことです。

動画広告を最後まで視聴したプレイヤーに報酬を与えるには、`ShowResult` の結果を使用してコールバックメソッドを実装し、ユーザーが広告の視聴を完了して報酬を受け取る資格があるかどうかを確認します。

## リワード広告ボタン##rewarded-ad-buttons

動画リワード広告では、プレイヤーに広告の視聴を促すボタンを実装するのが一般的です。リワード広告ボタンを作成するには、以下の手順でコード例を使用します。広告コンテンツが利用可能であれば、このボタンを押すと広告が表示されます。

### リワード動画広告の例##rewarded-video-ad-example

Unity エディターでボタンを設定するには、以下の手順に従います。

1. **Game Object** (ゲームオブジェクト) > **UI** > **Button** (ボタン) の順に選択して、シーンにボタンを追加します。
2. シーンに追加したボタンを選択し、Inspector (インスペクター) を使用してそのボタンにスクリプトコンポーネントを追加します (**Add Component** (コンポーネントを追加) > **New Script** (新しいスクリプト))。このスクリプトに、クラス名に合わせて `RewardedAdsButton` という名前を付けます。
3. スクリプトを開き、以下のコードを追加します。

1) **C#**

   ```cs
   using UnityEngine;
   using UnityEngine.UI;
   using UnityEngine.Advertisements;

   public class RewardedAdsButton : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
   {
     [SerializeField] Button _showAdButton;
     [SerializeField] string _androidAdUnitId = "Rewarded_Android";
     [SerializeField] string _iOSAdUnitId = "Rewarded_iOS";
     string _adUnitId = null; // This will remain null for unsupported platforms

     void Awake()
     {   
       // Get the Ad Unit ID for the current platform:
       #if UNITY_IOS
       _adUnitId = _iOSAdUnitId;
       #elif UNITY_ANDROID
       _adUnitId = _androidAdUnitId;
       #endif

       // Disable the button until the ad is ready to show:
       _showAdButton.interactable = false;
     }

     // Call this public method when you want to get an ad ready to show.
     public void LoadAd()
     {
       // IMPORTANT!コンテンツのロードは初期化した後にのみ行ってください (この例では、初期化は別のスクリプトで処理されています)。
       Debug.Log("Loading Ad: " + _adUnitId);
       Advertisement.Load(_adUnitId, this);
     }

     // If the ad successfully loads, add a listener to the button and enable it:
     public void OnUnityAdsAdLoaded(string adUnitId)
     {
       Debug.Log("Ad Loaded: " + adUnitId);

       if (adUnitId.Equals(_adUnitId))
       {
         // Configure the button to call the ShowAd() method when clicked:
         _showAdButton.onClick.AddListener(ShowAd);
         // Enable the button for users to click:
         _showAdButton.interactable = true;
       }
     }

     // Implement a method to execute when the user clicks the button:
     public void ShowAd()
     {
       // Disable the button:
       _showAdButton.interactable = false;
       // Then show the ad:
       Advertisement.Show(_adUnitId, this);
     }

     // Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward:
     public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
     {
       if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
       {
         Debug.Log("Unity Ads Rewarded Ad Completed");
         // Grant a reward.
       }
     }

     // Implement Load and Show Listener error callbacks:
     public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
     {
       Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}");
       // Use the error details to determine whether to try to load another ad.
     }

     public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
     {
       Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
       // Use the error details to determine whether to try to load another ad.
     }

     public void OnUnityAdsShowStart(string adUnitId) { }
     public void OnUnityAdsShowClick(string adUnitId) { }

     void OnDestroy()
     {
       // Clean up the button listeners:
       _showAdButton.onClick.RemoveAllListeners();
     }
   }
   ```

**次のステップ**: [Unity でのバナー広告の実装](/ads-unity/4.19.0/sdk-integration/banner-ads.md) を参照してください。
