[Unity] Google Mobile Ads SDK 활용하여 광고 삽입하기
Unity에서 광고를 표시하기 위한 여러 플랫폼이 마련되어 있습니다.
이 중에서도 Google의 AdMob은 광고 통합에 있어 선택받는 주요 플랫폼 중 하나입니다.
(이번 ‘Pi Brain Battle’에서도 Google AdMob을 사용하였습니다.)
Google Mobile Ads SDK를 활용하게 되면, 앱 내에 광고를 간편하게 삽입할 수 있을 뿐만 아니라,
광고의 표시와 관리에 필요한 다양한 기능을 제공하여 광고 효율성을 극대화하는 데 도움을 줍니다.
이번 글에서는 Unity 환경에서 Google Mobile Ads SDK를 활용하는 방법과 주요 이벤트들을 자세히 살펴보겠습니다.
우선 Google Mobile Ads SDK를 활용하기 전에, 아래 Google AdMob 사이트에서 광고를 생성하여 앱 ID와 광고 단위 ID를 발급받아야 합니다.
https://admob.google.com/home/
ID를 발급받았다면, 아래 사이트에서 Unity Plugin을 다운로드합니다.
https://github.com/googleads/googleads-mobile-unity/releases
unitypackage를 다운로드 받은 후, 프로젝트 내에 import 해줍니다.
import를 완료한 후, Google Mobile Ads 설정 과정에서 발급받은 앱 ID를 입력합니다.
(아직 앱 출시 전이라면 발급받은 ID 대신, sample ID를 입력해야 합니다.
sample ID를 입력하지 않을 경우, 의심스러운 결제 활동으로 계정이 정지되는 현상이 발생할 수 있습니다.sample App ID: ca-app-pub-3940256099942544~3347511713)
이제 설정은 완료되었으며, 광고를 표시하기 위한 스크립트를 작성해야 합니다.
이번 ‘Pi Brain Battle’에서는 사용자가 인앱 보상을 대가로 상호작용할 수 있는 리워드 광고를 사용하였습니다.
sample 코드를 통해 해당 광고를 표시하기 위한 방법을 알아보겠습니다.
먼저 광고 단위 ID를 설정합니다. Google AdMob에서 발급받은 ID를 사용하면 됩니다.
(설정과 마찬가지로, 앱 출시 전이라면 스크립트 내에서도 sample ID를 입력해야 합니다.
sample 리워드 광고 ID: ca-app-pub-3940256099942544/5224354917)
1 2 | // ANDROID private const string _adUnitId = "ca-app-pub-3940256099942544/5224354917"; | cs |
광고 단위 ID를 설정하고 SDK 초기화 작업을 완료하였다면,
Load() 정적 메서드를 통해 리워드 광고를 로드합니다.
광고 로딩 중 에러가 발생하거나 광고 및 에러 객체가 null일 경우, 콜백 함수를 종료합니다.
로드된 리워드 광고 객체는 완료 핸들러에 파라미터로 제공됩니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | /// <summary> /// Loads the ad. /// </summary> public void LoadAd() { // Clean up the old ad before loading a new one. if (_rewardedAd != null) { DestroyAd(); } Debug.Log("Loading rewarded ad."); // Create our request used to load the ad. var adRequest = new AdRequest(); // Send the request to load the ad. RewardedAd.Load(_adUnitId, adRequest, (RewardedAd ad, LoadAdError error) => { // If the operation failed with a reason. if (error != null) { Debug.LogError("Rewarded ad failed to load an ad with error : " + error); return; } // If the operation failed for unknown reasons. // This is an unexpected error, please report this bug if it happens. if (ad == null) { Debug.LogError("Unexpected error: Rewarded load event fired with null ad and null error."); return; } // The operation completed successfully. Debug.Log("Rewarded ad loaded with response : " + ad.GetResponseInfo()); _rewardedAd = ad; // Register to ad events to extend functionality. RegisterEventHandlers(ad); // Inform the UI that the ad is ready. AdLoadedStatus?.SetActive(true); }); } | cs |
로드된 리워드 광고를 사용자에게 보여주기 위해서는 Show() 메서드를 활용할 수 있습니다.
CanShowAd() 메서드를 통해 광고를 표시할 준비가 되었는지 확인 후, 리워드를 처리하기 위한 콜백을 제공합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /// <summary> /// Shows the ad. /// </summary> public void ShowAd() { if (_rewardedAd != null && _rewardedAd.CanShowAd()) { Debug.Log("Showing rewarded ad."); _rewardedAd.Show((Reward reward) => { Debug.Log(String.Format("Rewarded ad granted a reward: {0} {1}", reward.Amount, reward.Type)); }); } else { Debug.LogError("Rewarded ad is not ready yet."); } // Inform the UI that the ad is not ready. AdLoadedStatus?.SetActive(false); } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | private void RegisterEventHandlers(RewardedAd ad) { // Raised when the ad is estimated to have earned money. ad.OnAdPaid += (AdValue adValue) => { Debug.Log(String.Format("Rewarded ad paid {0} {1}.", adValue.Value, adValue.CurrencyCode)); }; // Raised when an impression is recorded for an ad. ad.OnAdImpressionRecorded += () => { Debug.Log("Rewarded ad recorded an impression."); }; // Raised when a click is recorded for an ad. ad.OnAdClicked += () => { Debug.Log("Rewarded ad was clicked."); }; // Raised when the ad opened full screen content. ad.OnAdFullScreenContentOpened += () => { Debug.Log("Rewarded ad full screen content opened."); }; // Raised when the ad closed full screen content. ad.OnAdFullScreenContentClosed += () => { Debug.Log("Rewarded ad full screen content closed."); }; // Raised when the ad failed to open full screen content. ad.OnAdFullScreenContentFailed += (AdError error) => { Debug.LogError("Rewarded ad failed to open full screen content with error : " + error); }; } | cs |
OnAdPaid: 광고가 수익을 얻을 것으로 예상될 때 발생합니다. AdValue 파라미터를 통해 광고에서 얻은 수익에 관한 정보를 받습니다.
OnAdImpressionRecorded: 사용자가 광고를 실제로 보았다는 것이 기록될 때 발생합니다.
OnAdClicked: 사용자가 광고를 클릭할 때 발생합니다.
OnAdFullScreenContentOpened: 광고가 풀 스크린 콘텐츠를 열었을 때 발생합니다.
OnAdFullScreenContentClosed: 광고의 풀 스크린 콘텐츠가 닫혔을 때 발생합니다. 사용자가 광고를 닫았을 때 호출됩니다.
OnAdFullScreenContentFailed: 광고가 풀 스크린 콘텐츠를 열려고 했으나 어떠한 오류로 실패했을 때 발생합니다. AdError 파라미터를 통해 오류에 대한 정보를 받습니다.
Google Mobile Ads Unity Plugin가 업데이트되면서 이벤트의 이름이나 내용이 변경될 수 있으므로,
아래의 공식 가이드와 업데이트 시 지원하는 샘플 코드를 참고하여 최신 정보를 확인하는 게 좋을 것 같습니다.
https://developers.google.com/admob/unity/quick-start?hl=ko
댓글
댓글 쓰기