[Unity] 유니티 애드몹 전면 광고 적용하기 (AdMob Interstitial Ads) v3.17

Posted by Dev Park
2019. 6. 2. 11:41 Unity

유니티에서 애드몹을 이용해서 전면 광고를 넣는 방법입니다. 

애드몹 버전은 Google Mobile Ads Unity Plugin v3.17 입니다.

 

먼저 전면 광고를 넣으려면 애드몹에서 전면 광고의 광고 단위를 만들고 

플러그인을 설치하고 AndroidManifest.xml 을 설정해야 합니다. 

이 부분은 이전 배너광고 적용하기 글에서 설명하고 있으니 

애드몹 배너 광고 적용하기 (AdMob Banner) v3.17 를 참고해 주세요. 

 

1. 광고 단위 만들기 

2. 플러그인 설치 

3. AndroidManifest.xml 설정

 

이 글은 위 3가지가 준비된 상태에서 진행되는 내용이니 위 3가지가 준비되지 않았다면 먼저 이전 글을 읽고 오시기 바랍니다. 

 

위의 3가지가 준비된 상태라면 전면 광고를 넣기는 매우 쉽습니다. 

코드는 아래와 같습니다. 

using System;
using UnityEngine;
using GoogleMobileAds.Api;

public class AdMobAdsManager : MonoBehaviour
{
    private InterstitialAd interstitial;

    public void Start()
    {
#if UNITY_ANDROID
        string appId = "ca-app-pub-3940256099942544~3347511713";
#elif UNITY_IPHONE
            string appId = "ca-app-pub-3940256099942544~1458002511";
#else
            string appId = "unexpected_platform";
#endif
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(appId);

        this.RequestInterstitial();
    }
    
    // 전면 광고 
    private void RequestInterstitial()
    {
#if UNITY_ANDROID
        string adUnitId = "ca-app-pub-3940256099942544/1033173712"; //테스트 아이디 
#elif UNITY_IPHONE
        //string adUnitId = "ca-app-pub-3940256099942544/4411468910";
#else
        string adUnitId = "unexpected_platform";
#endif

        // Initialize an InterstitialAd.
        this.interstitial = new InterstitialAd(adUnitId);
        // 전면광고
        // Called when an ad request has successfully loaded.
        this.interstitial.OnAdLoaded += HandleOnAdLoaded;
        // Called when an ad request failed to load.
        this.interstitial.OnAdFailedToLoad += HandleOnAdFailedToLoad;
        // Called when an ad is shown.
        this.interstitial.OnAdOpening += HandleOnAdOpened;
        // Called when the ad is closed.
        this.interstitial.OnAdClosed += HandleOnAdClosed;
        // Called when the ad click caused the user to leave the application.
        this.interstitial.OnAdLeavingApplication += HandleOnAdLeavingApplication;

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the interstitial with the request.
        this.interstitial.LoadAd(request);
    }
    public void HandleOnAdLoaded(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdLoaded event received");
    }

    public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        MonoBehaviour.print("interstitial Failed : "
                            + args.Message);
    }

    public void HandleOnAdOpened(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdOpened event received");
    }

    public void HandleOnAdClosed(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdClosed event received");
        RequestInterstitial();
    }

    public void HandleOnAdLeavingApplication(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdLeavingApplication event received");
    }

    public void ShowInterstitial()
    {
        if (this.interstitial.IsLoaded())
        {
            this.interstitial.Show();
        }
        else
        {
            Debug.Log("NOT Loaded Interstitial");
            RequestInterstitial();
        }
    }
}

 

이전 배너 설명 글에서도 이야기 했지만 

 

        // Called when an ad request has successfully loaded.
        this.interstitial.OnAdLoaded += HandleOnAdLoaded;
        // Called when an ad request failed to load.
        this.interstitial.OnAdFailedToLoad += HandleOnAdFailedToLoad;
        // Called when an ad is shown.
        this.interstitial.OnAdOpening += HandleOnAdOpened;
        // Called when the ad is closed.
        this.interstitial.OnAdClosed += HandleOnAdClosed;
        // Called when the ad click caused the user to leave the application.
        this.interstitial.OnAdLeavingApplication += HandleOnAdLeavingApplication;

 

이 부분은 광고의 이벤트 처리 부분으로 로그를 확인하거나 각 이벤트에 대응되는 작업이 필요할때 사용합니다. 

