본문 바로가기
개발/C#

유니티 C# 구글 애드몹 보상형 광고 간단 구현 Google Admob 8.7.0

by SPNK 2022. 6. 19.
반응형

구글 애드몹 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 AdmobReward : MonoBehaviour
{
    [Header("안드로이드 id")]
    public string androidUnitId = "ca-app-pub-3940256099942544/5224354917";

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

    string adUnitId;

    private RewardedAd rewardedAd;

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

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

        LoadRewardedAd();
    }

    public void LoadRewardedAd() //광고 로드 하기
    {
        // Clean up the old ad before loading a new one.
        if (rewardedAd != null)
        {
            rewardedAd.Destroy();
            rewardedAd = null;
        }

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

        // create our request used to load the ad.
        var adRequest = new AdRequest.Builder().Build();

        // send the request to load the ad.
        RewardedAd.Load(adUnitId, adRequest,
            (RewardedAd ad, LoadAdError error) =>
            {
                // if error is not null, the load request failed.
                if (error != null || ad == null)
                {
                    Debug.LogError("Rewarded ad failed to load an ad " +
                                   "with error : " + error);
                    return;
                }

                Debug.Log("Rewarded ad loaded with response : "
                          + ad.GetResponseInfo());

                rewardedAd = ad;
            });
    }

    public void ShowAd() //광고 보기
    {
        const string rewardMsg =
            "Rewarded ad rewarded the user. Type: {0}, amount: {1}.";

        if (rewardedAd != null && rewardedAd.CanShowAd())
        {
            rewardedAd.Show((Reward reward) =>
            {
                //보상 획득하기
                Debug.Log(string.Format(rewardMsg, reward.Type, reward.Amount));
            });
        }
        else
        {
            LoadRewardedAd();
        }
    }

    private void RegisterReloadHandler(RewardedAd ad) //광고 재로드
    {
        // Raised when the ad closed full screen content.
        ad.OnAdFullScreenContentClosed += (null);
        {
            Debug.Log("Rewarded Ad full screen content closed.");

            // Reload the ad so that we can show another as soon as possible.
            LoadRewardedAd();
        };
        // 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);

            // Reload the ad so that we can show another as soon as possible.
            LoadRewardedAd();
        };
    }
}

 

반응형

참고할만한 글

 

 

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

반응형

댓글