using System.Collections; using System.Collections.Generic; using UnityEngine; using GoogleMobileAds.Api; using System.Threading.Tasks; using System; using UnityEngine.Events; using UnityEngine.SceneManagement; public class AdsManager : MonoBehaviour { private static AdsManager _instance; public static AdsManager instance => _instance; private BannerView bannerView; private InterstitialAd interstitial; private RewardedAd rewardedAd; [Header("Ids")] [SerializeField]private string bannerId; [SerializeField]private string interstitialId; [SerializeField]private string rewardedId; public UnityEvent onAwardEarned = new UnityEvent(); public bool dontDestroyOnLoad=true; void Start() { _instance =this; if(dontDestroyOnLoad){DontDestroyOnLoad(gameObject);} MobileAds.Initialize(OnInit); } float t =0; void Update(){ if(t>0){ if(t< 30){ t += Time.deltaTime; }else{ SceneManager.LoadScene("Login"); t=-10; } } } void OnInit(InitializationStatus initStatus){ // MessageDialog.instance.ShowMessage("Debug","Ads inited"); SceneManager.LoadScene("Login"); Debug.Log("Google ads init,"+initStatus); Debug.Log("Loading ads now"); rewardedAd = new RewardedAd(rewardedId); rewardedAd.OnUserEarnedReward += OnRewardedComplete; rewardedAd.OnAdFailedToLoad += OnRewardedFailed; rewardedAd.OnAdFailedToShow += OnRewardedFailed; // RequestBanner(); LoadInterestitial(); LoadRewarded(); } void RequestBanner(){ bannerView = new BannerView(bannerId, AdSize.Banner, AdPosition.BottomLeft); AdRequest request = new AdRequest.Builder().Build(); this.bannerView.LoadAd(request); } void LoadInterestitial(){ interstitial = new InterstitialAd(interstitialId); // Create an empty ad request. AdRequest request = new AdRequest.Builder().Build(); // Load the interstitial with the request. interstitial.LoadAd(request); } void LoadRewarded(){ AdRequest request = new AdRequest.Builder().Build(); this.rewardedAd.LoadAd(request); } public async void ShowInterestitial(){ interstitial.Show(); await Task.Delay(10000); LoadInterestitial(); } public async void ShowRewarded(UnityAction OnAwardEarned){ // MessageDialog.instance.ShowMessage("Debug","Showing rewarded, is rewareded null? : " + (rewardedAd == null).ToString()); onAwardEarned.RemoveAllListeners(); onAwardEarned.AddListener(OnAwardEarned); Debug.Log("Show Reward : is ad null? : " + (rewardedAd == null).ToString()); if(!rewardedAd.IsLoaded()){ Debug.Log("Ad not loaded, Loading now"); LoadRewarded(); }else{ } while(!rewardedAd.IsLoaded()){ await Task.Delay(500); } Debug.Log("Ad ready, showing now"); this.rewardedAd.Show(); Debug.Log("Ad shown"); } void OnRewardedComplete(object sender, EventArgs args){ onAwardEarned.Invoke(); LoadRewarded(); } void OnRewardedFailed(object sender, EventArgs args){ MessageDialog.instance.ShowMessage("Sorry", "We couldn't Load the Ad right now. Please try again later."); LoadRewarded(); } void OnRewardCanceled(object sender, EventArgs args){ MessageDialog.instance.ShowMessage("Failed", "You closed the Ad. Please watch the full ad to receive the gift"); LoadRewarded(); } }