using System;
using UnityEngine;
using GoogleMobileAds.Api;
using UnityEngine.Advertisements;
using UnityEngine.SceneManagement;
public class Admob_Unity_Ads : MonoBehaviour , IUnityAdsInitializationListener , IUnityAdsLoadListener, IUnityAdsShowListener
{
[Header(“Test Mode”)]
public bool Test = true;
private RewardedAd rewarded_ad;
public BannerView bannerView;
public string Andriod_Banner_ID;
public string rewarded_ad_id;
public string Interstitial_ad_id;
public string Rew_Interstitial_ad_id;
private InterstitialAd interstitial;
private bool Req_Interstitial_Add = true;
private RewardedInterstitialAd rewardedInterstitialAd;
[Header("Unity Test Mode")]
public bool TestMode;
// For Unity
[Header("Unity ID's")]
public string gameID = "";
public string bannerID = "Banner_Android";
public string interstitialID = "Interstitial_Android";
public string rewardedVideoID = "Rewarded_Android";
void Start()
{
//rewarded_ad_id = "ca-app-pub-5616490787498850/9447604557";
MobileAds.Initialize(InitializationStatus => { });
// requestintertetial();
requestrewardedvideo();
Show_Ban();
Rew_Int_Request();
if (Req_Interstitial_Add == true)
{
this.RequestInterstitial();
Req_Interstitial_Add = false;
}
//For Unity
//Advertisement.Initialize(gameID, TestMode);
InitializeAds();
//this.Advertisement.AddListener;
}
public void InitializeAds()
{
Advertisement.Initialize(gameID, TestMode, this);
}
public void Rew_Int_Request()
{
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded ad with the request.
RewardedInterstitialAd.LoadAd(Rew_Interstitial_ad_id, request, adLoadCallback);
}
public void ShowRewardedInterstitialAd()
{
if (rewardedInterstitialAd != null)
{
rewardedInterstitialAd.Show(userEarnedRewardCallback);
}
else
{
Advertisement.Show(interstitialID);
}
}
private void adLoadCallback(RewardedInterstitialAd ad, string error)
{
if (error == null)
{
rewardedInterstitialAd = ad;
rewardedInterstitialAd.OnAdFailedToPresentFullScreenContent += HandleAdFailedToPresent;
rewardedInterstitialAd.OnAdDidPresentFullScreenContent += HandleAdDidPresent;
rewardedInterstitialAd.OnAdDidDismissFullScreenContent += HandleAdDidDismiss;
rewardedInterstitialAd.OnPaidEvent += HandlePaidEvent;
}
}
private void HandleAdFailedToPresent(object sender, AdErrorEventArgs args)
{
print("Rewarded interstitial ad has failed to present.");
}
private void HandleAdDidPresent(object sender, EventArgs args)
{
bannerView.Hide();
print("Rewarded interstitial ad has presented.");
}
private void HandleAdDidDismiss(object sender, EventArgs args)
{
print("Rewarded interstitial ad has dismissed presentation.");
bannerView.Show();
}
private void HandlePaidEvent(object sender, AdValueEventArgs args)
{
MonoBehaviour.print(
"Rewarded interstitial ad has received a paid event.");
}
private void userEarnedRewardCallback(Reward reward)
{
// TODO: Reward the user.
}
public void RequestInterstitial()
{
string adUnitId = "";
if (Test)
{
if (Application.platform == RuntimePlatform.Android)
{
adUnitId = "ca-app-pub-3940256099942544/1033173712";
}
}
else
{
if (Application.platform == RuntimePlatform.Android)
{
adUnitId = Interstitial_ad_id;
}
//if (Application.platform == RuntimePlatform.IPhonePlayer)
//{
// //adUnitId = IOS_Intertisial_ID;
//}
}
//#if UNITY_ANDROID
// string adUnitId = Interstitial_ad_id;
//#elif UNITY_IPHONE
// string adUnitId = "ca-app-pub-3940256099942544/4411468910";
//#else
// string adUnitId = "unexpected_platform";
//#endif
// Initialize an InterstitialAd.
this.interstitial = new InterstitialAd(adUnitId);
// Called when an ad request has successfully loaded.
this.interstitial.OnAdLoaded += HandleOnAdLoaded;
// Called when an ad request failed to load.
this.interstitial.OnAdFailedToLoad += HandleOnAdFailedToLoad;
// Called when an ad is shown.
this.interstitial.OnAdOpening += HandleOnAdOpened;
// Called when the ad is closed.
this.interstitial.OnAdClosed += HandleOnAdClosed;
// Called when the ad click caused the user to leave the application.
this.interstitial.OnAdLeavingApplication += HandleOnAdLeavingApplication;
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
this.interstitial.LoadAd(request);
}
public void RequestBanner()
{
string adUnitId = "";
if (Test)
{
if (Application.platform == RuntimePlatform.Android)
{
adUnitId = "ca-app-pub-3940256099942544/6300978111";
}
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
//adUnitId = "ca-app-pub-3940256099942544/2934735716";
}
}
else
{
if (Application.platform == RuntimePlatform.Android)
{
adUnitId = Andriod_Banner_ID;
}
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
//adUnitId = IOS_Banner_ID;
}
}
//#if UNITY_ANDROID
// //string adUnitId = "ca-app-pub-5616490787498850/7462964263";
// //adUnitId = Andriod_Banner_ID;
//#elif UNITY_IPHONE
// string adUnitId = "ca-app-pub-3940256099942544/2934735716";
//#else
// string adUnitId = "unexpected_platform";
//#endif
//Create a 320x50 banner at the top of the screen.
this.bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);
//AdSize adSize = new AdSize(250, 250);
// this.bannerView = new BannerView(adUnitId, adSize, AdPosition.Top);
//Create a 320x50 banner ad at coordinate(0, 50) on screen.
//AdSize adSize = new AdSize(11, 20);
//this.bannerView = new BannerView(adUnitId, adSize, AdPosition.Top);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the banner with the request.
this.bannerView.LoadAd(request);
// Called when an ad request has successfully loaded.
this.bannerView.OnAdLoaded += this.HandleOnAdLoaded;
// Called when an ad request failed to load.
this.bannerView.OnAdFailedToLoad += this.HandleOnAdFailedToLoad;
// Called when an ad is clicked.
this.bannerView.OnAdOpening += this.HandleOnAdOpened;
// Called when the user returned from the app after an ad click.
this.bannerView.OnAdClosed += this.HandleOnAdClosed;
// Called when the ad click caused the user to leave the application.
this.bannerView.OnAdLeavingApplication += this.HandleOnAdLeavingApplication;
}
private void HandleOnAdLoaded(object sender, EventArgs args)
{
}
private void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
}
private void HandleOnAdOpened(object sender, EventArgs args)
{
bannerView.Destroy();
}
private void HandleOnAdClosed(object sender, EventArgs args)
{
Show_Ban();
}
private void HandleOnAdClosed1(object sender, EventArgs args)
{
}
private void HandleRewardedAdClosed(object sender, EventArgs args)
{
requestrewardedvideo();
Show_Ban();
}
private void HandleOnAdLeavingApplication(object sender, EventArgs args)
{
}
//public void HandleOnUserEarnedReward(object sender, EventArgs args)
//{
// requestrewardedvideo();
// string type = args.Type;
// double amount = args.Amount;
// MonoBehaviour.print(
// "HandleRewardedAdRewarded event received for "
// + amount.ToString() + " " + type);
//}
public void HandleOnUserEarnedReward(object sender, Reward args)
{
requestrewardedvideo();
string type = args.Type;
double amount = args.Amount;
MonoBehaviour.print(
"HandleRewardedAdRewarded event received for "
+ amount.ToString() + " " + type);
PlayerPrefs.SetInt("coins", PlayerPrefs.GetInt("coins") + 200);
}
public void HandleRewardedAdFailedToShow(object sender, AdErrorEventArgs args)
{
requestrewardedvideo();
}
void Update()
{
if (Req_Interstitial_Add == true)
{
this.RequestInterstitial();
Req_Interstitial_Add = false;
}
}
private void requestrewardedvideo()
{
string adUnitId = "";
if (Test)
{
if (Application.platform == RuntimePlatform.Android)
{
adUnitId = "ca-app-pub-3940256099942544/5224354917";
}
//if (Application.platform == RuntimePlatform.IPhonePlayer)
//{
// adUnitId = "ca-app-pub-3940256099942544/1712485313";
//}
}
else
{
if (Application.platform == RuntimePlatform.Android)
{
adUnitId = rewarded_ad_id;
}
//if (Application.platform == RuntimePlatform.IPhonePlayer)
//{
// adUnitId = IOS_Rewarded_ID;
//}
}
rewarded_ad = new RewardedAd(rewarded_ad_id);
rewarded_ad.OnUserEarnedReward += HandleOnUserEarnedReward;
rewarded_ad.OnAdClosed += HandleRewardedAdClosed;
rewarded_ad.OnAdFailedToShow += HandleRewardedAdFailedToShow;
AdRequest request = new AdRequest.Builder().Build();
rewarded_ad.LoadAd(request);
}
public void showrewardedvideo()
{
if (this.rewarded_ad.IsLoaded())
{
Destroy_Banner();
this.rewarded_ad.Show();
}
else
{
Advertisement.Show(rewarded_ad_id);
}
}
public void Destroy_Banner()
{
bannerView.Destroy();
}
private void Show_Ban()
{
this.RequestBanner();
}
public void Show_Interstitial_Ad()
{
//if (this.interstitial.IsLoaded())
//{
// this.interstitial.Show();
// Req_Interstitial_Add = true;
//}
//else
{
Advertisement.Show(interstitialID);
}
}
//For Unity
public void ShowInterstitial()
{
Advertisement.Show(interstitialID);
}
public void ShowRewardedVideo()
{
Advertisement.Show(rewardedVideoID);
}
public void HideBanner()
{
Advertisement.Banner.Hide();
}
public void OnUnityAdsReady(string placementID)
{
if (placementID == rewardedVideoID)
{
//watchRewardAd.interactable = true;
}
if (placementID == interstitialID)
{
//showInterstitial.interactable = true;
}
//if (placementID == bannerID)
//{
// Advertisement.Banner.SetPosition(BannerPosition.TOP_CENTER);
// Advertisement.Banner.Show(bannerID);
//}
}
public void OnUnityAdsDidFinish(string placementID, ShowResult showResult)
{
if (placementID == rewardedVideoID)
{
if (showResult == ShowResult.Finished)
{
GetReward();
}
else if (showResult == ShowResult.Skipped)
{
//Do nothing
}
else if (showResult == ShowResult.Failed)
{
//tell player ads failed
}
}
}
public void OnUnityAdsDidError(string message)
{
//Show or log the error here
}
public void OnUnityAdsDidStart(string placementID)
{
//Do this if ads starts
}
private void GetReward()
{
if (PlayerPrefs.HasKey("coins"))
{
int gemAmount = PlayerPrefs.GetInt("coins");
PlayerPrefs.SetInt("coins", gemAmount + 200);
}
else
{
PlayerPrefs.SetInt("coins", 200);
}
//gemsAmt.text = "coins: " + PlayerPrefs.GetInt("coins").ToString();
}
public void OnInitializationComplete()
{
// DebugLog("Init Success");
}
public void OnInitializationFailed(UnityAdsInitializationError error, string message)
{
// DebugLog($"Init Failed: [{error}]: {message}");
}
public void OnUnityAdsAdLoaded(string placementId)
{
// DebugLog($"Load Success: {placementId}");
}
public void OnUnityAdsFailedToLoad(string placementId, UnityAdsLoadError error, string message)
{
//DebugLog($"Load Failed: [{error}:{placementId}] {message}");
}
public void OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message)
{
//DebugLog($"OnUnityAdsShowFailure: [{error}]: {message}");
}
public void OnUnityAdsShowStart(string placementId)
{
// DebugLog($"OnUnityAdsShowStart: {placementId}");
}
public void OnUnityAdsShowClick(string placementId)
{
// DebugLog($"OnUnityAdsShowClick: {placementId}");
}
public void OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState)
{
// DebugLog($"OnUnityAdsShowComplete: [{showCompletionState}]: {placementId}");
}
//#endregion
}