Unity/Unity ๋ฆฌ์„œ์น˜

Unity์— AdMob ๊ตฌํ˜„

lipnus 2024. 2. 12. 20:12
๋ฐ˜์‘ํ˜•

 

๐Ÿ“—์ฐธ๊ณ : https://learnandcreate.tistory.com/2365

 

 

ํŒจํ‚ค์ง€ ์„ค์น˜

Admob Package ๋‹ค์šด๋กœ๋“œ: https://github.com/googleads/googleads-mobile-unity/releases

(v8.6.0 ๋ฐ›์Œ)

 

 

Import

 

 

Settings
Enable Auto Resolution ์ฒดํฌ

 

 

 

Assets -> Google Mobile Ads -> Settgins...

 

 

ํ…Œ์ŠคํŠธ ํ• ๊ฑฐ๋ฉด Android์— ํ…Œ์ŠคํŠธ ์•„์ด๋”” ๋„ฃ์Œ

ca-app-pub-3940256099942544~3347511713

 

๐Ÿ“ƒDocument: https://developers.google.com/admob/unity/rewarded?hl=ko

 

 

Admob์—์„œ ์•ฑ ์ถ”๊ฐ€

๐Ÿ“ŒAdmobhttps://apps.admob.com/v2/home

์ฐธ๊ณ : https://learnandcreate.tistory.com/2366

 

 

 

๊ด‘๊ณ ๋‹จ์œ„ ์ถ”๊ฐ€

 

 

 

 

๊ตฌ๊ธ€ ์ƒ˜ํ”Œํ”„๋กœ์ ํŠธ

๐Ÿ“‘Initํ•˜๋Š” ๋ถ€๋ถ„

https://github.com/googleads/googleads-mobile-unity/blob/main/samples/HelloWorld/Assets/Scripts/InterstitialAdController.cs

 

๐Ÿ“‘ Reward ๊ด‘๊ณ 

https://github.com/googleads/googleads-mobile-unity/blob/main/samples/HelloWorld/Assets/Scripts/RewardedAdController.cs

 

 

 

์ƒ˜ํ”Œ์ฝ”๋“œ

Admob Document๊ธฐ์ค€์œผ๋กœ ์ž‘์„ฑ (8.6.0์—์„œ ์ •์ƒ๋™์ž‘)

using System;
using GoogleMobileAds.Api;
using MoreMountains.Tools;
using UnityEngine;

public class AdManager : MMSingleton<AdManager>
{
    // These ad units are configured to always serve test ads.
    #if UNITY_ANDROID
        private string _adUnitId = "ca-app-pub-3940256099942544/5224354917"; // ๋ฆฌ์›Œ๋“œ ๊ด‘๊ณ  ํ…Œ์ŠคํŠธ Id
    #elif UNITY_IPHONE
      private string _adUnitId = "ca-app-pub-3940256099942544/1712485313";
    #else
      private string _adUnitId = "unused";
    #endif

    private RewardedAd _rewardedAd;
    
    public void Start()
    {
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize((InitializationStatus initStatus) =>
        {
            Debug.Log("[AD] MobileAds Initialized.");
            // This callback is called once the MobileAds SDK is initialized.
        });
    }

    /// <summary>
    /// Loads the rewarded ad.
    /// </summary>
    public void LoadRewardedAd()
    {
        // Clean up the old ad before loading a new one.
        if (_rewardedAd != null)
        {
            _rewardedAd.Destroy();
            _rewardedAd = null;
        }

        Debug.Log("[AD] Loading the rewarded ad.");

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

        // 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("[AD] Rewarded ad failed to load an ad " + "with error : " + error);
                    return;
                }

                Debug.Log("[AD] Rewarded ad loaded with response : " + ad.GetResponseInfo());
                _rewardedAd = ad;
                
                RegisterEventHandlers(ad); // ๊ฒฐ๊ณผ๋ฅผ ์ด๋ฒคํŠธ๋กœ ์ˆ˜์‹ 
            });
    }
    
    public void ShowRewardedAd()
    {

        if (_rewardedAd != null && _rewardedAd.CanShowAd())
        {
            _rewardedAd.Show((Reward reward) =>
            {
                // TODO: Reward the user.
                Debug.Log($"[AD] ๋ฆฌ์›Œ๋“œ / reward.Type: {reward.Type}, reward.Amount: {reward.Amount}");
            });
        }
    }
    
    private void RegisterEventHandlers(RewardedAd ad)
    {
        // Raised when the ad is estimated to have earned money.
        ad.OnAdPaid += (AdValue adValue) =>
        {
            Debug.Log($"[AD] Rewarded ad paid {adValue.Value} {adValue.CurrencyCode}.");
        };
        // Raised when an impression is recorded for an ad.
        ad.OnAdImpressionRecorded += () =>
        {
            Debug.Log("[AD] Rewarded ad recorded an impression.");
        };
        // Raised when a click is recorded for an ad.
        ad.OnAdClicked += () =>
        {
            Debug.Log("[AD] Rewarded ad was clicked.");
        };
        // Raised when an ad opened full screen content.
        ad.OnAdFullScreenContentOpened += () =>
        {
            Debug.Log("[AD] Rewarded ad full screen content opened.");
        };
        // Raised when the ad closed full screen content.
        ad.OnAdFullScreenContentClosed += () =>
        {
            Debug.Log("[AD] Rewarded ad full screen content closed.");
            LoadRewardedAd(); // ๊ด‘๊ณ  ์žฌ์žฅ์ „
        };
        // Raised when the ad failed to open full screen content.
        ad.OnAdFullScreenContentFailed += (AdError error) =>
        {
            Debug.LogError("[AD] Rewarded ad failed to open full screen content " + "with error : " + error);
            LoadRewardedAd(); // ๊ด‘๊ณ  ์žฌ์žฅ์ „
        };
    }
}

 

 

๋™์ž‘ํ™•์ธ

 

OnStart()
Load Ad ๋ˆ„๋ฆ„
Show Ad ๋ˆ„๋ฆ„

 

 

Close Ad๋ฒ„ํŠผ ๋ˆ„๋ฆ„

 

 

- Load๋ฅผ ์•ˆํ•˜๋ฉด Show Ad ์•ˆ๋จ

- ํ•œ๋ฒˆ Loadํ•˜๋ฉด ๊ทธ ์ดํ›„๋กœ ๊ณ„์† Show Ad ๊ฐ€๋Šฅ

๋ฐ˜์‘ํ˜•