- AdMob 광고 아이디 생성 및 설정은 생략합니다.
- 광고 Id는 Test Id를 사용합니다.
SDK 다운로드
- https://github.com/googleads/googleads-mobile-unity/releases
- 최신 버전의 SDK를 다운로드합니다. (작성일 기준 : v5.4.0)
프로젝트 셋팅
- 다운로드 받은 SDK를 프로젝트에 임포트 합니다.
- 플랫폼을 android 스위치 플랫폼합니다.
AdMob 구현
Banner (배너) 광고 노출하기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using GoogleMobileAds.Api; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.SceneManagement; | |
public class BannerDemo : MonoBehaviour | |
{ | |
private BannerView bannerView; | |
void Start() | |
{ | |
MobileAds.Initialize(initStatus => { }); | |
RequestBanner(); | |
} | |
private void OnDestroy() | |
{ | |
if (bannerView != null) | |
{ | |
bannerView.Destroy(); | |
} | |
} | |
public void OnClickedBack() | |
{ | |
SceneManager.LoadScene("MainScene"); | |
} | |
private void RequestBanner() | |
{ | |
string adUnitId = "ca-app-pub-3940256099942544/6300978111"; | |
this.bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom); | |
this.bannerView.OnAdLoaded += this.HandleOnAdLoaded; | |
this.bannerView.OnAdFailedToLoad += this.HandleOnAdFailedToLoad; | |
this.bannerView.OnAdOpening += this.HandleOnAdOpened; | |
this.bannerView.OnAdClosed += this.HandleOnAdClosed; | |
this.bannerView.OnAdLeavingApplication += this.HandleOnAdLeavingApplication; | |
AdRequest request = new AdRequest.Builder().Build(); | |
this.bannerView.LoadAd(request); | |
} | |
#region Handle Admob | |
public void HandleOnAdLoaded(object sender, EventArgs args) | |
{ | |
MonoBehaviour.print("HandleAdLoaded event received"); | |
} | |
public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args) | |
{ | |
MonoBehaviour.print("HandleFailedToReceiveAd event received with message: " | |
+ args.Message); | |
} | |
public void HandleOnAdOpened(object sender, EventArgs args) | |
{ | |
MonoBehaviour.print("HandleAdOpened event received"); | |
} | |
public void HandleOnAdClosed(object sender, EventArgs args) | |
{ | |
MonoBehaviour.print("HandleAdClosed event received"); | |
} | |
public void HandleOnAdLeavingApplication(object sender, EventArgs args) | |
{ | |
MonoBehaviour.print("HandleAdLeavingApplication event received"); | |
} | |
#endregion Handle Admob | |
} |
Interstitial (전면) 광고 노출하기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using GoogleMobileAds.Api; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.SceneManagement; | |
public class InterstitialDemo : MonoBehaviour | |
{ | |
private InterstitialAd interstitial; | |
void Start() | |
{ | |
MobileAds.Initialize(initStatus => { }); | |
RequestInterstitial(); | |
} | |
private void OnDestroy() | |
{ | |
if (interstitial != null) | |
{ | |
interstitial.Destroy(); | |
} | |
} | |
public void OnClickedBack() | |
{ | |
SceneManager.LoadScene("MainScene"); | |
} | |
private void RequestInterstitial() | |
{ | |
StartCoroutine(CoRequestInterstitial()); | |
} | |
IEnumerator CoRequestInterstitial() | |
{ | |
string adUnitId = "ca-app-pub-3940256099942544/1033173712"; | |
this.interstitial = new InterstitialAd(adUnitId); | |
this.interstitial.OnAdLoaded += this.HandleOnAdLoaded; | |
this.interstitial.OnAdFailedToLoad += this.HandleOnAdFailedToLoad; | |
this.interstitial.OnAdOpening += this.HandleOnAdOpened; | |
this.interstitial.OnAdClosed += this.HandleOnAdClosed; | |
this.interstitial.OnAdLeavingApplication += this.HandleOnAdLeavingApplication; | |
AdRequest request = new AdRequest.Builder().Build(); | |
this.interstitial.LoadAd(request); | |
yield return new WaitUntil(() => interstitial.IsLoaded()); | |
this.interstitial.Show(); | |
} | |
public void HandleOnAdLoaded(object sender, EventArgs args) | |
{ | |
MonoBehaviour.print("HandleAdLoaded event received"); | |
} | |
public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args) | |
{ | |
MonoBehaviour.print("HandleFailedToReceiveAd event received with message: " | |
+ args.Message); | |
} | |
public void HandleOnAdOpened(object sender, EventArgs args) | |
{ | |
MonoBehaviour.print("HandleAdOpened event received"); | |
} | |
public void HandleOnAdClosed(object sender, EventArgs args) | |
{ | |
MonoBehaviour.print("HandleAdClosed event received"); | |
} | |
public void HandleOnAdLeavingApplication(object sender, EventArgs args) | |
{ | |
MonoBehaviour.print("HandleAdLeavingApplication event received"); | |
} | |
} |
rewarded (보상형) 광고 노출하기 및 보상 받기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using GoogleMobileAds.Api; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.SceneManagement; | |
public class RewardedDemo : MonoBehaviour | |
{ | |
private RewardedAd rewardedAd; | |
void Start() | |
{ | |
MobileAds.Initialize(initStatus => { }); | |
RequestRewardedAd(); | |
} | |
public void OnClickedBack() | |
{ | |
SceneManager.LoadScene("MainScene"); | |
} | |
private void RequestRewardedAd() | |
{ | |
StartCoroutine(CoRequestRewardedAd()); | |
} | |
IEnumerator CoRequestRewardedAd() | |
{ | |
string adUnitId = "ca-app-pub-3940256099942544/5224354917"; | |
this.rewardedAd = new RewardedAd(adUnitId); | |
this.rewardedAd.OnAdLoaded += HandleRewardedAdLoaded; | |
this.rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad; | |
this.rewardedAd.OnAdOpening += HandleRewardedAdOpening; | |
this.rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow; | |
this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward; | |
this.rewardedAd.OnAdClosed += HandleRewardedAdClosed; | |
AdRequest request = new AdRequest.Builder().Build(); | |
this.rewardedAd.LoadAd(request); | |
yield return new WaitUntil(() => rewardedAd.IsLoaded()); | |
this.rewardedAd.Show(); | |
} | |
public void HandleRewardedAdLoaded(object sender, EventArgs args) | |
{ | |
MonoBehaviour.print("HandleRewardedAdLoaded event received"); | |
} | |
public void HandleRewardedAdFailedToLoad(object sender, AdErrorEventArgs args) | |
{ | |
MonoBehaviour.print( | |
"HandleRewardedAdFailedToLoad event received with message: " | |
+ args.Message); | |
} | |
public void HandleRewardedAdOpening(object sender, EventArgs args) | |
{ | |
MonoBehaviour.print("HandleRewardedAdOpening event received"); | |
} | |
public void HandleRewardedAdFailedToShow(object sender, AdErrorEventArgs args) | |
{ | |
MonoBehaviour.print( | |
"HandleRewardedAdFailedToShow event received with message: " | |
+ args.Message); | |
} | |
public void HandleRewardedAdClosed(object sender, EventArgs args) | |
{ | |
MonoBehaviour.print("HandleRewardedAdClosed event received"); | |
} | |
public void HandleUserEarnedReward(object sender, Reward args) | |
{ | |
string type = args.Type; | |
double amount = args.Amount; | |
MonoBehaviour.print( | |
"HandleRewardedAdRewarded event received for " | |
+ amount.ToString() + " " + type); | |
} | |
} |
github
https://github.com/siyanbae/Unity_AdMobDemo.git
siyanbae/Unity_AdMobDemo
Contribute to siyanbae/Unity_AdMobDemo development by creating an account on GitHub.
github.com
반응형
'Unity' 카테고리의 다른 글
Spine 연동 하기 (0) | 2021.05.09 |
---|---|
Unity Ads 광고 구현하기 (1) | 2021.04.29 |
[FCM] Firebase 클라우드 메시징 테스트 하기 #3 (0) | 2021.04.18 |
[FCM] Firebase 클라우드 메시징 클라이언트 구현하기 #2 (0) | 2021.04.18 |
[FCM] Firebase 클라우드 메시징 클라이언트 구현하기 #1 (0) | 2021.04.18 |