Show ads admob interstitial (full page) after game over

Asked

Viewed 521 times

5

I’ve already imported all the Admob files into my project and it’s all working, but I’d like to know how to get interstitial ads to show up when the WinPanel and the LosePanel stay active.

Here is the code:

using System.Collections;
using UnityEngine.UI;


public class GameManager : MonoBehaviour {
    public int LevelNumber;
    public GameObject imgStar1;
    public GameObject imgStar2;
    public GameObject imgStar3;
    public AudioClip audPickup;
    public AudioClip audWin;
    public AudioClip audCrash;
    //Panels
    public GameObject PausePanel;
    public GameObject PlayingPanel;
    public GameObject WinPanel;
    public GameObject LosePanel;
    public GameObject SettingsPanel;
    public Text txtGraphics;
    public Text MuteText;

    GameObject player;

    int StarsCollected;
    // Use this for initialization
    void Start () {
        player = GameObject.FindGameObjectWithTag("Player");
        Time.timeScale = 1;
        StarsCollected = 0;


        //Gets whether the sound is set to muted, and changes audio settings accordingly.
        if (PlayerPrefs.GetInt("SoundSettings") == 1)
        {
            AudioListener.pause = false;
            MuteText.text = "";
        } else if (PlayerPrefs.GetInt ("SoundSettings") == 0)
        {
            AudioListener.pause = true;
            MuteText.text = "/";
        }

        txtGraphics.text = GetQualityName (QualitySettings.GetQualityLevel());
    }



public    void CollectStar ()
    {
        audio.PlayOneShot (audPickup);
        StarsCollected += 1;

        if (StarsCollected == 1)
            imgStar1.SetActive (true);
        if (StarsCollected == 2)
            imgStar2.SetActive (true);
        if (StarsCollected == 3)
            imgStar3.SetActive (true);

    }

    public void CollectTime()
    {
        audio.PlayOneShot (audPickup);
    }


public void LevelWin ()
    {
        int lvlULTemp = PlayerPrefs.GetInt("LevelsUnlocked");
        if (lvlULTemp == LevelNumber)
            PlayerPrefs.SetInt("LevelsUnlocked",lvlULTemp+1);

        Time.timeScale = 0;
        int tempStars = PlayerPrefs.GetInt ("Level" + LevelNumber.ToString () + "Stars");
        audio.PlayOneShot (audWin);
        if (tempStars < StarsCollected)
        {
            PlayerPrefs.SetInt ("Level" + LevelNumber.ToString () + "Stars", StarsCollected);
        }

        PausePanel.SetActive (false);
        PlayingPanel.SetActive (false);
        WinPanel.SetActive (true);
        LosePanel.SetActive (false);
        SettingsPanel.SetActive (false);

        }


public    void LevelLose()
    {
        player.audio.Stop();
        audio.PlayOneShot (audCrash);
        PausePanel.SetActive (false);
        PlayingPanel.SetActive (false);
        WinPanel.SetActive (false);
        LosePanel.SetActive (true);
        SettingsPanel.SetActive (false);
        Time.timeScale = 0;
        Debug.Log ("Level Lose");
    }

    /////////////////////////////
    ////////////////////////////
    ///////UI FUNCTIONS////////
    //////////////////////////


    public void PreviousLevel()
    {

        Application.LoadLevel (Application.loadedLevel - 1);
    }

    public void NextLevel()
    {

        if(Application.loadedLevel <22)
        {
        Application.LoadLevel (Application.loadedLevel + 1);
        } else {
            Application.LoadLevel (0);
        }
    }

    public void Restart()
    {

        Application.LoadLevel (Application.loadedLevel);
    }

    public void SwitchToMenu()
    {
        Time.timeScale = 1;
        Application.LoadLevel (0);
    }

     public void Pause()
    {
    PausePanel.SetActive (true);
    PlayingPanel.SetActive (false);
    WinPanel.SetActive (false);
    LosePanel.SetActive (false);
    SettingsPanel.SetActive (false);
    Time.timeScale = 0;

    }

