Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using GoogleMobileAds;
- using GoogleMobileAds.Api;
- using System;
- using UnityEngine.UI;
- using UnityEngine.Advertisements;
- using UnityEngine.Purchasing;
- using Tapdaq;
- public class MyAdManager : MonoBehaviour {
- public static MyAdManager instance;
- public bool initAdmob;
- [SerializeField]
- bool testing;
- [SerializeField]
- string androidAppId;
- [SerializeField]
- string IosAppId;
- [SerializeField]
- string bannerAdId;
- [SerializeField]
- string iosBannerAdId;
- [SerializeField]
- string interstitialAdId;
- [SerializeField]
- string iosInterstitialAdId;
- [SerializeField]
- string rewardedSpin;
- [SerializeField]
- string iosRewardedSpin;
- [SerializeField]
- int rewardedAdRewardInt;
- [SerializeField]
- Animator doubleCoinAnim;
- public int tripleCoinRewardAmount;
- [SerializeField]
- RectTransform bannerSim;
- private BannerView bannerView;
- private InterstitialAd interstitial;
- private RewardBasedVideoAd rewardBasedVideo;
- private void Awake()
- {
- if (instance == null)
- {
- instance = this;
- }
- else
- {
- Debug.Log("There's Already an ad Manager, Destroy This one at " + gameObject.name);
- Destroy(this);
- }
- #if UNITY_IOS
- //Advertisement.Initialize("2650279");
- #elif UNITY_ANDROID
- //if(initAdmob)
- //{
- //Advertisement.Initialize("2650277");
- //}
- #endif
- }
- public void Start()
- {
- //InitAppID();
- //GameHandler.OnGameStart += ShowBanner;
- ////GameHandler.OnGameEnd += ShowInterstitial;
- //GameHandler.OnGameEnd += HideBanner;
- AdManager.Init();
- //Debug.Log("TapDaq Called");
- //AdManager.InitWithConsent (TDStatus.TRUE, TDStatus.FALSE, TDStatus.UNKNOWN);//IsUserSubjectToGDPR, UserConsent, isAgeRestrictedUser
- }
- private void InitAppID()
- {
- string appId = "ca-app-pub-2629468278215471~2139414196";
- #if UNITY_ANDROID
- if(!testing)
- {
- appId = androidAppId;
- }
- #elif UNITY_IPHONE
- if(!testing)
- {
- appId = androidAppId;
- }
- #else
- string appId = "unexpected_platform";
- #endif
- // Initialize the Google Mobile Ads SDK.
- MobileAds.Initialize(appId);
- //Req Banner
- this.RequestBanner();
- HideBanner();
- RequestInterstitial();
- //Req Rewarded Video
- rewardBasedVideo = RewardBasedVideoAd.Instance;
- RequestRewardedVideo();
- rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
- }
- private void RequestBanner()
- {
- string adUnitId = "ca-app-pub-2629468278215471/5385429561";
- #if UNITY_ANDROID
- if (!testing)
- {
- adUnitId = bannerAdId;
- }
- #elif UNITY_IPHONE
- if(!testing)
- {
- adUnitId = iosBannerAdId;
- }
- #else
- adUnitId = "unexpected_platform";
- #endif
- // Create a 320x50 banner at the top of the screen.
- Vector2 adsizeVector = new Vector2(Screen.width, Screen.height / 21);
- AdSize adSize = new AdSize((int)adsizeVector.x, (int)adsizeVector.y);
- bannerSim.sizeDelta = adsizeVector;
- bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
- // Create an empty ad request.
- AdRequest request = new AdRequest.Builder().Build();
- // Load the banner with the request.
- bannerView.LoadAd(request);
- }
- private void RequestInterstitial()
- {
- string adUnitId = "ca-app-pub-3940256099942544/1033173712";
- #if UNITY_ANDROID
- if(!testing)
- {
- adUnitId = interstitialAdId;
- }
- #elif UNITY_IPHONE
- if(!testing)
- {
- adUnitId = iosInterstitialAdId;
- }
- #else
- adUnitId = "unexpected_platform";
- #endif
- // Initialize an InterstitialAd.
- interstitial = new InterstitialAd(adUnitId);
- // Create an empty ad request.
- AdRequest request = new AdRequest.Builder().Build();
- // Load the interstitial with the request.
- interstitial.LoadAd(request);
- }
- private void RequestRewardedVideo()
- {
- string adUnitId = "ca-app-pub-3940256099942544/5224354917";
- #if UNITY_ANDROID
- if(!testing)
- {
- adUnitId = rewardedSpin;
- }
- #elif UNITY_IPHONE
- if(!testing)
- {
- adUnitId = iosRewardedSpin;
- }
- #else
- string adUnitId = "unexpected_platform";
- #endif
- // Create an empty ad request.
- AdRequest request = new AdRequest.Builder().Build();
- // Load the rewarded video ad with the request.
- this.rewardBasedVideo.LoadAd(request, adUnitId);
- //IOS not firing Rewarded Properly Handler
- #if UNITY_IOS
- rewardBasedVideo.OnAdRewarded += IOSReward();
- #endif
- }
- private EventHandler<Reward> IOSReward()
- {
- HandleRewards();
- return null;
- }
- public void HandleRewardBasedVideoRewarded(object sender, Reward args)
- {
- //AudioManager.instance.MuteMusic();
- string type = args.Type;
- double amount = args.Amount;
- MonoBehaviour.print(
- "HandleRewardBasedVideoRewarded event received for "
- + amount.ToString() + " " + type);
- //GameSlyce.LuckyWheel.instance.Spins = 1;
- HandleRewards();
- }
- private void HandleRewards()
- {
- //Convert this to switch later
- Dictionary<string, string> gameEvent = new Dictionary<string, string>();
- switch (rewardedAdRewardInt)
- {
- case 1:
- // admob set 1 == 60 Gold
- PlayerInfo.instance.AddCurrency(20, true);
- gameEvent = new Dictionary<string, string>();
- gameEvent.Add("RewardedAd Shown :", GameHandler.instance.currentLvlNum.ToString());
- GameHandler.instance.SendAnalyticsEvent("Ad : Rewarded Spin Coins", gameEvent);
- break;
- case 2:
- //Triple it
- PlayerInfo.instance.AddCurrency(tripleCoinRewardAmount);
- GameHandler.instance.SpawnCoinsNoValue(GameHandler.instance.UIAnim.gameObject, PlayerInfo.instance.gameplayCurrencyObj.gameObject);
- gameEvent = new Dictionary<string, string>();
- gameEvent.Add("RewardedAd Shown :", GameHandler.instance.currentLvlNum.ToString());
- GameHandler.instance.SendAnalyticsEvent("Ad : Rewarded Double Coins", gameEvent);
- break;
- case 3:
- //Free Hint Coins
- int goldReward = UnityEngine.Random.Range(60, 80);
- PlayerInfo.instance.AddCurrency(goldReward);
- GameHandler.instance.freeHintWindow.GetComponent<Animator>().SetBool("Active", true);
- GameHandler.instance.freeHintGoldAmount.text = goldReward.ToString();
- GameHandler.instance.SpawnCoinsNoValue(GameHandler.instance.freeHintWindow.gameObject, PlayerInfo.instance.gameplayCurrencyObj.gameObject);
- GameHandler.instance.SendAnalyticsEvent("Ad : Rewarded Free Hint = Coins", new Dictionary<string, string>());
- break;
- case 4:
- //Gift Hint Use Reward
- if (GameHandler.instance.inGame)
- {
- GameHandler.instance.UseHint();
- gameEvent = new Dictionary<string, string>();
- gameEvent.Add("RewardedAd Shown :", GameHandler.instance.currentLvlNum.ToString());
- GameHandler.instance.SendAnalyticsEvent("Ad : Rewarded Free Hint Used", gameEvent);
- }
- break;
- case 5:
- InGameStoreManager.instance.RewardWatchedAd();
- gameEvent = new Dictionary<string, string>();
- gameEvent.Add("RewardedAd Shown :", GameHandler.instance.currentLvlNum.ToString());
- GameHandler.instance.SendAnalyticsEvent("Ad : Rewarded,Player Store Hour Decrease", gameEvent);
- break;
- default:
- Debug.LogError("Reward Num Not Implemented.");
- break;
- }
- //if (rewardedAdRewardInt == 1)
- //{
- // // admob set 1 == 60 Gold
- // PlayerInfo.instance.AddCurrency(20, true);
- // Dictionary<string, string> gameEvent = new Dictionary<string, string>();
- // gameEvent.Add("RewardedAd Shown :", GameHandler.instance.currentLvlNum.ToString());
- // GameHandler.instance.SendAnalyticsEvent("Ad : Rewarded Spin Coins", gameEvent);
- //}
- //else if (rewardedAdRewardInt == 2)
- //{
- // //Admob Set Double Coins
- // PlayerInfo.instance.AddCurrency(tripleCoinRewardAmount);
- // GameHandler.instance.SpawnCoinsNoValue(GameHandler.instance.UIAnim.gameObject, PlayerInfo.instance.gameplayCurrencyObj.gameObject);
- // Dictionary<string, string> gameEvent = new Dictionary<string, string>();
- // gameEvent.Add("RewardedAd Shown :", GameHandler.instance.currentLvlNum.ToString());
- // GameHandler.instance.SendAnalyticsEvent("Ad : Rewarded Double Coins", gameEvent);
- //}
- //else if (rewardedAdRewardInt == 3)
- //{
- // //Gift Triple Reward
- // int goldReward = UnityEngine.Random.Range(30, 50);
- // PlayerInfo.instance.AddCurrency(goldReward);
- // GameHandler.instance.freeHintWindow.GetComponent<Animator>().SetBool("Active",true);
- // GameHandler.instance.freeHintGoldAmount.text = goldReward.ToString();
- // GameHandler.instance.SpawnCoinsNoValue(GameHandler.instance.freeHintWindow.gameObject, PlayerInfo.instance.gameplayCurrencyObj.gameObject);
- // GameHandler.instance.SendAnalyticsEvent("Ad : Rewarded Free Hint = Coins", new Dictionary<string, string>());
- //}
- //else if (rewardedAdRewardInt == 4)
- //{
- // //Gift Hint Use Reward
- // if (GameHandler.instance.inGame)
- // {
- // GameHandler.instance.UseHint();
- // Dictionary<string, string> gameEvent = new Dictionary<string, string>();
- // gameEvent.Add("RewardedAd Shown :", GameHandler.instance.currentLvlNum.ToString());
- // GameHandler.instance.SendAnalyticsEvent("Ad : Rewarded Free Hint Used", gameEvent);
- // }
- //}
- }
- // public void ShowInterstitial()
- // {
- // if (PlayerInfo.instance.playerData.showAds && GameHandler.instance.currentLvlNum % 2 == 0 && GameHandler.instance.currentLvlNum > 7)
- // {
- //#if UNITY_IOS
- // ShowUnityInterstitialVideo();
- //#else
- // if (interstitial.IsLoaded())
- // {
- // interstitial.Show();
- // RequestInterstitial();
- // }
- // else
- // {
- // //ShowUnityInterstitialVideo();
- // }
- //#endif
- // Dictionary<string, string> gameEvent = new Dictionary<string, string>();
- // gameEvent.Add("Interstitial Shown :", GameHandler.instance.currentLvlNum.ToString());
- // GameHandler.instance.SendAnalyticsEvent("Ad : Interstitial", gameEvent);
- // }
- // //if (PlayerInfo.instance.playerData.showAds && GameHandler.instance.currentLvlNum > 4)
- // //{
- // // ShowUnityInterstitialVideo();
- // //}
- // //StartCoroutine(TurnMusicOnTouch());
- // }
- //public void HideBanner()
- //{
- // //bannerSim.gameObject.SetActive(false);
- // bannerView.Hide();
- //}
- //public void ShowBanner()
- //{
- // //bannerSim.gameObject.SetActive(true);
- // if(PlayerInfo.instance.playerData.showAds)
- // {
- // bannerView.Show();
- // }
- //}
- public void ShowDoubleCoins()
- {
- //The Amount is set by the Level Complete Function
- //Disabled, now shown in Level Complete Screen
- doubleCoinAnim.GetComponentInChildren<Button>().interactable = true;
- doubleCoinAnim.SetTrigger("Appear");
- Invoke("HideDoubleCoins", 7f);
- }
- public void HideDoubleCoins()
- {
- doubleCoinAnim.SetTrigger("Appear");
- doubleCoinAnim.GetComponentInChildren<Button>().interactable = false;
- }
- #region UnityAds
- //void ShowUnityRewardedVideo()
- //{
- // ShowOptions options = new ShowOptions();
- // options.resultCallback = HandleRewardedResult;
- // Advertisement.Show("rewardedVideo", options);
- //}
- //void HandleRewardedResult(ShowResult result)
- //{
- // if (result == ShowResult.Finished)
- // {
- // Debug.Log("Video completed - Offer a reward to the player");
- // // Reward your player here.
- // HandleRewards();
- // }
- // else if (result == ShowResult.Skipped)
- // {
- // Debug.LogWarning("Video was skipped - Do NOT reward the player");
- // }
- // else if (result == ShowResult.Failed)
- // {
- // Debug.LogError("Video failed to show");
- // }
- //}
- //void ShowUnityInterstitialVideo()
- //{
- // ShowOptions options = new ShowOptions();
- // options.resultCallback = HandleInterstitialResult;
- // Advertisement.Show("video", options);
- //}
- //void HandleInterstitialResult(ShowResult result)
- //{
- // if (result == ShowResult.Finished)
- // {
- // Debug.Log("Video completed - Offer a reward to the player");
- // // Reward your player here.
- // }
- // else if (result == ShowResult.Skipped)
- // {
- // Debug.LogWarning("Video was skipped - Do NOT reward the player");
- // }
- // else if (result == ShowResult.Failed)
- // {
- // Debug.LogError("Video failed to show");
- // }
- //}
- #endregion
- private void OnEnable()
- {
- TDCallbacks.TapdaqConfigLoaded += OnTapdaqConfigLoaded;
- TDCallbacks.TapdaqConfigFailedToLoad += OnTapdaqConfigFailToLoad;
- TDCallbacks.RewardVideoValidated += OnRewardVideoValidated;
- TDCallbacks.AdAvailable += OnAdAvailable;
- TDCallbacks.AdNotAvailable += OnAdNotAvailable;
- TDCallbacks.AdClicked += OnAdClicked;
- }
- private void OnDisable()
- {
- TDCallbacks.TapdaqConfigLoaded -= OnTapdaqConfigLoaded;
- TDCallbacks.TapdaqConfigFailedToLoad -= OnTapdaqConfigFailToLoad;
- TDCallbacks.RewardVideoValidated -= OnRewardVideoValidated;
- TDCallbacks.AdAvailable -= OnAdAvailable;
- TDCallbacks.AdNotAvailable -= OnAdNotAvailable;
- TDCallbacks.AdClicked -= OnAdClicked;
- }
- private void OnTapdaqConfigLoaded()
- {
- //Tapdaq Ready to use
- Debug.Log("Loading Ads Called");
- AdManager.LoadRewardedVideo("game_over");
- AdManager.LoadVideo("main_menu");
- AdManager.RequestBanner(TDMBannerSize.TDMBannerStandard);
- }
- private void OnTapdaqConfigFailToLoad(TDAdError error)
- {
- //Tapdaq failed to initialise
- Debug.LogError("TapDaq Failed To Load");
- Debug.LogError("TapDaq : " + error.message);
- foreach (KeyValuePair<string, Tapdaq.TDAdError> attachStat in error.subErrors)
- {
- //Now you can access the key and value both separately from this attachStat as:
- Debug.LogError("Key : " + attachStat.Key);
- Debug.LogError("Value : " + attachStat.Value);
- }
- }
- void OnApplicationPause(bool pauseStatus)
- {
- AdManager.OnApplicationPause(pauseStatus);
- }
- private void OnAdAvailable(TDAdEvent e)
- {
- // Ad has loaded, can now be displayed.
- // This method will also be called when a banner is refreshed every 30 seconds even if the banner is already in view. Calling show again is safe, alternatively TDAdEvent message property will note either "LOADED" or "REFRESH"
- Debug.Log("Calling OnAdAvailable");
- if (e.adType == "BANNER")
- {
- AdManager.ShowBanner(TDBannerPosition.Bottom);
- }
- if (e.adType == "REWARD_AD" && e.tag == "game_over")
- {
- // Add code here to display or enable your Rewarded Video button.
- }
- }
- public void OnAdNotAvailable(TDAdEvent adEvent)
- {
- // Ad has failed to load/refresh
- }
- public void OnAdClicked(TDAdEvent adEvent)
- {
- // Ad has been clicked
- }
- public void ShowInterstitial()
- {
- if (AdManager.IsVideoReady("main_menu"))
- {
- AdManager.ShowVideo("main_menu");
- }
- }
- public void ShowBanner()
- {
- // Ad has loaded, can now be displayed.
- // This method will also be called when a banner is refreshed every 30 seconds even if the banner is already in view. Calling show again is safe, alternatively TDAdEvent message property will note either "LOADED" or "REFRESH"
- if (AdManager.IsBannerReady())
- {
- AdManager.ShowBanner(TDBannerPosition.Bottom);
- }
- }
- public void HideBanner()
- {
- AdManager.HideBanner();
- }
- public void ShowRewardedAd(int _rewardId)
- {
- rewardedAdRewardInt = _rewardId;
- if (AdManager.IsRewardedVideoReady("game_over"))
- {
- AdManager.ShowRewardVideo("game_over");
- }
- }
- private void OnRewardVideoValidated(TDVideoReward videoReward)
- {
- Debug.Log("I got " + videoReward.RewardAmount + " of " + videoReward.RewardName
- + " tag=" + videoReward.Tag + " IsRewardValid " + videoReward.RewardValid + " CustomJson: " + videoReward.RewardJson);
- if (videoReward.RewardValid)
- {
- //Give Reward
- HandleRewards();
- Dictionary<string, string> gameEvent = new Dictionary<string, string>();
- gameEvent.Add("RewardedAd Shown :", GameHandler.instance.currentLvlNum.ToString());
- GameHandler.instance.SendAnalyticsEvent("Ad : Rewarded Total", gameEvent);
- }
- else
- {
- //Reward is invalid, video may not have completed or an ad network may have refused to the provide reward
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement