5

Eu já importei todos os ficheiros do Admob para o meu projeto e está tudo a funcionar, mas gostava de saber como colocar anúncios interstitial a aparecer quando o WinPanel e o LosePanel ficarem ativos.

Aqui fica o código:

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];
    }
}

Tentei de várias maneiras porém não consegui.

1 Resposta 1

2

Você tentou seguir algum tutorial em particular? Segui este aqui https://developers.google.com/admob/android/interstitial?hl=en e não tive problemas.

Vou colar o meu código aqui porque há alguns callbacks que não estão no código tutorial e podem ser úteis para depurar problemas:

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

1) Executado de preferência ANTES de realmente querer mostrar propaganda, para ela estar pronta na hora do 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) Exibe de fato o ad carregado, que você chamaria ao ativar a tela WIN ou LOSE:

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

Lembrar que o objeto ad não é reciclável. Uma vez exibido o anúncio deve ser recriado (recomeçar do passo 1).

6
  • como você fez para determinar o numero do addTestDevice
    – Vale
    28/01/2016 às 12:23
  • Ele mostra o número correto numa mensagem de log, dizendo algo como "não é recomendável servir propaganda durante testes, o código de teste para seu device é X".
    – epx
    28/01/2016 às 13:36
  • @apx você poderia dar uma força, de como você fez para implementar o anuncio Interstitial eu segui os passo do site de admob e nada não aparece, eu queria algo simples, abriu na unity aparecer o anuncio, você poderia me informa como você configurou seu android manifest se precisar eu abro um pergunta
    – Vale
    28/01/2016 às 19:27
  • se você quiser responder para me ajudar segue o link da pergunta pt.stackoverflow.com/questions/110734/…
    – Vale
    29/01/2016 às 15:36
  • Oi, onde encontro esse código do addTestDevice?
    – user41630
    9/11/2016 às 5:27

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .