# Unity에서 배너 광고 구현

> Unity 프로젝트 배너 광고를 구현합니다. 광고 콘텐츠를 로드하고 C# 스크립트를 통해 화면의 고정된 위치에 표시합니다.

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

> **Important:**
>
> &#x20;
>
> [app-ads.txt](/grow/ads/optimization/app-ads-txt.md)
>
> 는 광고 생태계의 부정 행위를 근절하고 투명성을 구축하기 위한&#x20;
>
> [IAB](https://www.iab.com/)
>
> &#x20;이니셔티브입니다. 설명된 대로 app-ads.txt를 구현해야 합니다. 그렇지 않으면 배너 수요가 크게 줄어듭니다.&#x20;

## 스크립트 구현##script-implementation

플레이스먼트 스크립트 헤더에서 [`Banner`](/grow/ads/unity-sdk/unity-api.md#banner) 클래스를 포함하는 `UnityEngine.Advertisements` 네임스페이스를 선언합니다. 다음으로, SDK를 초기화한 후 [`Banner.Load`](/grow/ads/unity-sdk/unity-api.md#banner-load) 및 [`Banner.Show`](/grow/ads/unity-sdk/unity-api.md#banner-show) 메서드를 사용하여 배너 광고를 로드하고 표시합니다.

다음 예제 스크립트는 씬의 버튼을 설정하여 이 기능을 테스트하는 방법을 보여 줍니다. Unity 에디터에서 버튼을 만들려면 **Game Object** > **UI** > **Button**을 선택합니다.

> **Note:**
>
> SDK가 초기화된 후에만 콘텐츠를 로드하십시오. 그렇지 않으면 스크립트가 작동하지 않습니다. 이 예제에서 초기화는 다른 스크립트에서 처리합니다.

### 배너 광고 예시##banner-ad-example

1. **C#**

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

   public class BannerAdExample : MonoBehaviour
   {
     // For the purpose of this example, these buttons are for functionality testing:
     [SerializeField] Button _loadBannerButton;
     [SerializeField] Button _showBannerButton;
     [SerializeField] Button _hideBannerButton;

     [SerializeField] BannerPosition _bannerPosition = BannerPosition.BOTTOM_CENTER;

     [SerializeField] string _androidAdUnitId = "Banner_Android";
     [SerializeField] string _iOSAdUnitId = "Banner_iOS";
     string _adUnitId = null; // This will remain null for unsupported platforms.

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

       // Disable the button until an ad is ready to show:
       _showBannerButton.interactable = false;
       _hideBannerButton.interactable = false;

       // Set the banner position:
       Advertisement.Banner.SetPosition(_bannerPosition);

       // Configure the Load Banner button to call the LoadBanner() method when clicked:
       _loadBannerButton.onClick.AddListener(LoadBanner);
       _loadBannerButton.interactable = true;
     }

     // Implement a method to call when the Load Banner button is clicked:
     public void LoadBanner()
     {
         // Set up options to notify the SDK of load events:
         BannerLoadOptions options = new BannerLoadOptions
         {
             loadCallback = OnBannerLoaded,
             errorCallback = OnBannerError
         };

         // Load the Ad Unit with banner content:
         Advertisement.Banner.Load(_adUnitId, options);
     }

     // Implement code to execute when the loadCallback event triggers:
     void OnBannerLoaded()
     {
         Debug.Log("Banner loaded");

         // Configure the Show Banner button to call the ShowBannerAd() method when clicked:
         _showBannerButton.onClick.AddListener(ShowBannerAd);
         // Configure the Hide Banner button to call the HideBannerAd() method when clicked:
         _hideBannerButton.onClick.AddListener(HideBannerAd);

         // Enable both buttons:
         _showBannerButton.interactable = true;
         _hideBannerButton.interactable = true;     
     }

     // Implement code to execute when the load errorCallback event triggers:
     void OnBannerError(string message)
     {
         Debug.Log($"Banner Error: {message}");
         // Optionally execute additional code, such as attempting to load another ad.
     }

     // Implement a method to call when the Show Banner button is clicked:
     void ShowBannerAd()
     {
         // Set up options to notify the SDK of show events:
         BannerOptions options = new BannerOptions
         {
             clickCallback = OnBannerClicked,
             hideCallback = OnBannerHidden,
             showCallback = OnBannerShown
         };

         // Show the loaded Banner Ad Unit:
         Advertisement.Banner.Show(_adUnitId, options);
     }

     // Implement a method to call when the Hide Banner button is clicked:
     void HideBannerAd()
     {
         // Hide the banner:
         Advertisement.Banner.Hide();
     }

     void OnBannerClicked() { }
     void OnBannerShown() { }
     void OnBannerHidden() { }

     void OnDestroy()
     {
         // Clean up the listeners:
         _loadBannerButton.onClick.RemoveAllListeners();
         _showBannerButton.onClick.RemoveAllListeners();
         _hideBannerButton.onClick.RemoveAllListeners();
     }
   }
   ```

## 배너 위치##banner-position

기본적으로 배너 광고는 화면 중앙 하단에 고정되어 표시되며 320x50픽셀 또는 728x90픽셀 해상도를 지원합니다. 배너 앵커를 지정하려면 `Banner.SetPosition` API를 사용합니다. 예제는 아래와 같습니다.

```cs
Advertisement.Banner.SetPosition (BannerPosition.TOP_CENTER);
```

**다음 단계**: [수익화 전략](/grow/ads/monetization-strategy.md) 가이드를 검토하고 [구현 내용을 테스트](/grow/ads/optimization/test-ads-integration.md)하십시오.
