├── Admob_all_ads__Banner-Interstitial-Reward └── Admob.cs ├── Admob_banner └── Admob.cs ├── Admob_interstitial └── Admob.cs ├── Admob_reward └── Admob.cs └── README.md /Admob_all_ads__Banner-Interstitial-Reward/Admob.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using GoogleMobileAds.Api; 3 | using System; 4 | using UnityEngine.UI; 5 | 6 | public class Admob : MonoBehaviour 7 | { 8 | private BannerView adBanner; 9 | private InterstitialAd adInterstitial; 10 | private RewardBasedVideoAd adReward; 11 | 12 | private string idApp, idBanner, idInterstitial, idReward; 13 | 14 | [SerializeField] Button BtnInterstitial; 15 | [SerializeField] Button BtnReward; 16 | [SerializeField] Text TxtPoints; 17 | 18 | 19 | void Start () 20 | { 21 | BtnInterstitial.interactable = false; 22 | 23 | idApp = "ca-app-pub-3940256099942544~3347511713"; 24 | idBanner = "ca-app-pub-3940256099942544/6300978111"; 25 | idInterstitial = "ca-app-pub-3940256099942544/1033173712"; 26 | idReward = "ca-app-pub-3940256099942544/5224354917"; 27 | 28 | adReward = RewardBasedVideoAd.Instance; 29 | 30 | MobileAds.Initialize (idApp); 31 | 32 | RequestBannerAd (); 33 | RequestInterstitialAd (); 34 | } 35 | 36 | 37 | 38 | #region Banner Methods -------------------------------------------------- 39 | 40 | public void RequestBannerAd () 41 | { 42 | adBanner = new BannerView (idBanner, AdSize.Banner, AdPosition.Bottom); 43 | AdRequest request = AdRequestBuild (); 44 | adBanner.LoadAd (request); 45 | } 46 | 47 | public void DestroyBannerAd () 48 | { 49 | if (adBanner != null) 50 | adBanner.Destroy (); 51 | } 52 | 53 | #endregion 54 | 55 | #region Interstitial methods --------------------------------------------- 56 | 57 | public void RequestInterstitialAd () 58 | { 59 | adInterstitial = new InterstitialAd (idInterstitial); 60 | AdRequest request = AdRequestBuild (); 61 | adInterstitial.LoadAd (request); 62 | 63 | //attach events 64 | adInterstitial.OnAdLoaded += this.HandleOnAdLoaded; 65 | adInterstitial.OnAdOpening += this.HandleOnAdOpening; 66 | adInterstitial.OnAdClosed += this.HandleOnAdClosed; 67 | } 68 | 69 | public void ShowInterstitialAd () 70 | { 71 | if (adInterstitial.IsLoaded ()) 72 | adInterstitial.Show (); 73 | } 74 | 75 | public void DestroyInterstitialAd () 76 | { 77 | adInterstitial.Destroy (); 78 | } 79 | 80 | //interstitial ad events 81 | public void HandleOnAdLoaded (object sender, EventArgs args) 82 | { 83 | //this method executes when interstitial ad is Loaded and ready to show 84 | BtnInterstitial.interactable = true; //button is ready to click (enabled) 85 | } 86 | 87 | public void HandleOnAdOpening (object sender, EventArgs args) 88 | { 89 | //this method executes when interstitial ad is shown 90 | BtnInterstitial.interactable = false; //disable the button 91 | } 92 | 93 | public void HandleOnAdClosed (object sender, EventArgs args) 94 | { 95 | //this method executes when interstitial ad is closed 96 | adInterstitial.OnAdLoaded -= this.HandleOnAdLoaded; 97 | adInterstitial.OnAdOpening -= this.HandleOnAdOpening; 98 | adInterstitial.OnAdClosed -= this.HandleOnAdClosed; 99 | 100 | RequestInterstitialAd (); //request new interstitial ad after close 101 | } 102 | 103 | #endregion 104 | 105 | #region Reward video methods --------------------------------------------- 106 | 107 | public void RequestRewardAd () 108 | { 109 | AdRequest request = AdRequestBuild (); 110 | adReward.LoadAd (request, idReward); 111 | 112 | adReward.OnAdLoaded += this.HandleOnRewardedAdLoaded; 113 | adReward.OnAdRewarded += this.HandleOnAdRewarded; 114 | adReward.OnAdClosed += this.HandleOnRewardedAdClosed; 115 | } 116 | 117 | public void ShowRewardAd () 118 | { 119 | if (adReward.IsLoaded ()) 120 | adReward.Show (); 121 | } 122 | //events 123 | public void HandleOnRewardedAdLoaded (object sender, EventArgs args) 124 | {//ad loaded 125 | ShowRewardAd (); 126 | 127 | } 128 | 129 | public void HandleOnAdRewarded (object sender, EventArgs args) 130 | {//user finished watching ad 131 | int points = int.Parse (TxtPoints.text); 132 | points += 50; //add 50 points 133 | TxtPoints.text = points.ToString (); 134 | } 135 | 136 | public void HandleOnRewardedAdClosed (object sender, EventArgs args) 137 | {//ad closed (even if not finished watching) 138 | BtnReward.interactable = true; 139 | BtnReward.GetComponentInChildren ().text = "More Points"; 140 | 141 | adReward.OnAdLoaded -= this.HandleOnRewardedAdLoaded; 142 | adReward.OnAdRewarded -= this.HandleOnAdRewarded; 143 | adReward.OnAdClosed -= this.HandleOnRewardedAdClosed; 144 | } 145 | 146 | #endregion 147 | 148 | //other functions 149 | //btn (more points) clicked 150 | public void OnGetMorePointsClicked () 151 | { 152 | BtnReward.interactable = false; 153 | BtnReward.GetComponentInChildren ().text = "Loading..."; 154 | RequestRewardAd (); 155 | } 156 | 157 | //------------------------------------------------------------------------ 158 | AdRequest AdRequestBuild () 159 | { 160 | return new AdRequest.Builder ().Build (); 161 | } 162 | 163 | void OnDestroy () 164 | { 165 | DestroyBannerAd (); 166 | DestroyInterstitialAd (); 167 | 168 | //dettach events 169 | adInterstitial.OnAdLoaded -= this.HandleOnAdLoaded; 170 | adInterstitial.OnAdOpening -= this.HandleOnAdOpening; 171 | adInterstitial.OnAdClosed -= this.HandleOnAdClosed; 172 | 173 | 174 | adReward.OnAdLoaded -= this.HandleOnRewardedAdLoaded; 175 | adReward.OnAdRewarded -= this.HandleOnAdRewarded; 176 | adReward.OnAdClosed -= this.HandleOnRewardedAdClosed; 177 | 178 | } 179 | 180 | } 181 | -------------------------------------------------------------------------------- /Admob_banner/Admob.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using GoogleMobileAds.Api; 3 | using System; 4 | 5 | //Banner ad 6 | public class Admob : MonoBehaviour 7 | { 8 | private BannerView adBanner; 9 | 10 | private string idApp, idBanner; 11 | 12 | 13 | void Start () 14 | { 15 | idApp = "ca-app-pub-3940256099942544~3347511713"; 16 | idBanner = "ca-app-pub-3940256099942544/6300978111"; 17 | 18 | MobileAds.Initialize (idApp); 19 | 20 | RequestBannerAd (); 21 | } 22 | 23 | 24 | 25 | #region Banner Methods -------------------------------------------------- 26 | 27 | public void RequestBannerAd () 28 | { 29 | adBanner = new BannerView (idBanner, AdSize.Banner, AdPosition.Bottom); 30 | AdRequest request = AdRequestBuild (); 31 | adBanner.LoadAd (request); 32 | } 33 | 34 | public void DestroyBannerAd () 35 | { 36 | if (adBanner != null) 37 | adBanner.Destroy (); 38 | } 39 | 40 | #endregion 41 | 42 | 43 | //------------------------------------------------------------------------ 44 | AdRequest AdRequestBuild () 45 | { 46 | return new AdRequest.Builder ().Build (); 47 | } 48 | 49 | void OnDestroy () 50 | { 51 | DestroyBannerAd (); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Admob_interstitial/Admob.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using GoogleMobileAds.Api; 3 | using System; 4 | using UnityEngine.UI; 5 | //Interstitial ad 6 | 7 | public class Admob : MonoBehaviour 8 | { 9 | private InterstitialAd adInterstitial; 10 | 11 | private string idApp, idInterstitial; 12 | 13 | [SerializeField] Button BtnInterstitial; 14 | 15 | 16 | void Start () 17 | { 18 | BtnInterstitial.interactable = false; 19 | 20 | idApp = "ca-app-pub-3940256099942544~3347511713"; 21 | idInterstitial = "ca-app-pub-3940256099942544/1033173712"; 22 | 23 | MobileAds.Initialize (idApp); 24 | 25 | RequestInterstitialAd (); 26 | } 27 | 28 | #region Interstitial methods --------------------------------------------- 29 | 30 | public void RequestInterstitialAd () 31 | { 32 | adInterstitial = new InterstitialAd (idInterstitial); 33 | AdRequest request = AdRequestBuild (); 34 | adInterstitial.LoadAd (request); 35 | 36 | //attach events 37 | adInterstitial.OnAdLoaded += this.HandleOnAdLoaded; 38 | adInterstitial.OnAdOpening += this.HandleOnAdOpening; 39 | adInterstitial.OnAdClosed += this.HandleOnAdClosed; 40 | } 41 | 42 | public void ShowInterstitialAd () 43 | { 44 | if (adInterstitial.IsLoaded ()) 45 | adInterstitial.Show (); 46 | } 47 | 48 | public void DestroyInterstitialAd () 49 | { 50 | adInterstitial.Destroy (); 51 | } 52 | 53 | //interstitial ad events 54 | public void HandleOnAdLoaded (object sender, EventArgs args) 55 | { 56 | //this method executes when interstitial ad is Loaded and ready to show 57 | BtnInterstitial.interactable = true; //button is ready to click (enabled) 58 | } 59 | 60 | public void HandleOnAdOpening (object sender, EventArgs args) 61 | { 62 | //this method executes when interstitial ad is shown 63 | BtnInterstitial.interactable = false; //disable the button 64 | } 65 | 66 | public void HandleOnAdClosed (object sender, EventArgs args) 67 | { 68 | //this method executes when interstitial ad is closed 69 | adInterstitial.OnAdLoaded -= this.HandleOnAdLoaded; 70 | adInterstitial.OnAdOpening -= this.HandleOnAdOpening; 71 | adInterstitial.OnAdClosed -= this.HandleOnAdClosed; 72 | 73 | RequestInterstitialAd (); //request new interstitial ad after close 74 | } 75 | 76 | #endregion 77 | 78 | 79 | //------------------------------------------------------------------------ 80 | AdRequest AdRequestBuild () 81 | { 82 | return new AdRequest.Builder ().Build (); 83 | } 84 | 85 | void OnDestroy () 86 | { 87 | DestroyInterstitialAd (); 88 | 89 | //dettach events 90 | adInterstitial.OnAdLoaded -= this.HandleOnAdLoaded; 91 | adInterstitial.OnAdOpening -= this.HandleOnAdOpening; 92 | adInterstitial.OnAdClosed -= this.HandleOnAdClosed; 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /Admob_reward/Admob.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using GoogleMobileAds.Api; 3 | using System; 4 | using UnityEngine.UI; 5 | //reward ad 6 | 7 | public class Admob : MonoBehaviour 8 | { 9 | private RewardBasedVideoAd adReward; 10 | 11 | private string idApp, idReward; 12 | 13 | [SerializeField] Button BtnReward; 14 | [SerializeField] Text TxtPoints; 15 | 16 | 17 | void Start () 18 | { 19 | idApp = "ca-app-pub-3940256099942544~3347511713"; 20 | idReward = "ca-app-pub-3940256099942544/5224354917"; 21 | 22 | adReward = RewardBasedVideoAd.Instance; 23 | 24 | MobileAds.Initialize (idApp); 25 | } 26 | 27 | 28 | #region Reward video methods --------------------------------------------- 29 | 30 | public void RequestRewardAd () 31 | { 32 | AdRequest request = AdRequestBuild (); 33 | adReward.LoadAd (request, idReward); 34 | 35 | adReward.OnAdLoaded += this.HandleOnRewardedAdLoaded; 36 | adReward.OnAdRewarded += this.HandleOnAdRewarded; 37 | adReward.OnAdClosed += this.HandleOnRewardedAdClosed; 38 | } 39 | 40 | public void ShowRewardAd () 41 | { 42 | if (adReward.IsLoaded ()) 43 | adReward.Show (); 44 | } 45 | //events 46 | public void HandleOnRewardedAdLoaded (object sender, EventArgs args) 47 | {//ad loaded 48 | ShowRewardAd (); 49 | 50 | } 51 | 52 | public void HandleOnAdRewarded (object sender, EventArgs args) 53 | {//user finished watching ad 54 | int points = int.Parse (TxtPoints.text); 55 | points += 50; //add 50 points 56 | TxtPoints.text = points.ToString (); 57 | } 58 | 59 | public void HandleOnRewardedAdClosed (object sender, EventArgs args) 60 | {//ad closed (even if not finished watching) 61 | BtnReward.interactable = true; 62 | BtnReward.GetComponentInChildren ().text = "More Points"; 63 | 64 | adReward.OnAdLoaded -= this.HandleOnRewardedAdLoaded; 65 | adReward.OnAdRewarded -= this.HandleOnAdRewarded; 66 | adReward.OnAdClosed -= this.HandleOnRewardedAdClosed; 67 | } 68 | 69 | #endregion 70 | 71 | //other functions 72 | //btn (more points) clicked 73 | public void OnGetMorePointsClicked () 74 | { 75 | BtnReward.interactable = false; 76 | BtnReward.GetComponentInChildren ().text = "Loading..."; 77 | RequestRewardAd (); 78 | } 79 | 80 | //------------------------------------------------------------------------ 81 | AdRequest AdRequestBuild () 82 | { 83 | return new AdRequest.Builder ().Build (); 84 | } 85 | 86 | void OnDestroy () 87 | { 88 | adReward.OnAdLoaded -= this.HandleOnRewardedAdLoaded; 89 | adReward.OnAdRewarded -= this.HandleOnAdRewarded; 90 | adReward.OnAdClosed -= this.HandleOnRewardedAdClosed; 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Admob Ads in unity 2 | 3 | ### Video tutorial 👉 : [Here](https://www.youtube.com/watch?v=EMO85Lb9ZqM&list=PLMWgYNtBT-xPZsyjZoJ41TAAQq9bWCAQT&ab_channel=HamzaHerbou) 4 | 5 |

6 |
7 | ## ❤️ Donate 8 | Paypal 9 | --------------------------------------------------------------------------------