๋ฐ์ํ
๐์ฐธ๊ณ : https://learnandcreate.tistory.com/2365
ํจํค์ง ์ค์น
Admob Package ๋ค์ด๋ก๋: https://github.com/googleads/googleads-mobile-unity/releases
(v8.6.0 ๋ฐ์)
ํ ์คํธ ํ ๊ฑฐ๋ฉด Android์ ํ ์คํธ ์์ด๋ ๋ฃ์
ca-app-pub-3940256099942544~3347511713
๐Document: https://developers.google.com/admob/unity/rewarded?hl=ko
Admob์์ ์ฑ ์ถ๊ฐ
๐Admob: https://apps.admob.com/v2/home
๊ด๊ณ ๋จ์ ์ถ๊ฐ
๊ตฌ๊ธ ์ํํ๋ก์ ํธ
๐Initํ๋ ๋ถ๋ถ
๐ Reward ๊ด๊ณ
์ํ์ฝ๋
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(); // ๊ด๊ณ ์ฌ์ฅ์
};
}
}
๋์ํ์ธ
- Load๋ฅผ ์ํ๋ฉด Show Ad ์๋จ
- ํ๋ฒ Loadํ๋ฉด ๊ทธ ์ดํ๋ก ๊ณ์ Show Ad ๊ฐ๋ฅ
๋ฐ์ํ
'Unity > Unity ๋ฆฌ์์น' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Mine Particle ํญํ์๊ฐ ์กฐ์ (0) | 2024.05.15 |
---|---|
Particle์ Trail์ด ๋ถ๋ชจ์ Transform์ ๋ฐ๋ผ ์๊ธฐ๊ฒ ํ๊ธฐ (0) | 2024.05.03 |
Dependency hunter (0) | 2023.12.13 |
์คํฌ๋กค๋ทฐ ๋๊ฐ ๋์์ ์์ง์ด๊ธฐ (0) | 2023.07.12 |
์ค์ฒฉ๋ LayoutView๊ฐ ์ ๋๋ก ์๋ํ์ง ์์ ๋ (0) | 2023.07.08 |