본문 바로가기
개발/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

 

  • 구글 애드몹 홈페이지
 

배너 광고  |  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 AdmobBanner : MonoBehaviour
{
    [Header("안드로이드 id")]
    public string androidUnitId = "ca-app-pub-3940256099942544/6300978111";

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

    string adUnitId;

    BannerView _bannerView;

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

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

        LoadAd();
    }

    public void LoadAd() //광고 로드
    {
        if (_bannerView == null)
        {
            CreateBannerView();
        }
        var adRequest = new AdRequest.Builder()
            .AddKeyword("unity-admob-sample")
            .Build();

        Debug.Log("Loading banner ad.");
        _bannerView.LoadAd(adRequest);
    }

    public void CreateBannerView() //광고 보여주기
    {
        Debug.Log("Creating banner view");

        if (_bannerView != null)
        {
            DestroyAd();
        }

        AdSize adaptiveSize =
                AdSize.GetCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(AdSize.FullWidth);

        _bannerView = new BannerView(adUnitId, adaptiveSize, AdPosition.Top);

        //_bannerView = new BannerView(_adUnitId, AdSize.Banner, 0, 50);
    }


    private void ListenToAdEvents()
    {
        _bannerView.OnBannerAdLoaded += () =>
        {
            Debug.Log("Banner view loaded an ad with response : "
                + _bannerView.GetResponseInfo());
        };
        _bannerView.OnBannerAdLoadFailed += (LoadAdError error) =>
        {
            Debug.LogError("Banner view failed to load an ad with error : "
                + error);
        };
        _bannerView.OnAdPaid += (AdValue adValue) =>
        {
            Debug.Log(string.Format("Banner view paid {0} {1}.",
                adValue.Value,
                adValue.CurrencyCode));
        };
        _bannerView.OnAdImpressionRecorded += () =>
        {
            Debug.Log("Banner view recorded an impression.");
        };
        _bannerView.OnAdClicked += () =>
        {
            Debug.Log("Banner view was clicked.");
        };
        _bannerView.OnAdFullScreenContentOpened += (null);
        {
            Debug.Log("Banner view full screen content opened.");
        };
        _bannerView.OnAdFullScreenContentClosed += (null);
        {
            Debug.Log("Banner view full screen content closed.");
        };
    }

    public void ShowAd() //광고 표시
    {
        if (_bannerView != null)
        {
            Debug.Log("Show banner ad.");
            _bannerView.Show();
        }
        else
        {
            LoadAd();
        }
    }

    public void HideAd() //광고 숨기기
    {
        if (_bannerView != null)
        {
            Debug.Log("Hide banner ad.");
            _bannerView.Hide();
        }
    }


    public void DestroyAd() //광고 제거
    {
        if (_bannerView != null)
        {
            Debug.Log("Destroying banner ad.");
            _bannerView.Destroy();
            _bannerView = null;
        }
    }
}

 


참고할만한 글

 

유니티 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

 

반응형

댓글