AdsManager.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using GoogleMobileAds.Api;
  5. using System.Threading.Tasks;
  6. using System;
  7. using UnityEngine.Events;
  8. public class AdsManager : MonoBehaviour
  9. {
  10. private static AdsManager _instance;
  11. public static AdsManager instance => _instance;
  12. private BannerView bannerView;
  13. private InterstitialAd interstitial;
  14. private RewardedAd rewardedAd;
  15. [Header("Ids")]
  16. [SerializeField]private string bannerId;
  17. [SerializeField]private string interstitialId;
  18. [SerializeField]private string rewardedId;
  19. public UnityEvent onAwardEarned = new UnityEvent();
  20. public bool dontDestroyOnLoad=true;
  21. void Start()
  22. {
  23. _instance =this;
  24. if(dontDestroyOnLoad){DontDestroyOnLoad(gameObject);}
  25. MobileAds.Initialize(OnInit);
  26. }
  27. void OnInit(InitializationStatus initStatus){
  28. // MessageDialog.instance.ShowMessage("Debug","Ads inited");
  29. DevLog.Log("Ads initiated, Loading ads now");
  30. Debug.Log("Google ads init,"+initStatus);
  31. Debug.Log("Loading ads now");
  32. rewardedAd = new RewardedAd(rewardedId);
  33. rewardedAd.OnUserEarnedReward += OnRewardedComplete;
  34. rewardedAd.OnAdFailedToLoad += OnRewardedFailed;
  35. rewardedAd.OnAdFailedToShow += OnRewardedFailed;
  36. rewardedAd.OnAdClosed += OnRewardCanceled;
  37. RequestBanner();
  38. LoadInterestitial();
  39. LoadRewarded();
  40. }
  41. void RequestBanner(){
  42. DevLog.Log("Loading Banner");
  43. try{
  44. bannerView = new BannerView(bannerId, AdSize.Banner, AdPosition.BottomLeft);
  45. AdRequest request = new AdRequest.Builder().Build();
  46. this.bannerView.LoadAd(request);
  47. }catch(Exception e){
  48. DevLog.Log(e.Message);
  49. }
  50. }
  51. public void LoadInterestitial(){
  52. DevLog.Log("Loading Interestitial");
  53. try{
  54. interstitial = new InterstitialAd(interstitialId);
  55. // Create an empty ad request.
  56. AdRequest request = new AdRequest.Builder().Build();
  57. // Load the interstitial with the request.
  58. interstitial.LoadAd(request);
  59. }catch(Exception e){
  60. DevLog.Log(e.Message);
  61. }
  62. }
  63. public void LoadRewarded(){
  64. DevLog.Log("Loading Rewarded");
  65. try{
  66. AdRequest request = new AdRequest.Builder().Build();
  67. this.rewardedAd.LoadAd(request);
  68. }catch(Exception e){
  69. DevLog.Log(e.Message);
  70. }
  71. }
  72. public async void ShowInterestitial(){
  73. DevLog.Log("Showing Interestitial");
  74. interstitial.Show();
  75. await Task.Delay(10000);
  76. LoadInterestitial();
  77. }
  78. public void ShowRewarded(){
  79. ShowRewarded(()=>{DevLog.Log("I Earned this, Back off");});
  80. }
  81. public async void ShowRewarded(UnityAction OnAwardEarned){
  82. // MessageDialog.instance.ShowMessage("Debug","Showing rewarded, is rewareded null? : " + (rewardedAd == null).ToString());
  83. DevLog.Log("Showing Rewarded, Rewarded ad null? : " + (rewardedAd==null).ToString());
  84. try{
  85. onAwardEarned.RemoveAllListeners();
  86. onAwardEarned.AddListener(OnAwardEarned);
  87. Debug.Log("Show Reward : is ad null? : " + (rewardedAd == null).ToString());
  88. if(!rewardedAd.IsLoaded()){
  89. Debug.Log("Ad not loaded, Loading now");
  90. LoadRewarded();
  91. }else{
  92. }
  93. while(!rewardedAd.IsLoaded()){
  94. await Task.Delay(500);
  95. }
  96. Debug.Log("Ad ready, showing now");
  97. this.rewardedAd.Show();
  98. Debug.Log("Ad shown");
  99. }catch(Exception e){
  100. DevLog.Log(e.Message);
  101. }
  102. }
  103. void OnRewardedComplete(object sender, EventArgs args){
  104. onAwardEarned.Invoke();
  105. LoadRewarded();
  106. }
  107. void OnRewardedFailed(object sender, EventArgs args){
  108. DevLog.Log("Ad failed to load");
  109. // MessageDialog.instance.ShowMessage("Sorry", "We couldn't Load the Ad right now. Please try again later.");
  110. LoadRewarded();
  111. }
  112. void OnRewardCanceled(object sender, EventArgs args){
  113. DevLog.Log("User canceled the ad");
  114. // MessageDialog.instance.ShowMessage("Failed", "You closed the Ad. Please watch the full ad to receive the gift");
  115. LoadRewarded();
  116. }
  117. }