반응형
구글 애드몹 SDK 설치
구글 애드몹 설정
구글 애드몹 홈페이지
- v8.7.0 기준
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class AdmobScreen : MonoBehaviour
{
[Header("안드로이드 id")]
public string androidUnitId = "ca-app-pub-3940256099942544/1033173712";
[Header("아이폰 id")]
public string iosUnitId = "ca-app-pub-3940256099942544/4411468910";
string adUnitId;
private InterstitialAd interstitialAd;
public void Start()
{
MobileAds.Initialize((InitializationStatus initStatus) =>
{
//초기화 완료
});
#if UNITY_ANDROID
adUnitId = androidUnitId;
#elif UNITY_IOS
adUnitId = iosUnitId;
#else
adUnitId = "unexpected_platform";
#endif
LoadInterstitialAd();
}
public void LoadInterstitialAd() //광고 로드
{
if (interstitialAd != null)
{
interstitialAd.Destroy();
interstitialAd = null;
}
Debug.Log("Loading the interstitial ad.");
var adRequest = new AdRequest.Builder()
.AddKeyword("unity-admob-sample")
.Build();
InterstitialAd.Load(adUnitId, adRequest,
(InterstitialAd ad, LoadAdError error) =>
{
if (error != null || ad == null)
{
Debug.LogError("interstitial ad failed to load an ad " +
"with error : " + error);
return;
}
Debug.Log("Interstitial ad loaded with response : "
+ ad.GetResponseInfo());
interstitialAd = ad;
});
RegisterEventHandlers(interstitialAd); //이벤트 등록
}
public void ShowAd() //광고 보기
{
if (interstitialAd != null && interstitialAd.CanShowAd())
{
Debug.Log("Showing interstitial ad.");
interstitialAd.Show();
}
else
{
LoadInterstitialAd(); //광고 재로드
Debug.LogError("Interstitial ad is not ready yet.");
}
}
private void RegisterEventHandlers(InterstitialAd ad) //광고 이벤트
{
ad.OnAdPaid += (AdValue adValue) =>
{
//보상 주기
Debug.Log(string.Format("Interstitial ad paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));
};
ad.OnAdImpressionRecorded += () =>
{
Debug.Log("Interstitial ad recorded an impression.");
};
ad.OnAdClicked += () =>
{
Debug.Log("Interstitial ad was clicked.");
};
ad.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Interstitial ad full screen content opened.");
};
ad.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Interstitial ad full screen content closed.");
};
ad.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Interstitial ad failed to open full screen content " +
"with error : " + error);
};
}
//수동으로 광고 재로드를 하고 싶다면 추가하기 (선언 필요)
private void RegisterReloadHandler(InterstitialAd ad)
{
ad.OnAdFullScreenContentClosed += (null);
{
Debug.Log("Interstitial Ad full screen content closed.");
LoadInterstitialAd();
};
ad.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Interstitial ad failed to open full screen content " +
"with error : " + error);
LoadInterstitialAd();
};
}
}
반응형
참고할만한 글
반응형
'개발 > C#' 카테고리의 다른 글
유니티 C# 카메라 이동범위 제한 Camera Mathf.Clamp (0) | 2022.12.22 |
---|---|
유니티 C# 캐릭터 3인칭 카메라 따라가기 Follow Camera (0) | 2022.12.22 |
유니티 C# 카메라 목표지점까지 부드럽게 이동 Camera Vector3.SmoothDamp (0) | 2022.12.22 |
유니티 C# 텍스트 코드로 색깔 간단 변경법 Text Color (0) | 2022.12.22 |
유니티 C# 구글 애드몹 배너 광고 간단 구현 Google Admob 8.7.0 (0) | 2022.11.09 |
유니티 C# String Enum 타입 간단 변환 (0) | 2022.10.24 |
유니티 C# Enum Count 길이 간단 구하기 (0) | 2022.09.20 |
유니티 C# 마우스 좌표 Mouse Position 간단 구하기 (0) | 2022.09.20 |
댓글