ewarded
using GoogleMobileAds.Api;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds;
using System;
public class rewarded : MonoBehaviour {
public int adtoshow, adrun;
// Start is called before the first frame update
void Start() {
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize((InitializationStatus initStatus) => {
// This callback is called once the MobileAds SDK is initialized.
});
}
// Update is called once per frame
void Update() {
}
// These ad units are configured to always serve test ads.
#if UNITY_ANDROID
private string _adUnitId = "ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
private string _adUnitId = "ca-app-pub-3940256099942544/1712485313";
#else
private string _adUnitId = "unused";
#endif
private RewardedAd _rewardedAd;
/// <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("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("Rewarded ad failed to load an ad " +
"with error : " + error);
return;
}
Debug.Log("Rewarded ad loaded with response : "
+ ad.GetResponseInfo());
_rewardedAd = ad;
});
}
public void ShowRewardedAd() {
const string rewardMsg =
"Rewarded ad rewarded the user. Type: {0}, amount: {1}.";
if (_rewardedAd != null && _rewardedAd.CanShowAd()) {
_rewardedAd.Show((Reward reward) => {
// TODO: Reward the user.
Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount));
});
}
}
public void runrewarded() {
if (adtoshow != 0) {
adtoshow -= 1;
LoadRewardedAd();
} else {
adtoshow = adrun;
ShowRewardedAd();
}
}
}
Comments
Post a Comment