반응형
구글 애드몹 SDK 설치
- 구글 애드몹 홈페이지
- v8.7.0 기준
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class AdmobReward : MonoBehaviour
{
[Header("안드로이드 id")]
public string androidUnitId = "ca-app-pub-3940256099942544/5224354917";
[Header("아이폰 id")]
public string iosUnitId = "ca-app-pub-3940256099942544/1712485313";
string adUnitId;
private RewardedAd rewardedAd;
void Start()
{
MobileAds.Initialize((InitializationStatus initStatus) =>
{
//초기화 완료
});
#if UNITY_ANDROID
adUnitId = androidUnitId;
#elif UNITY_IOS
adUnitId = iosUnitId;
#else
adUnitId = "unexpected_platform";
#endif
LoadRewardedAd();
}
public void LoadRewardedAd() //광고 로드 하기
{
// Clean up the old ad before loading a new one.
if (rewardedAd != null)
{
rewardedAd.Destroy();
rewardedAd = null;
}
Debug.Log("Loading the rewarded ad.");
// create our request used to load the ad.
var adRequest = new AdRequest.Builder().Build();
// 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("Rewarded ad failed to load an ad " +
"with error : " + error);
return;
}
Debug.Log("Rewarded ad loaded with response : "
+ ad.GetResponseInfo());
rewardedAd = ad;
});
}
public void ShowAd() //광고 보기
{
const string rewardMsg =
"Rewarded ad rewarded the user. Type: {0}, amount: {1}.";
if (rewardedAd != null && rewardedAd.CanShowAd())
{
rewardedAd.Show((Reward reward) =>
{
//보상 획득하기
Debug.Log(string.Format(rewardMsg, reward.Type, reward.Amount));
});
}
else
{
LoadRewardedAd();
}
}
private void RegisterReloadHandler(RewardedAd ad) //광고 재로드
{
// Raised when the ad closed full screen content.
ad.OnAdFullScreenContentClosed += (null);
{
Debug.Log("Rewarded Ad full screen content closed.");
// Reload the ad so that we can show another as soon as possible.
LoadRewardedAd();
};
// Raised when the ad failed to open full screen content.
ad.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Rewarded ad failed to open full screen content " +
"with error : " + error);
// Reload the ad so that we can show another as soon as possible.
LoadRewardedAd();
};
}
}
반응형
참고할만한 글
반응형
'개발 > C#' 카테고리의 다른 글
유니티 C# 파일 저장 경로 System Path 간단 사용법 (0) | 2022.06.19 |
---|---|
유니티 C# DontDestroyOnLoad 간단 사용법 (0) | 2022.06.19 |
유니티 C# 구글 로그인 Google Play Games GPGS 간단 구현 (0) | 2022.06.19 |
유니티 C# 싱글톤 패턴 Singleton Pattern 간단 사용법 (0) | 2022.06.19 |
유니티 C# 열거형 Enum 간단 사용법 (1) | 2022.06.19 |
유니티 C# 형 변환 Type Conversions 간단 사용법 (0) | 2022.06.19 |
유니티 C# 최적화 기법 Object Pooling 오브젝트 풀링 간단 사용법 (0) | 2022.06.15 |
유니티 C# Scriptable Object 스크립터블 오브젝트 간단 사용법 (0) | 2022.06.15 |
댓글