    public void Unpause()
    {
        Time.timeScale = 1;
        PausePanel.SetActive (false);
        PlayingPanel.SetActive (true);
        WinPanel.SetActive (false);
        LosePanel.SetActive (false);
        SettingsPanel.SetActive (false);
    }

    public void Settings()
    {
        PausePanel.SetActive (false);
        PlayingPanel.SetActive (false);
        WinPanel.SetActive (false);
        LosePanel.SetActive (false);
        SettingsPanel.SetActive (true);
    }

    public void ChangeGraphics(string GraphicsSet)
    {
        if(GraphicsSet == "up")
        {
            QualitySettings.IncreaseLevel();
        }else if(GraphicsSet == "down")
        {
            QualitySettings.DecreaseLevel();
        }
        txtGraphics.text = GetQualityName (QualitySettings.GetQualityLevel());
    }


    public void ToggleSound()
    {
        int tempSound = PlayerPrefs.GetInt ("SoundSettings");

        if (tempSound == 1) {
            PlayerPrefs.SetInt("SoundSettings",0);
            //audio.mute = true;
            AudioListener.pause = true;
            MuteText.text = "/";
        } else if (tempSound == 0) {
            PlayerPrefs.SetInt("SoundSettings",1);
            //audio.mute = false;
            MuteText.text = "";
            AudioListener.pause = false;
        }
    }


    public string GetQualityName(int qualityNo)
    {
        string[] names;
        names = QualitySettings.names;

        return names[qualityNo];
    }
}

I tried several ways but I couldn’t.

1 answer

2

Have you tried following any particular tutorial? I followed this one https://developers.google.com/admob/android/interstitial?hl=en and had no problems.

I will paste my code here because there are some callbacks that are not in the tutorial code and may be useful for debugging issues:

private InterstitialAd ad;
static private final String AD_GPLAY = "ca-app-pub-......";

1) Run preferably BEFORE you really want to show advertising, so it’s ready to win/lose:

public void propaganda() {
    ad = new InterstitialAd(this);
    ad.setAdUnitId(AD_GPLAY);

    AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice("BCE0578F6CE........052A443764A")
        .addTestDevice("5FFB806C61C........E178FEE1F7B")
        .build();

    ad.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            Log.d(TAG, "ad has loaded");
            if (ad.isLoaded()) {
                Log.d(TAG, " scheduling to show ad");
                // você provavelmente não vai querer mostrar o ad aqui
                showAd();
            } else {
                Log.d(TAG, "ad not loaded yet (should not happen)");
                // chama propaganda() novamente mais tarde;
            }
        }

        @Override
        public void onAdClosed() {
            Log.d(TAG, "ad closed");
            // chama propaganda() novamente mais tarde;
        }

        @Override
        public void onAdFailedToLoad(int err) {
            Log.d(TAG, "ad load error " + err);
            // chama propaganda() novamente mais tarde;
        }
    });

    Log.d(TAG, "loading ad");
    ad.loadAd(adRequest);
}

2) Actually displays the loaded ad, which you would call when you activate the WIN or LOSE screen:

private void showAd() {
     if (ad != null) {
         ad.show();
     }
}

Remember that the ad object is not recyclable. Once displayed the ad should be recreated (restart from step 1).

  • as you did to determine the number of addTestDevice

  • It shows the correct number in a log message, saying something like "it is not recommended to serve advertising during testing, the test code for your device is X".

  • @apx you could give a force, as you did to implement the Interstitial ad I followed the steps of the admob site and nothing does not appear, I wanted something simple, opened in Unity appear the ad, you could inform me how you set up your android manifest if you need I open a question

  • if you want to answer to help me please follow the link to the question http://answall.com/questions/110734/admob-n%C3%A3o-esta-funcionado-na-Unity

  • Hi, where can I find this addTestDevice code?

  • Appears in the program’s Execution Log. It logs a message suggesting to add your device as a test and provides the code.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.