> ## Documentation Index
> Fetch the complete documentation index at: https://beta-guide.daro.so/llms.txt
> Use this file to discover all available pages before exploring further.

# 앱 오픈 광고

> Unity 프로젝트에서 DARO 앱 오픈 광고를 구현하는 방법을 알아봅니다.

## 앱 오픈 광고 형태 소개

* 앱이 백그라운드에서 포그라운드로 돌아올 때 노출하는 전면 광고입니다.
* 첫 실행 직후가 아니라, 앱 복귀 시점에 노출하는 것을 권장합니다.

***

## 광고 연동하기

<Steps>
  <Step title="광고 인스턴스 생성">
    ```csharp theme={null}
    private DaroAppOpenAd ad;

    ad = new DaroAppOpenAd("your-appopen-ad-unit-id");
    ```
  </Step>

  <Step title="이벤트 핸들러 등록">
    ```csharp theme={null}
    ad.OnAdLoaded += info => Debug.Log("app open loaded");
    ad.OnAdFailedToLoad += error => Debug.LogWarning(error.Message);
    ad.OnAdFailedToShow += error => Debug.LogWarning(error.Message);
    ```
  </Step>

  <Step title="앱 상태 변경 구독">
    ```csharp theme={null}
    DaroAppStateNotifier.OnAppStateChanged += OnAppStateChanged;
    ```
  </Step>

  <Step title="포그라운드 복귀 시 광고 표시">
    ```csharp theme={null}
    private void OnAppStateChanged(DaroAppStateNotifier.AppState state)
    {
        if (state == DaroAppStateNotifier.AppState.Foreground
            && ad != null
            && ad.IsReady())
        {
            ad.Show();
        }
    }
    ```
  </Step>

  <Step title="구독 해제 및 광고 해제">
    ```csharp theme={null}
    DaroAppStateNotifier.OnAppStateChanged -= OnAppStateChanged;
    ad?.Dispose();
    ad = null;
    ```
  </Step>
</Steps>

***

## Example

```csharp expandable theme={null}
using Daro;
using UnityEngine;

public sealed class AppOpenHost : MonoBehaviour
{
    [SerializeField] private string adUnitId = "your-appopen-ad-unit-id";
    private DaroAppOpenAd ad;
    private bool hasEnteredBackground;

    private void OnEnable()
    {
        ad = new DaroAppOpenAd(adUnitId);
        ad.OnAdLoaded += info => Debug.Log("app open loaded");
        ad.OnAdFailedToLoad += error => Debug.LogWarning(error.Message);
        ad.OnAdFailedToShow += error => Debug.LogWarning(error.Message);
        ad.Load();

        DaroAppStateNotifier.OnAppStateChanged += OnAppStateChanged;
    }

    private void OnAppStateChanged(DaroAppStateNotifier.AppState state)
    {
        if (state == DaroAppStateNotifier.AppState.Foreground
            && ad != null
            && hasEnteredBackground
            && ad.IsReady())
        {
            ad.Show();
        }

        if (state == DaroAppStateNotifier.AppState.Background)
        {
            hasEnteredBackground = true;
        }
    }

    private void OnDisable()
    {
        DaroAppStateNotifier.OnAppStateChanged -= OnAppStateChanged;
        ad?.Dispose();
        ad = null;
    }
}
```

<Warning>
  앱 오픈 광고는 첫 실행 직후에 표시하지 말고, 백그라운드에서 포그라운드로 돌아오는 시점에 표시하세요.
</Warning>

<Warning>
  Android에서는 `OnAdLoaded` 콜백이 네이티브 preload 흐름과 겹칠 수 있습니다. 표시 가능 여부는 `OnAdLoaded` 플래그만 믿지 말고 `IsReady()`로 확인하세요.
</Warning>

<Warning>
  앱 오픈 광고는 dismiss 후 자동 preload 정책을 사용할 수 있습니다. `OnAdDismissed`에서 반복적으로 `Load()`를 호출하지 마세요.
</Warning>
