using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class AdManager : MonoBehaviour
{
private InterstitialAd interstitial;
private RewardBasedVideoAd rewardBasedVideo;
private string adType;
public Button coins, rubies, upgrade, powerup;
public Text coolDown;
private DateTime currentDate, oldDate;
private TimeSpan timeAway, difference;
private bool countDown;
public GameObject msgPanel;
public Text msgTitle;
public Text msgBody;
// Use this for initialization
void Start()
{
#if UNITY_ANDROID
string appId = "ca-app-pub-***************~***********"; // Edited out my real appId for this post.
#elif UNITY_IPHONE
string appId = "unexpected_platform";
#else
string appId = "unexpected_platform";
#endif
//MobileAds.SetiOSAppPauseOnBackground(true);
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize(appId);
// Get singleton reward based video ad reference.
rewardBasedVideo = RewardBasedVideoAd.Instance;
// RewardBasedVideoAd is a singleton, so handlers should only be registered once.
this.rewardBasedVideo.OnAdLoaded += this.HandleRewardBasedVideoLoaded;
this.rewardBasedVideo.OnAdFailedToLoad += this.HandleRewardBasedVideoFailedToLoad;
this.rewardBasedVideo.OnAdOpening += this.HandleRewardBasedVideoOpened;
this.rewardBasedVideo.OnAdStarted += this.HandleRewardBasedVideoStarted;
this.rewardBasedVideo.OnAdRewarded += this.HandleRewardBasedVideoRewarded;
this.rewardBasedVideo.OnAdClosed += this.HandleRewardBasedVideoClosed;
this.rewardBasedVideo.OnAdLeavingApplication += this.HandleRewardBasedVideoLeftApplication;
RequestRewardBasedVideo();
}
void Update()
{
CheckTime();
if (countDown)
{
CountDown();
}
}
// Returns an ad request with custom ad targeting.
private AdRequest CreateAdRequest()
{
return new AdRequest.Builder().Build();
}
private void RequestInterstitial()
{
// These ad units are configured to always serve test ads.
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-3940256099942544/8691691433";
#elif UNITY_IPHONE
string adUnitId = "unexpected_platform";
#else
string adUnitId = "unexpected_platform";
#endif
// Clean up interstitial ad before creating a new one.
if (this.interstitial != null)
{
this.interstitial.Destroy();
}
// Create an interstitial.
this.interstitial = new InterstitialAd(adUnitId);
// Register for ad events.
this.interstitial.OnAdLoaded += this.HandleInterstitialLoaded;
this.interstitial.OnAdFailedToLoad += this.HandleInterstitialFailedToLoad;
this.interstitial.OnAdOpening += this.HandleInterstitialOpened;
this.interstitial.OnAdClosed += this.HandleInterstitialClosed;
this.interstitial.OnAdLeavingApplication += this.HandleInterstitialLeftApplication;
// Load an interstitial ad.
this.interstitial.LoadAd(this.CreateAdRequest());
}
private void RequestRewardBasedVideo()
{
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-3940256099942544/
5224354917";
#elif UNITY_IPHONE
string adUnitId = "unexpected_platform";
#else
string adUnitId = "unexpected_platform";
#endif
rewardBasedVideo.LoadAd(CreateAdRequest(), adUnitId);
}
private void ShowInterstitial()
{
if (interstitial.IsLoaded())
{
interstitial.Show();
}
else
{
MonoBehaviour.print("Interstitial is not ready yet");
}
}
private void ShowRewardBasedVideo()
{
if (rewardBasedVideo.IsLoaded())
{
rewardBasedVideo.Show();
}
else
{
msgPanel.SetActive(true);
msgTitle.text = "No ads ready!";
msgBody.text = "Sorry, There is not an ad ready for you at this time.. Try again!";
MonoBehaviour.print("Reward based video ad is not ready yet");
}
}
public void PlayRewardedVideo(string type)
{
adType = type;
Debug.Log("Attempting to fire reward ad request... IsLoaded = " + rewardBasedVideo.IsLoaded().ToString());
ShowRewardBasedVideo();
}
#region Interstitial callback handlers
public void HandleInterstitialLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleInterstitialLoaded event received");
}
public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print(
"HandleInterstitialFailedToLoad event received with message: " + args.Message);
}
public void HandleInterstitialOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleInterstitialOpened event received");
}
public void HandleInterstitialClosed(object sender, EventArgs args)
{
MonoBehaviour.print("HandleInterstitialClosed event received");
}
public void HandleInterstitialLeftApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleInterstitialLeftApplication event received");
}
#endregion
#region RewardBasedVideo callback handlers
public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLoaded event received");
}
public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print(
"HandleRewardBasedVideoFailedToLoad event received with message: " + args.Message);
}
public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoOpened event received");
}
public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoStarted event received");
}
public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoClosed event received");
this.RequestRewardBasedVideo();
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
string type = args.Type;
double amount = args.Amount;
MonoBehaviour.print(
"HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " + type);
if (type == "double")
{
Debug.Log("Double Coins! Working");
}
else if (type == "freecoins")
{
Debug.Log("Free Coins! Working");
Player.GiveCoins(1000);
SetDateTime();
UnlockButton(coins, rubies);
PlayerPrefs.SetString("CoinButton", "Locked");
}
else if (type == "freerubies")
{
Debug.Log("Free Rubies! Working");
Player.GiveRubies(5);
UnlockButton(rubies, upgrade);
PlayerPrefs.SetString("RubiesButton", "Locked");
}
else if (type == "freehydros")
{
Debug.Log("Free Hydros! Working");
Player.AddHydros(1);
UnlockButton(upgrade, powerup);
}
else if (type == "freenitros")
{
Debug.Log("Free Nitros! Working");
Player.AddNitros(1);
UnlockButton(powerup, null);
}
}
public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
}
#endregion
private void CheckTime()
{
//Store the current time when it starts
currentDate = System.DateTime.Now;
//Grab the old time from the player prefs as a long
long temp = Convert.ToInt64(PlayerPrefs.GetString("videoRewards"));
oldDate = DateTime.FromBinary(temp);
//Use the Subtract method and store the result as a timespan variable
difference = currentDate.Subtract(oldDate);
timeAway = difference;
}
private void SetDateTime()
{
PlayerPrefs.SetString("videoRewards", DateTime.Now.ToBinary().ToString());
}
private void CheckUnlock()
{
if (timeAway >= TimeSpan.FromHours(24))
{
countDown = false;
UnlockButtons();
coolDown.text = "Your daily rewards are ready to be claimed!";
}
else
{
countDown = true;
if (PlayerPrefs.GetString("CoinButton") == "Locked")
{
coins.interactable = false;
}
else
{
coins.interactable = true;
}
if (PlayerPrefs.GetString("RubiesButton") == "Locked")
{
rubies.interactable = false;
}
else
{
rubies.interactable = true;
}
if (PlayerPrefs.GetString("UpgradeButton") == "Locked")
{
upgrade.interactable = false;
}
else
{
upgrade.interactable = true;
}
if (PlayerPrefs.GetString("PowerupButton") == "Locked")
{
powerup.interactable = false;
}
else
{
powerup.interactable = true;
}
}
}
private void CountDown()
{
TimeSpan remaining = TimeSpan.FromHours(24) - timeAway;
string hours = string.Format("{0}", remaining.Hours);
string mins = string.Format("{0}", remaining.Minutes);
string secs = string.Format("{0}", remaining.Seconds);
if (remaining.Hours > 0)
{
if (coolDown != null)
coolDown.text = "Daily Rewards Unlock in " + hours + " hours, " + mins + " minutes, and " + secs + " seconds!";
}
else if (remaining.Minutes > 0)
{
if (coolDown != null)
coolDown.text = "Daily Rewards Unlock in " + mins + " minutes, and " + secs + " seconds!";
}
else
{
if (coolDown != null)
coolDown.text = "Daily Rewards Unlock in " + secs + " seconds!";
}
}
private void UnlockButtons()
{
coins.interactable = true;
rubies.interactable = false;
upgrade.interactable = false;
powerup.interactable = false;
PlayerPrefs.SetString("CoinButton", "Unlocked");
PlayerPrefs.SetString("RubiesButton", "Unlocked");
PlayerPrefs.SetString("UpgradeButton", "Unlocked");
PlayerPrefs.SetString("PowerupButton", "Unlocked");
}
public void UnlockButton(Button cbtn, Button nbtn)
{
Debug.Log("UnlockButton Called");
Debug.Log(cbtn);
Debug.Log(nbtn);
if (cbtn != null)
cbtn.interactable = false;
if (nbtn != null)
nbtn.interactable = true;
}
}