- Home /
Problem is not reproducible or outdated
[Unity game admob] Why interstitial ad doesn't work?
I would like to put ads in my Unity game when OverSystem appears to me.
Unfortunately, interstitial ad doesn't work. Any help?
private InterstitialAd interstitial;
void Start ()
{
RequestInterstitial() ;
GetComponent<AudioSource> ().playOnAwake = false;
GetComponent<AudioSource> ().clip = destroySound;
}
public void OnCollisionEnter2D (Collision2D col)
{
collisionObject colObj= col.transform.GetComponent<collisionObject>();
if(!colObj) return;
CheckDestroyedIndex(colObj);
}
void CheckDestroyedIndex(collisionObject cObj) {
if(index == cObj.myIndexNumber)
{
if (cObj.isHighestNumber)
{
Debug.Log ("Yay you correctly hit the last Square, You WIN!");
WinSystem.SetActive (true);
gameObject.SetActive (false);
Manager.SetActive (false);
}
Destroy(cObj.gameObject);
index++;
}
else
{
GameOver ();
{
if (interstitial.IsLoaded()) {
interstitial.Show();
}
}
}
}
void GameOver (){
ImageOver.SetActive (true);
restartButton.SetActive (true);
menuButton.SetActive (true);
gameObject.SetActive (false);
Manager.SetActive (false);
WinSystem.SetActive (false);
}
private void RequestInterstitial()
{
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-3940256099942544/1033173712";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-3940256099942544/4411468910";
#else
string adUnitId = "unexpected_platform";
#endif
// Clean up interstitial ad before creating a new one.
if (this.interstitial != null) {
this.interstitial.Destroy ();
}
// Initialize an InterstitialAd.
InterstitialAd interstitial = new InterstitialAd(adUnitId);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
interstitial.LoadAd(request);
// Called when an ad request has successfully loaded.
interstitial.OnAdLoaded += HandleOnAdLoaded;
// Called when an ad request failed to load.
interstitial.OnAdFailedToLoad += HandleOnAdFailedToLoad;
// Called when an ad is shown.
interstitial.OnAdOpening += HandleOnAdOpened;
// Called when the ad is closed.
interstitial.OnAdClosed += HandleOnAdClosed;
}
public void HandleOnAdLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdLoaded event received");
}
public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print("HandleFailedToReceiveAd event received with message: "
+ args.Message);
}
public void HandleOnAdOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdOpened event received");
}
public void HandleOnAdClosed(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdClosed event received");
}
}
Answer by Getsumi3 · Jun 06, 2018 at 07:44 PM
Here is a good explanation on how to work with addmob in Unity
Just scroll down until you'll find "Quick Start" and follow the examples
https://github.com/unity-plugins/Unity-Admob
And check this video tutorial. It helped me when I tried to figure out on how to work with addmob and other google play services
https://www.youtube.com/watch?v=khlROw-PfNE
Thanks for the answer, I've already checked this video, but unfortunately the DontDestroyOnLoad function does not work for me.
Every time I change the scene, it's removed.
Well then.
In your main menu create an empty object. Name it something like "Add$$anonymous$$anager". Attach you script that contain DontDestroyOnLoad to that object.
For more info about DontDestroyOnLoad check the documentation https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Yes, I know ( like on the video), but this video only works in the old version of Unity.
Answer by Equalent · Jun 08, 2018 at 03:59 PM
Well, there is a great guide on using AdMob with Unity written by Google: link text. You just need to import the package and use it as described in the article.
this is a tutorial that I don't understand :) I have already checked all these tutorials from the first pages of Google :)