# Unity에서 보상형 광고 구현

> Unity 프로젝트에 보상형 광고를 구현합니다. 광고 콘텐츠를 로드하고 C# 스크립트를 통해 표시하고 광고 완료 시 게임 내 보상 부여합니다.

광고를 보는 플레이어에게 보상을 제공하면 사용자 참여가 증가하고 더욱 높은 수익으로 이어질 수 있습니다. 예를 들어, 게임 내 화폐나 소모품, 추가 생명, 경험치 증가 등의 보상을 게임에서 플레이어에게 제공할 수 있습니다. 보상형 광고를 효과적으로 설계하는 방법에 관한 자세한 내용은 [수익화 전략](/grow/ads/monetization-strategy.md) 기술 자료를 참조하십시오.

> **Note:**
>
> SDK 버전 4.4.1부터 Unity 에디터에서는 Unity Ads 패키지를 Advertisement Legacy 패키지라고 부릅니다.
> Advertisement Legacy 패키지 버전 4.12는 계속 작동하지만 새로운 기능이나 향상된 기능에 대한 추가 업데이트는 수신하지 않습니다.
> SDK 버전 4.12는 Apple 개인정보 보호 매니페스트 업데이트를 지원하며, 패키지에 대한 추가 업데이트는 계획되어 있지 않습니다.

동영상 광고 시청을 완료한 플레이어에게 보상을 제공하려면 `ShowResult` 결과를 사용하는 콜백 메서드를 구현하여 사용자가 광고 시청을 완료했으며 보상이 제공되어야 하는지 확인합니다.

## 보상형 광고 버튼##rewarded-ad-buttons

플레이어가 광고 보기를 선택할 수 있도록 프롬프트를 표시하는 버튼을 사용하는 것이 보상형 비디오 광고의 일반적인 구현 방식입니다. 다음 단계의 예시 코드를 사용하면 보상형 광고 버튼을 생성할 수 있습니다. 이 버튼은 눌렸을 때 이용 가능한 광고 콘텐츠가 있으면 이를 표시합니다.

### 보상형 비디오 광고 예제##rewarded-video-ad-example

Unity 에디터에서 버튼을 구성하려면 다음 단계를 수행합니다.

1. **Game Object** > **UI** > **Button**을 선택하여 씬에 버튼을 추가합니다.
2. 씬에 추가한 버튼을 선택한 다음 인스펙터를 사용하여 스크립트 컴포넌트를 버튼에 추가합니다(**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! Only load content AFTER initialization (in this example, initialization is handled in a different script).
       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에서 배너 광고 구현](/grow/ads/unity-sdk/banner-ads.md)을 참조하십시오.
