Unity Admob Integration Tutorial | Unity Tutorial
Unlock the power of monetization in your Unity games with our Unity Admob Tutorial 2024. In this comprehensive guide, you'll learn how to seamlessly integrate AdMob into your Unity project, step-by-step. Discover how to set up ad units, configure settings, and optimize ad performance to maximize your revenue. Whether you're new to mobile advertising or looking to refine your skills, this tutorial provides all the tools you need to start earning from your game today!
WATCH FULL TUTORIAL ON YOUTUBE
=========================================================
Unity Projects - ON SALE!!!
1. Water Sort Puzzle 9000 Levels
2. Ball Sort Puzzle 9000 Levels
3. Sling Shot
=========================================================
Script:
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class AdmobAdsManager : MonoBehaviour
{
public static AdmobAdsManager Instance;
BannerView bannerAd;
private InterstitialAd interstitialAds;
private RewardedAd rewardedAds;
public string admobBannerId;
public string admobInterstitialId;
public string admobRewardedId;
// Start is called before the first frame update
void Start()
{
MobileAds.Initialize((InitializationStatus initStatus) =>
{
// This callback is called once the MobileAds SDK is initialized.
LoadBannerAd();
LoadInterstitialAd();
LoadRewardedAd();
});
}
// Update is called once per frame
void Awake()
{
Instance = this;
}
#region BANNER
public void LoadBannerAd()
{
Debug.Log("Loading Banner Ad");
// If we already have a banner, destroy the old one.
if (bannerAd != null)
{
DestroyBannerAd();
}
// Create a 320x50 banner at top of the screen
bannerAd = new BannerView(admobBannerId, AdSize.Banner, AdPosition.Bottom);
ShowBannerAd();
}
void ShowBannerAd()
{
// create an instance of a banner view first.
if (bannerAd == null)
{
LoadBannerAd();
}
// create our request used to load the ad.
var adRequest = new AdRequest();
// send the request to load the ad.
Debug.Log("Showing Banner Ad");
bannerAd.LoadAd(adRequest);
}
public void DestroyBannerAd()
{
if (bannerAd != null)
{
Debug.Log("Destroying Banner Ad");
bannerAd.Destroy();
bannerAd = null;
}
}
private void ListenToAdEvents()
{
// Raised when an ad is loaded into the banner view.
bannerAd.OnBannerAdLoaded += () =>
{
Debug.Log("Banner view loaded an ad with response : "
+ bannerAd.GetResponseInfo());
};
// Raised when an ad fails to load into the banner view.
bannerAd.OnBannerAdLoadFailed += (LoadAdError error) =>
{
Debug.LogError("Banner view failed to load an ad with error : "
+ error);
LoadBannerAd();
};
// Raised when the ad is estimated to have earned money.
bannerAd.OnAdPaid += (AdValue adValue) =>
{
Debug.Log(String.Format("Banner view paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));
};
// Raised when an impression is recorded for an ad.
bannerAd.OnAdImpressionRecorded += () =>
{
Debug.Log("Banner view recorded an impression.");
};
// Raised when a click is recorded for an ad.
bannerAd.OnAdClicked += () =>
{
Debug.Log("Banner view was clicked.");
};
// Raised when an ad opened full screen content.
bannerAd.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Banner view full screen content opened.");
};
// Raised when the ad closed full screen content.
bannerAd.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Banner view full screen content closed.");
};
}
#endregion
#region INTERSTITIAL
public void LoadInterstitialAd()
{
// Clean up the old ad before loading a new one.
if (interstitialAds != null)
{
interstitialAds.Destroy();
interstitialAds = null;
}
Debug.Log("Loading the interstitial ad.");
// create our request used to load the ad.
var adRequest = new AdRequest();
// send the request to load the ad.
InterstitialAd.Load(admobInterstitialId, adRequest,
(InterstitialAd ad, LoadAdError error) =>
{
// if error is not null, the load request failed.
if (error != null || ad == null)
{
Debug.LogError("interstitial ad failed to load an ad " +
"with error : " + error);
return;
}
Debug.Log("Interstitial ad loaded with response : "
+ ad.GetResponseInfo());
interstitialAds = ad;
RegisterEventHandlers(interstitialAds);
});
}
public void ShowInterstitialAd()
{
if (interstitialAds != null && interstitialAds.CanShowAd())
{
Debug.Log("Showing interstitial ad.");
interstitialAds.Show();
}
else
{
Debug.LogError("Interstitial ad is not ready yet.");
}
}
private void RegisterEventHandlers(InterstitialAd interstitialAd)
{
// Raised when the ad is estimated to have earned money.
interstitialAd.OnAdPaid += (AdValue adValue) =>
{
Debug.Log(String.Format("Interstitial ad paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));
};
// Raised when an impression is recorded for an ad.
interstitialAd.OnAdImpressionRecorded += () =>
{
Debug.Log("Interstitial ad recorded an impression.");
};
// Raised when a click is recorded for an ad.
interstitialAd.OnAdClicked += () =>
{
Debug.Log("Interstitial ad was clicked.");
};
// Raised when an ad opened full screen content.
interstitialAd.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Interstitial ad full screen content opened.");
};
// Raised when the ad closed full screen content.
interstitialAd.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Interstitial ad full screen content closed.");
LoadInterstitialAd();
};
// Raised when the ad failed to open full screen content.
interstitialAd.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Interstitial ad failed to open full screen content " +
"with error : " + error);
LoadInterstitialAd();
};
}
#endregion
#region REWARDED
public void LoadRewardedAd()
{
// Clean up the old ad before loading a new one.
if (rewardedAds != null)
{
rewardedAds.Destroy();
rewardedAds = null;
}
Debug.Log("Loading the rewarded ad.");
// create our request used to load the ad.
var adRequest = new AdRequest();
// send the request to load the ad.
RewardedAd.Load(admobRewardedId, adRequest,
(RewardedAd ad, LoadAdError error) =>
{
// if error is not null, the load request failed.
if (error != null || ad == null)
{
Debug.LogError("Rewarded ad failed to load an ad " +
"with error : " + error);
return;
}
Debug.Log("Rewarded ad loaded with response : "
+ ad.GetResponseInfo());
rewardedAds = ad;
RegisterEventHandlers(rewardedAds);
});
}
public void ShowRewardedAd()
{
const string rewardMsg =
"Rewarded ad rewarded the user. Type: {0}, amount: {1}.";
if (rewardedAds != null && rewardedAds.CanShowAd())
{
rewardedAds.Show((Reward reward) =>
{
// TODO: Reward the user.
Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount));
});
}
}
private void RegisterEventHandlers(RewardedAd ad)
{
// Raised when the ad is estimated to have earned money.
ad.OnAdPaid += (AdValue adValue) =>
{
Debug.Log(String.Format("Rewarded ad paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));
//Reward The Player
};
// Raised when an impression is recorded for an ad.
ad.OnAdImpressionRecorded += () =>
{
Debug.Log("Rewarded ad recorded an impression.");
};
// Raised when a click is recorded for an ad.
ad.OnAdClicked += () =>
{
Debug.Log("Rewarded ad was clicked.");
};
// Raised when an ad opened full screen content.
ad.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Rewarded ad full screen content opened.");
};
// Raised when the ad closed full screen content.
ad.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Rewarded ad full screen content closed.");
LoadRewardedAd();
};
// Raised when the ad failed to open full screen content.
ad.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Rewarded ad failed to open full screen content " +
"with error : " + error);
LoadRewardedAd();
};
}
#endregion
}