using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;
public class RewardAdManager : MonoBehaviour
{
private RewardedAd rewardedAd;
public UnityEvent OnAdLoadedEvent;
public UnityEvent OnAdFailedToLoadEvent;
public UnityEvent OnAdOpeningEvent;
public UnityEvent OnAdFailedToShowEvent;
public UnityEvent OnUserEarnedRewardEvent;
public UnityEvent OnAdClosedEvent;
public Text rewardText;
private int gameAmount = 0;
private int rewardAmount;
private string idApp, idRewardAd;
public static RewardAdManager instance;
void Awake(){
instance = this;
}
void Start(){
List<String> deviceIds = new List<String>() { AdRequest.TestDeviceSimulator };
idApp = "75EF8D155528C04DACBBA6F36F433035";
idRewardAd = "ca-app-pub-3940256099942544/
5224354917";
// Add some test device IDs (replace with your own device IDs).
#if UNITY_EDITOR
deviceIds.Add("unused");
#elif UNITY_ANDROID
deviceIds.Add(idApp);
#else
deviceIds.Add("unexpected_platform");
#endif
// Configure TagForChildDirectedTreatment and test device IDs.
RequestConfiguration requestConfiguration =
new RequestConfiguration.Builder()
.SetTagForChildDirectedTreatment(TagForChildDirectedTreatment.Unspecified)
.SetTestDeviceIds(deviceIds).build();
MobileAds.SetRequestConfiguration(requestConfiguration);
//Initialize the Google Mobile Ads SDK.
//MobileAds.Initialize(HandleInitCompleteAction);
}
void Update(){
if(rewardAmount > 0){
gameAmount = rewardAmount;
rewardText.text = "Points Earned: " + gameAmount.ToString();
rewardAmount=0;
}
}
/*private void HandleInitCompleteAction(InitializationStatus initstatus) {
Debug.Log("Initialization complete.");
MobileAdsEventExecutor.ExecuteInUpdate(() => {
RequestAndLoadRewardedAd();
});
}*/
public void RequestAndLoadRewardedAd() {
Debug.Log("Requesting Rewarded ad.");
//create new rewarded ad instance
rewardedAd = new RewardedAd("ca-app-pub-3940256099942544/
5224354917");
// Add Event Handlers
rewardedAd.OnAdLoaded += (sender, args) =>
{
Debug.Log("Reward ad loaded.");
OnAdLoadedEvent.Invoke();
};
rewardedAd.OnAdFailedToLoad += (sender, args) =>
{
Debug.Log("Reward ad failed to load.");
OnAdFailedToLoadEvent.Invoke();
};
rewardedAd.OnAdOpening += (sender, args) =>
{
Debug.Log("Reward ad opening.");
OnAdOpeningEvent.Invoke();
};
rewardedAd.OnAdFailedToShow += (sender, args) =>
{
Debug.Log("Reward ad failed to show with error: "+ args.AdError.GetMessage());
OnAdFailedToShowEvent.Invoke();
};
rewardedAd.OnAdClosed += (sender, args) =>
{
Debug.Log("Reward ad closed.");
OnAdClosedEvent.Invoke();
};
rewardedAd.OnUserEarnedReward += (sender, args) =>
{
Debug.Log("User earned Reward ad reward: "+ args.Amount);
rewardAmount = (int) args.Amount;
OnUserEarnedRewardEvent.Invoke();
};
rewardedAd.OnAdDidRecordImpression += (sender, args) =>
{
Debug.Log("Reward ad recorded an impression.");
};
//Create empty ad request and show once loaded
rewardedAd.LoadAd(CreateAdRequest());
}
public void ShowRewardedAd() {
if (rewardedAd != null) {
rewardedAd.Show();
} else {
Debug.Log("Rewarded ad is not ready yet.");
}
}
/* public void MobileRewardAd(){
StartCoroutine(Delay());
}
IEnumerator Delay(){
RequestAndLoadRewardedAd();
yield return new WaitForSeconds(2f);
ShowRewardedAd();
}*/
private AdRequest CreateAdRequest() {
return new AdRequest.Builder().AddKeyword("ca-app-pub-3940256099942544/
5224354917").Build();
}
public void DestroyRewardAd() {
if (rewardedAd != null) {
rewardedAd.Destroy();
}
}
}