I have a simple script to handle Admob. I downloaded the plugin and everything works fine. Also, I updated the Google Mobile Ads Settings for my Android app and it works perfectly in the editor. However, when I build it on my phone, it doesn’t respond. The ad doesn’t load or show up. Is anyone able to help me with this issue?
Here is the script:
using UnityEngine;
using GoogleMobileAds.Api;
using TMPro;
using UnityEngine.UI;
using System;
public class AdmobManager : MonoBehaviour
{
public TextMeshProUGUI totalCoinsTxt;
public string appId = "ca-app-pub-3940256099942544~3347511713";
string interId = "ca-app-pub-3940256099942544/1033173712";
string rewardedId = "ca-app-pub-3940256099942544/5224354917";
InterstitialAd interstitialAd;
RewardedAd rewardedAd;
private void Start()
{
ShowCoins();
MobileAds.RaiseAdEventsOnUnityMainThread = true;
MobileAds.Initialize(initStatus => {
print("Ads Initialised !!");
});
}
#region Interstitial
public void LoadInterstitialAd()
{
if (interstitialAd != null)
{
interstitialAd.Destroy();
interstitialAd = null;
}
var adRequest = new AdRequest();
adRequest.Keywords.Add("unity-admob-sample");
InterstitialAd.Load(interId, adRequest, (InterstitialAd ad, LoadAdError error) =>
{
if (error != null || ad == null)
{
print("Interstitial ad failed to load" + error);
return;
}
print("Interstitial ad loaded !!" + ad.GetResponseInfo());
interstitialAd = ad;
InterstitialEvent(interstitialAd);
});
}
public void ShowInterstitialAd()
{
if (interstitialAd != null && interstitialAd.CanShowAd())
{
interstitialAd.Show();
}
else
{
print("Intersititial ad not ready!!");
}
}
public void InterstitialEvent(InterstitialAd ad)
{
// Raised when the ad is estimated to have earned money.
ad.OnAdPaid += (AdValue adValue) =>
{
Debug.Log("Interstitial ad paid {0} {1}." +
adValue.Value +
adValue.CurrencyCode);
};
// Raised when an impression is recorded for an ad.
ad.OnAdImpressionRecorded += () =>
{
Debug.Log("Interstitial ad recorded an impression.");
};
// Raised when a click is recorded for an ad.
ad.OnAdClicked += () =>
{
Debug.Log("Interstitial ad was clicked.");
};
// Raised when an ad opened full screen content.
ad.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Interstitial ad full screen content opened.");
};
// Raised when the ad closed full screen content.
ad.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Interstitial ad full screen content closed.");
};
// Raised when the ad failed to open full screen content.
ad.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Interstitial ad failed to open full screen content " +
"with error : " + error);
};
}
#endregion
#region Rewarded
public void LoadRewardedAd()
{
if (rewardedAd != null)
{
rewardedAd.Destroy();
rewardedAd = null;
}
var adRequest = new AdRequest();
adRequest.Keywords.Add("unity-admob-sample");
RewardedAd.Load(rewardedId, adRequest, (RewardedAd ad, LoadAdError error) =>
{
if (error != null || ad == null)
{
print("Rewarded failed to load" + error);
return;
}
print("Rewarded ad loaded !!");
rewardedAd = ad;
RewardedAdEvents(rewardedAd);
});
}
public void ShowRewardedAd()
{
if (rewardedAd != null && rewardedAd.CanShowAd())
{
rewardedAd.Show((Reward reward) =>
{
print("Give reward to player !!");
GrantCoins(100);
});
}
else
{
print("Rewarded ad not ready");
}
}
public void RewardedAdEvents(RewardedAd ad)
{
// Raised when the ad is estimated to have earned money.
ad.OnAdPaid += (AdValue adValue) =>
{
Debug.Log("Rewarded ad paid {0} {1}." +
adValue.Value +
adValue.CurrencyCode);
};
// 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.");
};
// 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);
};
}
#endregion
#region extra
void GrantCoins(int coins)
{
int crrCoins = PlayerPrefs.GetInt("totalCoins");
crrCoins += coins;
PlayerPrefs.SetInt("totalCoins", crrCoins);
ShowCoins();
}
void ShowCoins()
{
totalCoinsTxt.text = PlayerPrefs.GetInt("totalCoins").ToString();
}
#endregion
}
I am using two buttons - one for loading and one for showing the rewarded ads. However, it seems like the admob isn’t initializing at all.