Quantcast
Viewing all articles
Browse latest Browse all 137

App crash when there is no internet cause by admob

here is the code using UnityEngine.Events; using UnityEngine; using GoogleMobileAds.Api; using GoogleMobileAds.Common; using UnityEngine.UI; using System; using System.Collections.Generic; public class Addmanager : MonoBehaviour { string GameID = "ca-app-pub-9533178649333744~4108135359"; // Sample ads string bannerAdId = "ca-app-pub-3940256099942544/1033173712";// test unit only add the real one on game string InterstitialAdID = "ca-app-pub-3940256099942544/6300978111";// test unit only add the real one on game string rewarded_Ad_ID = "ca-app-pub-3940256099942544/5224354917";// test unit only add the real one on game // string rewarded_Ad_ID = "ca-app-pub-9533178649333744/1663781322";enable this on game public GameObject Nointernet; public BannerView bannerAd; public InterstitialAd interstitial; public RewardBasedVideoAd rewardedAd; public QuestAndPopups questAndPopups; public GameObject rewardMessage; public GameObject rewardad1; private int rewardType; public static Addmanager instance; private void Awake() { if (Application.internetReachability != NetworkReachability.NotReachable) { if (instance != null && instance != this) { Destroy(gameObject); return; } instance = this; DontDestroyOnLoad(this); rewardedAd = RewardBasedVideoAd.Instance; } } // Start is called before the first frame update void Start() { if(Application.internetReachability != NetworkReachability.NotReachable) { MobileAds.Initialize(initStatus => { }); loadRewardVideo(); } } #region rewarded Video Ads public void loadRewardVideo() {if (Application.internetReachability != NetworkReachability.NotReachable) { MobileAds.Initialize(initStatus => { }); rewardedAd.LoadAd(new AdRequest.Builder().Build(), rewarded_Ad_ID); rewardedAd.OnAdLoaded += HandleRewardedAdLoaded; rewardedAd.OnAdClosed += HandleRewardedAdClosed; rewardedAd.OnAdOpening += HandleRewardedAdOpening; rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad; rewardedAd.OnAdRewarded += HandleUserEarnedReward; rewardedAd.OnAdLeavingApplication += HandleOnRewardAdleavingApp; } } /// rewarded video events ////////////////////////////////////////////// public event EventHandler OnAdLoaded; public event EventHandler OnAdFailedToLoad; public event EventHandler OnAdOpening; public event EventHandler OnAdStarted; public event EventHandler OnAdClosed; public event EventHandler OnAdRewarded; public event EventHandler OnAdLeavingApplication; public event EventHandler OnAdCompleted; /// Rewared events ////////////////////////// public void HandleRewardedAdLoaded(object sender, EventArgs args) { Debug.Log("Video Loaded"); } public void HandleRewardedAdFailedToLoad(object sender, AdFailedToLoadEventArgs args) { Nointernet.SetActive(true); Invoke("DestroyInternet", 2.2f); } public void HandleRewardedAdOpening(object sender, EventArgs args) { Debug.Log("Video Loading"); } public void HandleRewardedAdFailedToShow(object sender, AdErrorEventArgs args) { Nointernet.SetActive(true); Invoke("DestroyInternet", 2.2f); } public void HandleRewardedAdClosed(object sender, EventArgs args) { Debug.Log("Video Loading failed"); } void DestroyInternet() { Nointernet.SetActive(false); } public void HandleUserEarnedReward(object sender, Reward args) { /// reward the player here -------------------- //Shoopingreward.instance.rewaredPlayer(); rewardType = PlayerPrefs.GetInt("rewardType"); if (rewardType == 1) { questAndPopups.ArmorColorDiscounter(); } if (rewardType == 2) { questAndPopups.UpgradeArmorDC(); } if(rewardType == 3) { questAndPopups.Weapon1Rewards(); } if (rewardType == 4) { questAndPopups.Weapon2Rewards(); } if (rewardType == 5) { questAndPopups.Weapon3rewards(); } ShowrewardMessage(); OnDestroy(); } public void HandleOnRewardAdleavingApp(object sender, EventArgs args) { //Debug.Log("when user clicks the video and open a new window"); } void ShowrewardMessage() { rewardMessage.SetActive(true); Invoke("DeleteRewardMessage", 2.2f); } void DeleteRewardMessage() { rewardMessage.SetActive(false); } public void showVideoAd() { if (Application.internetReachability != NetworkReachability.NotReachable) { Debug.Log("appreward2"); if (rewardedAd.IsLoaded()) { rewardedAd.Show(); } else { Debug.Log("Rewarded Video ad not loaded"); } } } #endregion #region banner public void reqBannerAd() { this.bannerAd = new BannerView(bannerAdId, AdSize.Banner, AdPosition.Bottom); // Called when an ad request has successfully loaded. this.bannerAd.OnAdLoaded += this.HandleOnAdLoaded; // Called when an ad request failed to load. this.bannerAd.OnAdFailedToLoad += this.HandleOnAdFailedToLoad; AdRequest request = new AdRequest.Builder().Build(); this.bannerAd.LoadAd(request); } public void hideBanner() { this.bannerAd.Hide(); } #endregion #region interstitial public void requestInterstital() { this.interstitial = new InterstitialAd(InterstitialAdID); this.interstitial.OnAdLoaded += this.HandleOnAdLoaded; // Called when an ad request failed to load. this.interstitial.OnAdFailedToLoad += this.HandleOnAdFailedToLoad; // Called when an ad is clicked. this.interstitial.OnAdOpening += this.HandleOnAdOpened; // Called when the user returned from the app after an ad click. this.interstitial.OnAdClosed += this.HandleOnAdClosed; // Called when the ad click caused the user to leave the application. this.interstitial.OnAdLeavingApplication += this.HandleOnAdLeavingApplication; AdRequest request = new AdRequest.Builder().Build(); this.interstitial.LoadAd(request); } private void OnDestroy() { // Unsubscribe from reward video event rewardedAd.OnAdLoaded -= HandleRewardedAdLoaded; rewardedAd.OnAdClosed -= HandleRewardedAdClosed; rewardedAd.OnAdOpening -= HandleRewardedAdOpening; rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad; rewardedAd.OnAdRewarded -= HandleUserEarnedReward; rewardedAd.OnAdLeavingApplication -= HandleOnRewardAdleavingApp; Debug.Log("ad destroyed"); loadRewardVideo(); } public void ShowInterstitialAd() { if (this.interstitial.IsLoaded()) { this.interstitial.Show(); } } #endregion #region adDelegates //Delegates that i dont know public void HandleOnAdLoaded(object sender, EventArgs args) { Debug.Log("Ad Loaded"); } public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args) { } public void HandleOnAdOpened(object sender, EventArgs args) { MonoBehaviour.print("HandleAdOpened event received"); } public void HandleOnAdClosed(object sender, EventArgs args) { Debug.Log("Ad Closed"); //requestInterstital(); // Optional : in case you want to load another interstial ad rightaway } public void HandleOnAdLeavingApplication(object sender, EventArgs args) { MonoBehaviour.print("HandleAdLeavingApplication event received"); } #endregion }

Viewing all articles
Browse latest Browse all 137

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>