Hi, this code is probably wrong somewhere. No experience in C#… I’ using Admob official plugin, but it doesn’t work. I just want to show an interstital ad on load…
Thanks for any help!
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
private InterstitialAd interstitial;
private void RequestInterstitial()
{
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-xxxx/xxxx";
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
// Returns an ad request
private AdRequest CreateAdRequest()
{
return new AdRequest.Builder().Build();
}
// Create an interstitial.
this.interstitial = new InterstitialAd(adUnitId);
// Load an interstitial ad.
this.interstitial.LoadAd(this.CreateAdRequest());
}
private void ShowInterstitial() {
if (interstitial.IsLoaded()) {
interstitial.Show();
}}
public void Start()
{
ShowInterstitial();
}
}
You need to enter here a proper id :
string adUnitId = “ca-app-pub-xxxx/xxxx”;
Like :
string adUnitId = “ca-app-pub-3940256099942544/1033173712”;
Which is a test id provided by Unity.
You also need to call RequestInterstiticial method before the ShowInterstitial method.
And you need to put CreateAdRequest() out of RequestInterstitial().
You need to call ShowInterstitial one time, when the ad is loaded. In this example, I call it 30 times per second, which is forbidden, but it’s an exemple.
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
private InterstitialAd interstitial;
private void RequestInterstitial()
{
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-3940256099942544/1033173712";
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
// Create an interstitial.
this.interstitial = new InterstitialAd(adUnitId);
// Load an interstitial ad.
this.interstitial.LoadAd(this.CreateAdRequest());
}
// Returns an ad request
private AdRequest CreateAdRequest()
{
return new AdRequest.Builder().Build();
}
private void ShowInterstitial()
{
if (interstitial.IsLoaded())
{
interstitial.Show();
}
}
public void Start()
{
RequestInterstitial();
}
void Update()
{
ShowInterstitial();
}
}
@TheRealMarco thank u so much…it really worked…everybody use this code…