본문 바로가기
개발/C#

유니티 C# 구글 애드몹 전면 광고 간단 구현 Google Admob 8.7.0

by SPNK 2022. 11. 9.
반응형

구글 애드몹 SDK 설치

 

Releases · googleads/googleads-mobile-unity

Official Unity Plugin for the Google Mobile Ads SDK - googleads/googleads-mobile-unity

github.com

구글 애드몹 설정

 

Google AdMob: 모바일 앱 수익 창출

인앱 광고를 사용하여 모바일 앱에서 더 많은 수익을 창출하고, 사용이 간편한 도구를 통해 유용한 분석 정보를 얻고 앱을 성장시켜 보세요.

admob.google.com

 

구글 애드몹 홈페이지

 

전면 광고  |  Unity  |  Google for Developers

Google 모바일 광고 Unity 플러그인 버전 5.4.0 이하에서는 서비스가 종료되어 광고가 게재되지 않을 수 있습니다. 지원되는 SDK 버전으로 앱을 업데이트합니다. 이 페이지는 Cloud Translation API를 통해

developers.google.com

 

  • v8.7.0 기준
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;

public class AdmobScreen : MonoBehaviour
{
    [Header("안드로이드 id")]
    public string androidUnitId = "ca-app-pub-3940256099942544/1033173712";

    [Header("아이폰 id")]
    public string iosUnitId = "ca-app-pub-3940256099942544/4411468910";

    string adUnitId;

    private InterstitialAd interstitialAd;

    public void Start()
    {
        MobileAds.Initialize((InitializationStatus initStatus) =>
        {
            //초기화 완료
        });

#if UNITY_ANDROID
        adUnitId = androidUnitId;
#elif UNITY_IOS
        adUnitId = iosUnitId;
#else
        adUnitId = "unexpected_platform";
#endif

        LoadInterstitialAd();
    }

    public void LoadInterstitialAd() //광고 로드
    {
        if (interstitialAd != null)
        {
            interstitialAd.Destroy();
            interstitialAd = null;
        }

        Debug.Log("Loading the interstitial ad.");

        var adRequest = new AdRequest.Builder()
                .AddKeyword("unity-admob-sample")
                .Build();

        InterstitialAd.Load(adUnitId, adRequest,
            (InterstitialAd ad, LoadAdError error) =>
            {
                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;
            });
        RegisterEventHandlers(interstitialAd); //이벤트 등록
    }

    public void ShowAd() //광고 보기
    {
        if (interstitialAd != null && interstitialAd.CanShowAd())
        {
            Debug.Log("Showing interstitial ad.");
            interstitialAd.Show();
        }
        else
        {
            LoadInterstitialAd(); //광고 재로드

            Debug.LogError("Interstitial ad is not ready yet.");
        }
    }

    private void RegisterEventHandlers(InterstitialAd ad) //광고 이벤트
    {
        ad.OnAdPaid += (AdValue adValue) =>
        {

            //보상 주기

            Debug.Log(string.Format("Interstitial ad paid {0} {1}.",
                adValue.Value,
                adValue.CurrencyCode));
        };
        ad.OnAdImpressionRecorded += () =>
        {
            Debug.Log("Interstitial ad recorded an impression.");
        };
        ad.OnAdClicked += () =>
        {
            Debug.Log("Interstitial ad was clicked.");
        };
        ad.OnAdFullScreenContentOpened += () =>
        {
            Debug.Log("Interstitial ad full screen content opened.");
        };
        ad.OnAdFullScreenContentClosed += () =>
        {
            Debug.Log("Interstitial ad full screen content closed.");
        };
        ad.OnAdFullScreenContentFailed += (AdError error) =>
        {
            Debug.LogError("Interstitial ad failed to open full screen content " +
                           "with error : " + error);
        };
    }

	//수동으로 광고 재로드를 하고 싶다면 추가하기 (선언 필요)
    private void RegisterReloadHandler(InterstitialAd ad)
    {
        ad.OnAdFullScreenContentClosed += (null);
        {
            Debug.Log("Interstitial Ad full screen content closed.");

            LoadInterstitialAd();
        };
        ad.OnAdFullScreenContentFailed += (AdError error) =>
        {
            Debug.LogError("Interstitial ad failed to open full screen content " +
                           "with error : " + error);

            LoadInterstitialAd();
        };
    }
}

 

반응형

참고할만한 글

 

 

유니티 C# 구글 애드몹 Google Admob 보상형 광고 간단 적용법

구글 애드몹 SDK 설치 https://github.com/googleads/googleads-mobile-unity/releases Releases · googleads/googleads-mobile-unity Official Unity Plugin for the Google Mobile Ads SDK - googleads/googleads-mobile-unity github.com 구글 애드몹 홈페

parksh3641.tistory.com

 

유니티 C# 구글 애드몹 Google Admob 배너 광고 간단 적용법

구글 애드몹 SDK 설치 https://github.com/googleads/googleads-mobile-unity/releases Releases · googleads/googleads-mobile-unity Official Unity Plugin for the Google Mobile Ads SDK - googleads/googleads-mobile-unity github.com 구글 애드몹 홈페

parksh3641.tistory.com

반응형

댓글