Show ads admob interstitial (full page) after game over

5

I've already imported all the Admob files into my project and it's working, but I'd like to know how to put interstitial ads to appear when WinPanel and LosePanel are 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 in many ways but I could not.

    
asked by anonymous 14.06.2015 / 20:44

1 answer

2

Have you tried to follow any particular tutorial? I followed this link and had no problems.

I'll paste my code here because there are some callbacks that are not in the tutorial code and can be useful for debugging problems:

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

1) Preferably run BEFORE you really want to show advertisement, so that it is ready at the time of 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 ad that you would call by activating 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 must be recreated (start over from step 1).

    
12.08.2015 / 06:52