전면 광고는 호스트 앱의 인터페이스를 가리는 전체 화면 광고입니다. 일반적으로 게임에서 다음 레벨로 넘어갈 때처럼 앱 이용이 잠시 중단될 때 자연스럽게 광고가 게재됩니다. 앱에서 전면 광고가 표시되면 사용자는 광고를 탭하여 도착 페이지로 이동하거나 광고를 닫고 앱으로 돌아갈 수도 있습니다.
이 가이드에서는 Unity 앱에 전면 광고를 통합하는 방법을 설명합니다.
기본 요건
- 시작 가이드를 모두 읽어보세요.
항상 테스트 광고로 테스트
다음 샘플 코드에는 테스트 광고를 요청하는 데 사용할 수 있는 광고 단위 ID가 포함되어 있습니다. 이 ID는 모든 요청에 대해 프로덕션 광고가 아닌 테스트 광고를 반환하도록 구성되어서 안전하게 사용할 수 있습니다.
그러나Ad Manager 웹 인터페이스에 앱을 등록하고 앱에서 사용할 광고 단위 ID를 직접 만든 후에는 개발 중에 명시적으로 기기를 테스트 기기로 구성해야 합니다.
/6499/example/interstitial
모바일 광고 SDK 초기화
광고를 로드하기 전에 MobileAds.Initialize()
를 호출하여 앱에서 모바일 광고 SDK를 초기화하도록 합니다. 이 작업은 앱 실행 시 한 번만 처리하면 됩니다.
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
public void Start()
{
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize((InitializationStatus initStatus) =>
{
// This callback is called once the MobileAds SDK is initialized.
});
}
}
미디에이션을 사용하는 경우 광고를 로드하기 전에 콜백이 발생할 때까지 기다려야 모든 미디에이션 어댑터가 초기화됩니다.
구현
전면 광고를 통합하는 주요 단계는 다음과 같습니다.
- 전면 광고 로드
- 전면 광고 표시
- 전면 광고 이벤트 수신
- 전면 광고 정리
- 다음 전면 광고 미리 로드
전면 광고 로드
전면 광고는 InterstitialAd
클래스의 정적 Load()
메서드를 사용하여 로드됩니다. 로드 메서드에는 광고 단위 ID, AdManagerAdRequest
객체, 광고 로드에 성공하거나 실패할 때 호출되는 완료 핸들러가 필요합니다. 로드된 AdManagerInterstitialAd
객체는 완료 핸들러의 매개변수로 제공됩니다. 아래 예는 AdManagerInterstitialAd
를 로드하는 방법을 보여줍니다.
// This ad unit is configured to always serve test ads.
private string _adUnitId = "/6499/example/interstitial";
private InterstitialAd _interstitialAd;
/// <summary>
/// Loads the interstitial ad.
/// </summary>
public void LoadInterstitialAd()
{
// Clean up the old ad before loading a new one.
if (_interstitialAd != null)
{
_interstitialAd.Destroy();
_interstitialAd = null;
}
Debug.Log("Loading the interstitial ad.");
// create our request used to load the ad.
var adRequest = new AdManagerAdRequest();
// send the request to load the ad.
AdManagerInterstitialAd.Load(_adUnitId, adRequest,
(InterstitialAd ad, LoadAdError error) =>
{
// if error is not null, the load request failed.
if (error != null || ad == null)
{
Debug.LogError("interstitial ad failed to load an ad " +
"with error : " + error);
return;
}
Debug.Log("Interstitial ad loaded with response : "
+ ad.GetResponseInfo());
_interstitialAd = ad;
});
}
전면 광고 표시
로드된 전면 광고를 표시하려면 AdManagerInterstitialAd
인스턴스에서
Show()
메서드를 호출합니다. 광고는 로드당 한 번 게재될 수 있습니다. CanShowAd()
메서드를 사용하여 광고를 게재할 준비가 되었는지 확인합니다.
/// <summary>
/// Shows the interstitial ad.
/// </summary>
public void ShowInterstitialAd()
{
if (_interstitialAd != null && _interstitialAd.CanShowAd())
{
Debug.Log("Showing interstitial ad.");
_interstitialAd.Show();
}
else
{
Debug.LogError("Interstitial ad is not ready yet.");
}
}
전면 광고 이벤트 수신
광고의 작동 방식을 추가로 맞춤설정하려는 경우 광고 수명 주기의 여러 이벤트에 연결하면 됩니다. 아래와 같이 대리자를 등록하여 이러한 이벤트를 수신합니다.
private void RegisterEventHandlers(InterstitialAd interstitialAd)
{
// Raised when the ad is estimated to have earned money.
interstitialAd.OnAdPaid += (AdValue adValue) =>
{
Debug.Log(String.Format("Interstitial ad paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));
};
// Raised when an impression is recorded for an ad.
interstitialAd.OnAdImpressionRecorded += () =>
{
Debug.Log("Interstitial ad recorded an impression.");
};
// Raised when a click is recorded for an ad.
interstitialAd.OnAdClicked += () =>
{
Debug.Log("Interstitial ad was clicked.");
};
// Raised when an ad opened full screen content.
interstitialAd.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Interstitial ad full screen content opened.");
};
// Raised when the ad closed full screen content.
interstitialAd.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Interstitial ad full screen content closed.");
};
// Raised when the ad failed to open full screen content.
interstitialAd.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Interstitial ad failed to open full screen content " +
"with error : " + error);
};
}
전면 광고 정리
AdManagerInterstitialAd
지정이 끝나면 참조를 삭제하기 전에 Destroy()
메서드를 호출해야 합니다.
_interstitialAd.Destroy();
이렇게 하면 플러그인이 객체를 더 이상 사용하지 않으며 점유하는 메모리를 회수할 수 있음을 알립니다. 이 메서드를 호출하지 못하면 메모리 누수가 발생합니다.
다음 전면 광고 미리 로드
전면 광고는 일회용 객체입니다. 즉, 전면 광고가 표시된 후에는
이 객체를 다시 사용할 수 없습니다. 다른 전면 광고를 요청하려면
새 AdManagerInterstitialAd
객체를 만드세요.
다음 노출 기회에 전면 광고가 게재되도록
OnAdFullScreenContentClosed
또는
OnAdFullScreenContentFailed
광고 이벤트가 발생한 후에
전면 광고를 미리 로드합니다.
private void RegisterReloadHandler(InterstitialAd interstitialAd)
{
// Raised when the ad closed full screen content.
interstitialAd.OnAdFullScreenContentClosed += ()
{
Debug.Log("Interstitial Ad full screen content closed.");
// Reload the ad so that we can show another as soon as possible.
LoadInterstitialAd();
};
// Raised when the ad failed to open full screen content.
interstitialAd.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Interstitial ad failed to open full screen content " +
"with error : " + error);
// Reload the ad so that we can show another as soon as possible.
LoadInterstitialAd();
};
}
앱 이벤트
앱 이벤트를 사용하면 앱 코드에 메시지를 전송하는 광고를 만들 수 있습니다. 그러면 앱에서 이러한 메시지를 기반으로 작업을 수행할 수 있습니다.
AppEvent
를 사용하여 Ad Manager 관련 앱 이벤트를 수신할 수 있습니다. 이러한 이벤트는
로드가 호출되기 전을 포함해 광고의 수명 주기 동안 언제든지 발생할 수 있습니다.
namespace GoogleMobileAds.Api.AdManager;
/// The App event message sent from the ad.
public class AppEvent
{
// Name of the app event.
string Name;
// Argument passed from the app event.
string Value;
}
OnAppEventReceived
는 광고에서 앱 이벤트가 발생하면 발생합니다. 다음은 코드에서 이 이벤트를 처리하는 방법을 보여주는 예입니다.
_interstitialAd.OnAppEventReceived += (AppEvent args) =>
{
Debug.Log($"Received app event from the ad: {args.Name}, {args.Value}.");
};
다음은 색상 이름이 적용된 앱 이벤트에 따라 앱의 배경색을 변경하는 방법의 예입니다.
_interstitialAd.OnAppEventReceived += (AppEvent args) =>
{
if (args.Name == "color")
{
Color color;
if (ColorUtility.TryParseColor(arg.Value, out color))
{
gameObject.GetComponent<Renderer>().material.color = color;
}
}
};
다음은 색상 앱 이벤트를 전송하는 해당 광고 소재입니다.
<html>
<head>
<script src="//www.gstatic.com/afma/api/v1/google_mobile_app_ads.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function() {
// Send a color=green event when ad loads.
admob.events.dispatchAppEvent("color", "green");
document.getElementById("ad").addEventListener("click", function() {
// Send a color=blue event when ad is clicked.
admob.events.dispatchAppEvent("color", "blue");
});
});
</script>
<style>
#ad {
width: 320px;
height: 50px;
top: 0px;
left: 0px;
font-size: 24pt;
font-weight: bold;
position: absolute;
background: black;
color: white;
text-align: center;
}
</style>
</head>
<body>
<div id="ad">Carpe diem!</div>
</body>
</html>
권장사항
- 전면 광고가 앱에 적합한 유형의 광고인지 확인하세요.
- 전면 광고는 자연스러운 전환 지점이 있는 앱에서 가장 효과적입니다. 이미지 공유나 게임 레벨 완료 등 앱에서 작업이 완료되는 순간이 발생합니다. 앱에서 전면 광고를 가장 효과적으로 표시할 시점과 사용자가 어떻게 반응할지를 고려해야 합니다.
- 전면 광고를 표시할 때 작업을 일시중지합니다.
- 텍스트, 이미지, 동영상 등 다양한 유형의 전면 광고가
있습니다. 앱에서 전면 광고를 표시할 때는 광고가 리소스를 활용할 수 있도록
일부 리소스의 사용도 중지해야 합니다. 예를 들어 전면 광고를 표시하도록 호출할 때
앱에서 재생되는 오디오 출력을 일시중지해야 합니다.
사용자가 광고와의 상호작용을 마칠 때 호출될 수 있는
OnAdFullScreenContentClosed()
이벤트에서 사운드 재생을 재개할 수 있습니다. 또한 광고가 표시되는 동안에는 게임 루프와 같은 강도 높은 계산 작업을 잠시 중단하는 것이 좋습니다. 이렇게 하면 그래픽이 느리거나 응답하지 않는 문제나 동영상 끊김 등의 문제가 발생하지 않습니다. - 사용자에게 광고를 지나치게 많이 게재하지 않습니다.
- 앱에 전면 광고를 더 많이 게재할수록 수익이 늘어난다고 생각할 수 있겠지만, 이렇게 하면 사용자 환경이 악화되고 클릭률이 떨어지기도 합니다. 사용자의 원활한 앱 사용에 지장을 주지 않는 범위에서 게재 빈도를 조절하시기 바랍니다.
추가 리소스
- HelloWorld 예시: 모든 광고 형식을 최소한으로 구현