Unity/서버

210722_ AdMob _구글 애드몹 유니티에 달기

minquu 2021. 7. 22. 16:03
반응형

먼저 구글 애드몹에 가입을 한다.

 

 

 

참고 블로그 

 

https://scvtwo.tistory.com/33

 

[Unity] 유니티 구글 애드몹 광고 넣기(Google AdMob)(전면 광고)

안녕하세요. 힘들게 만든 게임에서 수익을 얻으려면 게임 내에 광고를 붙여야 합니다. 그래야 보람도 있고, 살림살이도 나아지고.. 다음 게임도 제작할 수 있기 때문이죠... 그래서 오늘은 유니

scvtwo.tistory.com

 

-----

 

 

가입 후

 

앱 추가하기 하면 이따 내 앱을 등록하면 된다.

 

https://github.com/googleads/googleads-mobile-unity/releases/tag/v6.0.2

 

Release Google Mobile Ads Unity Plugin v6.0.2 · googleads/googleads-mobile-unity

Plugin: Fixed #1677 where build error occurs on XCode 12.4. This version requires Xcode 12.4 where the previous version required Xcode 12.5.1. Built and tested with: Google Play services 20.0.0 ...

github.com

플러그인 다운 받기

 

 

 

새로운 씬 유니티에서 만들어서 

 

테스트 하나 만들고,

 

메인 스크립터 만들어서 오브젝트에 넣고

 

다운 받은 패키지 임포트 하기

 

 

 

앱 등록 해주기

 

 

완료하면

 

광고 단위 추가 누르면

 

광고 종류 선택 가능

 

리워드로 해볼 것

 

 

 

 

광고 단위생성 완료

 

보상형 광고 유니티 API

 

https://developers.google.com/admob/unity/rewarded-ads?hl=ko 

 

보상형 광고  |  Unity  |  Google Developers

보상형 광고는 상호작용하는 사용자에게 인앱 보상을 제공하는 광고입니다. 이 가이드에서는 AdMob에서 Unity 앱에 보상형 광고를 통합하는 방법을 설명합니다. 고객 성공사례인 우수사례 1과 우

developers.google.com

 

 

 

지급 설정하기

 

기존 결제 프로필이랑 연결해서 제출하기

 

 

유니티 - 에섯 - 에드몹 - 세팅

 

앱 아이디 넣어주기

 

 

앱 아이디 넣기

 

 

추가로 앱 스토어 추가도 하기

 

 

지금은 내부테스트니깐 검색이 안된다.

 

나중에 출시하면 등록하면 될듯

 

 

앱 아이디 넣고, 코드 작성하기

 

----

★테스트 광고 설정으로 해야함

 

https://developers.google.com/admob/unity/test-ads?hl=ko 

 

테스트 광고 사용 설정  |  Unity  |  Google Developers

이 가이드에는 광고 통합 과정에서 테스트 광고를 활용하는 방법이 나와 있습니다. 개발 중에는 테스트 광고를 사용하여 광고를 클릭해도 Google 광고주에게 비용이 청구되지 않게 하는 것이 중

developers.google.com

 

 

 

테스트 기기 애드몹에서 등록

 

 

 

녹스에 구글- 광고 - 기기 아이디를 등록해준다.

 

 

빌드해서

 

 

저 값을 뽑아서 

 

 

디바이스 아이디를 등록해준다.

 

 

 

---

 

전체 코드

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using GoogleMobileAds.Api;
using System;

public class GoogleAdMobMain : MonoBehaviour
{
    public string adUnitId = "ca-app-pub-3940256099942544/5224354917";
    private RewardedAd rewardedAd;
    public Button btnShow;

    private void Awake()
    {
        List<string> deviceIds = new List<string>();
        deviceIds.Add("3BE03C7C1B0782C0C115BA959C88BB63");
        RequestConfiguration requestConfiguration = new RequestConfiguration
            .Builder()
            .SetTestDeviceIds(deviceIds)
            .build();
        MobileAds.SetRequestConfiguration(requestConfiguration);


        this.btnShow.onClick.AddListener(() => {
            this.btnShow.interactable = false;
            StartCoroutine(this.WaitForLoadAds(() => {
                this.UserChoseToWatchAd();
                this.btnShow.interactable = true;
            }));
        });

        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(initStatus => {
            Debug.LogFormat("initStatus: {0}", initStatus);
        });
    }

    void Start()
    {
        this.rewardedAd = new RewardedAd(adUnitId);

        // Called when an ad request has successfully loaded.
        this.rewardedAd.OnAdLoaded += HandleRewardedAdLoaded;
        // Called when an ad request failed to load.
        this.rewardedAd.OnAdFailedToLoad += RewardedAd_OnAdFailedToLoad;
        // Called when an ad is shown.
        this.rewardedAd.OnAdOpening += HandleRewardedAdOpening;
        // Called when an ad request failed to show.
        this.rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow;
        // Called when the user should be rewarded for interacting with the ad.
        this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
        // Called when the ad is closed.
        this.rewardedAd.OnAdClosed += HandleRewardedAdClosed;

        // Create an empty ad request.
        this.request = new AdRequest.Builder().Build();

    }

    private AdRequest request;

    private IEnumerator WaitForLoadAds(System.Action callback)
    {
        // Load the rewarded ad with the request.
        this.rewardedAd.LoadAd(request);
        while (true)
        {
            if (this.rewardedAd.IsLoaded())
            {
                break;
            }
            yield return null;
        }
        callback();
    }

    private void UserChoseToWatchAd()
    {
        if (this.rewardedAd.IsLoaded())
        {
            this.rewardedAd.Show();
        }
    }

    private void RewardedAd_OnAdFailedToLoad(object sender, AdFailedToLoadEventArgs e)
    {
        Debug.Log("RewardedAd_OnAdFailedToLoad");
    }

    public void HandleRewardedAdLoaded(object sender, EventArgs args)
    {
        Debug.Log("HandleRewardedAdLoaded event received");
    }

    public void HandleRewardedAdOpening(object sender, EventArgs args)
    {
        Debug.Log("HandleRewardedAdOpening event received");
    }

    public void HandleRewardedAdFailedToShow(object sender, AdErrorEventArgs args)
    {
        Debug.Log("HandleRewardedAdFailedToShow event received with message: " + args.Message);
    }

    public void HandleRewardedAdClosed(object sender, EventArgs args)
    {
        Debug.Log("HandleRewardedAdClosed event received");
    }

    public void HandleUserEarnedReward(object sender, Reward args)
    {
        string type = args.Type;
        double amount = args.Amount;
        Debug.Log("HandleRewardedAdRewarded event received for " + amount.ToString() + " " + type);
    }
}

 

 

 

반응형

'Unity > 서버' 카테고리의 다른 글

210727_ 유니티 RestAPI  (0) 2021.07.27
210722_ 유니티Ads  (0) 2021.07.22
210721_ IAP (인앱결제)  (0) 2021.07.21
210720_ 포톤 서버 (시험)  (5) 2021.07.20
210714 _ 데이터 프로그래밍 2 (복습 및 시험)  (0) 2021.07.14