using GoogleMobileAds;
using GoogleMobileAds.Api;
using UnityEngine;
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
#if UNITY_ANDROID
private string _adUnitId = "ca-app-pub-3940256099942544/5224354917"; // Android 광고 단위 ID
#elif UNITY_IPHONE
// private string _adUnitId = "ca-app-pub-3940256099942544/1712485313"; // iOS 광고 단위 ID
#endif
private RewardedAd _rewardedAd;
public void Start()
{
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize((InitializationStatus initStatus) =>
{
// This callback is called once the MobileAds SDK is initialized.
});
}
// Loads the rewarded ad.
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 an ad request.
AdRequest 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)
{
Debug.LogError("Rewarded ad failed to load an ad with error: " + error);
return;
}
Debug.Log("Rewarded ad loaded with response: " + ad.GetResponseInfo());
_rewardedAd = ad;
});
}
// Show the rewarded ad to the user when they earn a reward.
public void ShowRewardedAd()
{
if (_rewardedAd != null)
{
_rewardedAd.Show((Reward reward) =>
{
// Handle the reward here (e.g., give the user in-game currency).
double rewardAmount = reward.Amount;
string rewardType = reward.Type;
Debug.Log("User earned a reward of " + rewardAmount + " " + rewardType);
ot.gold += (int)ot.money; // 광고 시청하면 주는 보상
// Call a function to update the UI or perform any other actions related to the reward.
UpdateUIAfterReward();
});
}
else
{
Debug.LogError("Rewarded ad is not loaded yet.");
}
}
// Function to update the UI or perform other actions after the reward is given.
private void UpdateUIAfterReward()
{
// Update the user interface to reflect the updated gold value.
// For example, you can update a coin counter text or display a success message to the user.
}
}
코드는 저렇게 짰습니다
버튼 누르면 showrewardedad 함수 실행되도록 해놨어요
id는 샘플 광고 (테스트용 id) 넣어놨고요
구글 모바일 애드 세팅은 저렇게 했어요
광고 시청 버튼 누르면 이렇게 뜹니다
Rewarded ad is not loaded yet.
UnityEngine.Debug:LogError (object)
GoogleMobileAdsDemoScript:ShowRewardedAd () (at Assets/AdmobManager2.cs:75)
UnityEngine.EventSystems.EventSystem:Update () (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:530)
제가 혹시 빼먹은 부분이 있나요? 도와주세요
보상형광고가 로드가 되지않았다잖아 게임실행했을떄 LoadRewardedAd()함수 실행한거 맞음?