private string _adUnitId;
private RewardedAd rewardedAd;
// Start is called before the first frame update
void Start()
{
#if UNITY_IOS
_adUnitId = "..................................";
#endif
#if UNITY_ANDROID
_adUnitId = "............................";
#endif
MobileAds.Initialize((InitializationStatus initStatus) =>
{
// This callback is called once the MobileAds SDK is initialized.
});
LoadRewardedAd();
}
// Update is called once per frame
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();
//adRequest.Keywords.Add("unity-admob-sample");
// 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.
AB_DictionaryFileManager.Save_to_Profile("Ticket",1);
Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount));
});
}
}