사용하지 않으면 없어도 됩니다. 

 

위 코드에서는 광고 이벤트를 통해 광고가 닫힐때 RequestInterstitial() 함수를 통해 광고를 다시 로드하도록 했습니다. 

ShowInterstitial() 함수를 통해 전면 광고가 나와야 할 부분에서 전면 광고가 보여지도록 호출합니다. 

 

아래는 이전 배너 광고와 함께 전면 광고를 함께 사용 하는 코드입니다. 

using System;
using UnityEngine;
using GoogleMobileAds.Api;

public class AdmobAdManager : MonoBehaviour
{
    private BannerView banner;
    private InterstitialAd interstitial;

    public void Start()
    {
#if UNITY_ANDROID
        string appId = "ca-app-pub-3940256099942544~3347511713";
#elif UNITY_IPHONE
            string appId = "ca-app-pub-3940256099942544~1458002511";
#else
            string appId = "unexpected_platform";
#endif
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(appId);

        this.RequestInterstitial();
        this.RequestBanner();
    }

    private void RequestBanner()
    {
#if UNITY_ANDROID
        string AdUnitID = "ca-app-pub-3940256099942544/6300978111"; //테스트 아이디
#else
        string AdUnitID = "unDefind";
#endif

        banner = new BannerView(AdUnitID, AdSize.SmartBanner, AdPosition.Bottom);
        AdRequest request = new AdRequest.Builder().Build();

        banner.LoadAd(request);
    }

    public void ShowBanner()
    {
        banner.Show();
    }

    public void HideBanner()
    {
        banner.Hide();
    }

    // 전면 광고 
    private void RequestInterstitial()
    {
#if UNITY_ANDROID
        string adUnitId = "ca-app-pub-3940256099942544/1033173712"; //테스트 아이디 
#elif UNITY_IPHONE
        //string adUnitId = "ca-app-pub-3940256099942544/4411468910";
#else
        string adUnitId = "unexpected_platform";
#endif

        // Initialize an InterstitialAd.
        this.interstitial = new InterstitialAd(adUnitId);
        // 전면광고
        // Called when an ad request has successfully loaded.
        this.interstitial.OnAdLoaded += HandleOnAdLoaded;
        // Called when an ad request failed to load.
        this.interstitial.OnAdFailedToLoad += HandleOnAdFailedToLoad;
        // Called when an ad is shown.
        this.interstitial.OnAdOpening += HandleOnAdOpened;
        // Called when the ad is closed.
        this.interstitial.OnAdClosed += HandleOnAdClosed;
        // Called when the ad click caused the user to leave the application.
        this.interstitial.OnAdLeavingApplication += HandleOnAdLeavingApplication;

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the interstitial with the request.
        this.interstitial.LoadAd(request);
    }
    public void HandleOnAdLoaded(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdLoaded event received");
    }

    public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        MonoBehaviour.print("interstitial Failed : "
                            + args.Message);
    }

    public void HandleOnAdOpened(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdOpened event received");
    }

    public void HandleOnAdClosed(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdClosed event received");
        RequestInterstitial();
    }

    public void HandleOnAdLeavingApplication(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdLeavingApplication event received");
    }

    public void ShowInterstitial()
    {
        if (this.interstitial.IsLoaded())
        {
            this.interstitial.Show();
        }
        else
        {
            Debug.Log("NOT Loaded Interstitial");
            RequestInterstitial();
        }
    }
}

 

이전에 설명한대로 배너 광고는 광고 이벤트를 사용하지 않기 때문에 해당 부분은 삭제했습니다.

 

유니티 에디터 상에서 위와 같이 전면 광고 생성 로그가 나오면 정상적으로 작동하는 것입니다.

 

기본적으로 하단 배너가 나오고 전면 광고 테스트 버튼을 누르면 전면 광고가 나오도록 했습니다. 

 

테스트 버튼을 누르면 이렇게 전면 광고가 출력됩니다. 

 

위 코드에 사용된 앱 ID와 광고 단위 ID는 애드몹에서 제공하는 테스트용 ID입니다. 

실제 광고를 보고 싶으면 애드몹에서 만든 자신의 실제 앱ID와 광고 단위 ID를 코드에 적용하면됩니다.