새소식

유니티

[유니티]유니티 애드몹 리워드 / 유니티 구글 애드몹 / [Reward] 순서에 대하여 / 2023-02-02

  • -
code.txt
0.00MB
코드 출처
https://github.com/googleads/googleads-mobile-unity/blob/d629b7be42f93ff9efe4488b9d5ff7a2703571ac/samples/HelloWorld/Assets/Scripts/GoogleAdMobController.cs

 

 

구글 애드몹 버전 Google Mobile Ads Unity Plugin v7.4.0
전체코드
using GoogleMobileAds.Api;
using UnityEngine.Events;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

//AdmobManager.cs 플러그인이 6.0.0이상부턴 AddTestDevice가 제거되어 다음을 참고하세요.
public class ADmobManager : MonoBehaviour
{
    // 이벤트 함수에 함수를 담아 사용가능
    public UnityEvent OnAdOpeningEvent; 
    public UnityEvent OnAdClosedEvent;

    public Text LogText;
    public Button RewardAdsBtn;

    string adUnitId;

    private RewardedAd rewardedAd;  //리워드 광고 

    private void Start()
    {
        adUnitId = "ca-app-pub-3940256099942544/5224354917";
        RequestAndLoadRewardedAd();
        RewardAdsBtn.onClick.AddListener(() => ShowRewardedAd());
    }

    void PrintStatus(string s)
    {
        LogText.text = "\n" + s;

        Debug.LogWarning(s);
    }

    public void RequestAndLoadRewardedAd()
    {
        PrintStatus("광고 로드 시작");

        // create new rewarded ad instance
        RewardedAd.Load(adUnitId, CreateAdRequest(),
            (RewardedAd ad, LoadAdError loadError) =>
            {
                //리워드 요청이 실패할 경우
                if (loadError != null)
                {
                    PrintStatus("Rewarded ad failed to load with error: " +
                                loadError.GetMessage());
                    return;
                }
                else if (ad == null)
                {
                    PrintStatus("Rewarded ad failed to load.");
                    return;
                }

                PrintStatus("리워드 광고 로드완료");

                rewardedAd = ad;

                //각 상황별 이벤트 함수
                //rewardedAd변수에 각 상황에 대한 실행값들을 넣어준다.
                ad.OnAdFullScreenContentOpened += () =>
                {
                    PrintStatus("광고 오픈");
                    OnAdOpeningEvent.Invoke();
                };
                ad.OnAdFullScreenContentClosed += () =>
                {
                    PrintStatus("광고 닫음");
                    RequestAndLoadRewardedAd();
                    OnAdClosedEvent.Invoke();
                };
                ad.OnAdImpressionRecorded += () =>
                {
                    PrintStatus("광고 노출 수 기록" + ++num);
                };
                ad.OnAdClicked += () =>
                {
                    PrintStatus("광고 클릭 수 기록[실제 다운로드하기 위한]");
                };
                ad.OnAdFullScreenContentFailed += (AdError error) =>
                {
                    PrintStatus("광고 노출 실패와 이유 : " +
                               error.GetMessage());
                };
                ad.OnAdPaid += (AdValue adValue) =>
                {
                    string msg = string.Format("{0} (currency: {1}, value: {2}",
                                               "Rewarded ad received a paid event.",
                                               adValue.CurrencyCode,
                                               adValue.Value);
                    PrintStatus(msg);
                };
            });
    }

    public void ShowRewardedAd()            //리워드 광고를 보여줌
    {
        if (rewardedAd != null) // 리워드 광고가 있을 경우에 아래 해당 코드를 작성
        {
            rewardedAd.Show((Reward reward) =>
            {
                PrintStatus("Rewarded ad granted a reward: " + reward.Amount);      //리워드는 광고를 보는 즉시 지급되는 방식
            });
        }
        else
        {
            PrintStatus("Rewarded ad is not ready yet.");
        }
    }

    #region HELPER METHODS

    private AdRequest CreateAdRequest()         
    {
        return new AdRequest.Builder()
            .AddKeyword("unity-admob-sample")
            .Build();
    }
    #endregion

}

 

 

 

이벤트에 대한 설명
ad.OnAdFullScreenContentOpened += () =>
                {
                    PrintStatus("광고 오픈");
                    OnAdOpeningEvent.Invoke();
                };
광고 클릭 후 열리면 호출되는 이벤트
ad.OnAdFullScreenContentClosed += () =>
                {
                    PrintStatus("광고 닫음");
                    RequestAndLoadRewardedAd();
                    OnAdClosedEvent.Invoke();
                };
광고를 본 후 X를 눌렀을 때 호출되는 이벤트
ad.OnAdImpressionRecorded += () =>
                {
                    PrintStatus("광고 노출 수 기록");
                };
광고가 화면에 표시되었을 때 나타나는 이벤트
[EX. 광고의 일일 클릭수를 제한하는데 사용할 것으로 보임]
ad.OnAdClicked += () =>
                {
                    PrintStatus("광고 클릭 수 기록[실제 다운로드하기 위한]");
                };
광고를 클릭 수 실제 "설치하기"를 눌렀을 때의 이벤트
ad.OnAdFullScreenContentFailed += (AdError error) =>
                {
                    PrintStatus("광고 노출 실패와 이유 : " +
                               error.GetMessage());
                };
광고 노출에 실패한 경우
ad.OnAdPaid += (AdValue adValue) =>
                {
                    string msg = string.Format("{0} (currency: {1}, value: {2}",
                                               "Rewarded ad received a paid event.",
                                               adValue.CurrencyCode,
                                               adValue.Value);
                    PrintStatus(msg);
                };
모름

 

ShowRewardAd에 대해
public void ShowRewardedAd()            //리워드 광고를 보여줌
    {
        if (rewardedAd != null) // 리워드 광고가 있을 경우에 아래 해당 코드를 작성
        {
            rewardedAd.Show((Reward reward) =>
            {
                PrintStatus("리워드 광고 보상 제공 : " + reward.Amount);      //리워드는 광고를 보는 즉시 지급되는 방식
            });
        }
        else
        {
            PrintStatus("Rewarded ad is not ready yet.");
        }
    }

-광고를 닫고 보상이 지급되는 것이 아닌 광고에서 지정된 시간만큼 모두 시청하면,
(Reward reward) =>
            {
                PrintStatus("리워드 광고 보상 제공 : " + reward.Amount);      //리워드는 광고를 보는 즉시 지급되는 방식
            });
함수가 실행이 된다.

 

 


Unity Android Logcat으로 확인해본 Event 순서
유니티 시작 후 코드 실행 순서
1. RequestAndLoadRewardedAd();
2. RewardAdsBtn 버튼 클릭
1.광고로드 시작
2.리워드 광고 로드 완료
3.광고오픈
4.광고노출 수 기록 1
5.광고 모두 시청 후 => 보상제공 10
6.광고 닫음

**설치하기 누르기 => 광고 클릭 수 기록[실제 다운로드를 위한]

닫기 이후 => 
1.광고 로드 시작
2. 리워드 광고 로드 완료

 

 

광고 로드 실패시 코루틴으로 일정시간마다 재로드 하면 될것 같다.

Contents

아핫

땡큐하다