Search Unity

how to get unity ads working in unity 3d

Discussion in 'Unity Ads & User Acquisition' started by vyphius92, May 21, 2017.

  1. vyphius92

    vyphius92

    Joined:
    Dec 2, 2012
    Posts:
    8
    Followed all the steps, pasted code but unity ads not showing up. What did i do wrong?

    using UnityEngine;
    using UnityEngine.Advertisements;

    public class AdsUnity : MonoBehaviour {

    public void ShowRewardedAd()
    {
    if (Advertisement.IsReady("rewardedVideo"))
    {
    var options = new ShowOptions { resultCallback = HandleShowResult };
    Advertisement.Show("rewardedVideo", options);
    }
    }

    private void HandleShowResult(ShowResult result)
    {
    switch (result)
    {
    case ShowResult.Finished:
    Debug.Log("The ad was successfully shown.");
    //
    // YOUR CODE TO REWARD THE GAMER
    // Give coins etc.
    break;
    case ShowResult.Skipped:
    Debug.Log("The ad was skipped before reaching the end.");
    break;
    case ShowResult.Failed:
    Debug.LogError("The ad failed to be shown.");
    break;
    }
    }
    }
     
  2. rasmus-unity

    rasmus-unity

    Moderator

    Joined:
    Aug 15, 2014
    Posts:
    1,312
    From where in the game are you calling ShowRewardedAd() method?

    /Rasmus
     
  3. vyphius92

    vyphius92

    Joined:
    Dec 2, 2012
    Posts:
    8
    I'm calling it from a script that I attached to a game object! Then attached this game object to the button and called the method ShowRewardedAd() method. I will post a picture of this asap!
     
  4. rasmus-unity

    rasmus-unity

    Moderator

    Joined:
    Aug 15, 2014
    Posts:
    1,312
    Ok, and you have selected either Android or iOS platform, right? And when running in editor playmode, do you get a message similar to following:

    UnityAdsEditor: Initialize("(game id)", False);
    UnityEditor.Advertisements.UnityAdsEditor:EditorOnLoad()

    /Rasmus
     
  5. vyphius92

    vyphius92

    Joined:
    Dec 2, 2012
    Posts:
    8
    Actually know I figured it out thanks to your help.
    It was the name public class AdsUnity, I should have named it public class UnityAdsExample.
    Thank you for your help.

    Now I have a different question.
    How do I make:
    1) rewarded video on death that adds +1 to lives
    2) ads show on transition from game over panel to menu?
    3) watch adds to boost score by, for example 100


    here are some scripts I'm working with



    ================



    public class MainMenuController : MonoBehaviour {

    [SerializeField]
    private Button musicButton;

    [SerializeField]
    private Sprite[] musicIcons;

    void Start() {
    CheckIfMusicIsOnOrOff ();
    }

    void CheckIfMusicIsOnOrOff() {
    if (GamePreferences.GetMusicState () == 0) {
    if (MusicController.instance != null) {
    MusicController.instance.PlayMusic (true);
    }
    musicButton.image.sprite = musicIcons[0];
    } else {
    musicButton.image.sprite = musicIcons[1];
    }
    }

    public void PlayGame() {
    GameManager.instance.gameStartedFromMainMenu = true;
    // Application.LoadLevel ("Gameplay");
    SceneFader.instance.LoadLevel ("Gameplay");
    }

    public void HighScoreMenu() {
    Application.LoadLevel ("HighscoreMenu");
    }

    public void OptionsMenu() {
    Application.LoadLevel ("OptionsMenu");
    }

    public void QuitGame() {
    Application.Quit ();
    }

    public void TurnMusicOnOrOff() {
    if (GamePreferences.GetMusicState () == 0) {
    GamePreferences.SetMusicState (1);
    if (MusicController.instance != null) {
    MusicController.instance.PlayMusic (false);
    }
    musicButton.image.sprite = musicIcons[1];
    } else {
    GamePreferences.SetMusicState (0);
    if (MusicController.instance != null) {
    MusicController.instance.PlayMusic (true);
    }
    musicButton.image.sprite = musicIcons[0];
    }
    }

    } // MainMenuController


    using UnityEngine;
    using System.Collections;

    public class GameManager : MonoBehaviour {

    public static GameManager instance;

    [HideInInspector]
    public bool gameStartedFromMainMenu, gameRestartedAfterPlayerDied;

    [HideInInspector]
    public int score, coinScore, lifeScore;

    void Awake () {
    MakeSingleton ();
    }

    void Start() {
    InitializeGame ();
    }

    void OnLevelWasLoaded() {
    if (Application.loadedLevelName == "Gameplay") {

    if(gameRestartedAfterPlayerDied) {
    GameplayController.instance.SetScore(score);
    GameplayController.instance.SetLifeScore(lifeScore);
    GameplayController.instance.SetCoinScore(coinScore);

    PlayerScore.coinCount = coinScore;
    PlayerScore.scoreCount = score;
    PlayerScore.lifeCount = lifeScore;

    } else if(gameStartedFromMainMenu) {
    PlayerScore.coinCount = 0;
    PlayerScore.scoreCount = 0;
    PlayerScore.lifeCount = 2;

    GameplayController.instance.SetScore(0);
    GameplayController.instance.SetLifeScore(2);
    GameplayController.instance.SetCoinScore(0);

    }

    }
    }

    void MakeSingleton() {
    if (instance != null) {
    Destroy (gameObject);
    } else {
    instance = this;
    DontDestroyOnLoad(gameObject);
    }
    }

    void InitializeGame() {
    if (!PlayerPrefs.HasKey ("Game Initialized")) {
    GamePreferences.SetMusicState(1);

    GamePreferences.SetEasyDifficultyState(1);
    GamePreferences.SetEasyDifficultyHighscore(0);
    GamePreferences.SetEasyDifficultyCoinScore(0);

    GamePreferences.SetMediumDifficultyState(0);
    GamePreferences.SetMediumDifficultyHighscore(0);
    GamePreferences.SetMediumDifficultyCoinScore(0);

    GamePreferences.SetHardDifficultyState(1);
    GamePreferences.SetHardDifficultyHighscore(0);
    GamePreferences.SetHardDifficultyCoinScore(0);

    PlayerPrefs.SetInt("Game Initialized", 0);
    }
    }

    public void CheckGameStatus(int score, int coinScore, int lifeScore) {
    if (lifeScore < 0) {

    if (GamePreferences.GetEasyDifficultyState () == 0) {

    int highscore = GamePreferences.GetEasyDifficultyHighscore ();
    int highCoinScore = GamePreferences.GetEasyDifficultyCoinScore ();

    if (highscore < score)
    GamePreferences.SetEasyDifficultyHighscore (score);

    if (highCoinScore < coinScore)
    GamePreferences.SetEasyDifficultyCoinScore (coinScore);

    }

    if (GamePreferences.GetMediumDifficultyState () == 0) {

    int highscore = GamePreferences.GetMediumDifficultyHighscore ();
    int highCoinScore = GamePreferences.GetMediumDifficultyCoinScore ();

    if (highscore < score)
    GamePreferences.SetMediumDifficultyHighscore (score);

    if (highCoinScore < coinScore)
    GamePreferences.SetMediumDifficultyCoinScore (coinScore);

    }

    if (GamePreferences.GetHardDifficultyState () == 0) {

    int highscore = GamePreferences.GetHardDifficultyHighscore ();
    int highCoinScore = GamePreferences.GetHardDifficultyCoinScore ();

    if (highscore < score)
    GamePreferences.SetHardDifficultyHighscore (score);

    if (highCoinScore < coinScore)
    GamePreferences.SetHardDifficultyCoinScore (coinScore);

    }

    gameStartedFromMainMenu = false;
    gameRestartedAfterPlayerDied = false;

    GameplayController.instance.GameOverShowPanel(score, coinScore);

    } else {

    this.score = score;
    this.coinScore = coinScore;
    this.lifeScore = lifeScore;

    GameplayController.instance.SetScore(score);
    GameplayController.instance.SetLifeScore(lifeScore);
    GameplayController.instance.SetCoinScore(coinScore);

    gameStartedFromMainMenu = false;
    gameRestartedAfterPlayerDied = true;

    GameplayController.instance.PlayerDiedRestartLevel();

    }
    }

    } // GameManager


    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;

    public class HighscoreController : MonoBehaviour {

    [SerializeField]
    private Text scoreText, coinText;

    void Start() {
    SetScoreForDifficulty ();
    }

    public void SetScore(int score, int coin) {
    scoreText.text = "" + score;
    coinText.text = "" + coin;
    }

    void SetScoreForDifficulty() {
    if(GamePreferences.GetEasyDifficultyState() == 0) {
    SetScore(GamePreferences.GetEasyDifficultyHighscore(), GamePreferences.GetEasyDifficultyCoinScore());
    }

    if(GamePreferences.GetMediumDifficultyState() == 0) {
    SetScore(GamePreferences.GetMediumDifficultyHighscore(), GamePreferences.GetMediumDifficultyCoinScore());
    }

    if(GamePreferences.GetHardDifficultyState() == 0) {
    SetScore(GamePreferences.GetHardDifficultyHighscore(), GamePreferences.GetHardDifficultyCoinScore());
    }
    }

    public void GoBack() {
    Application.LoadLevel ("MainMenu");
    }

    } // HighscoreController

    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;

    public class GameplayController : MonoBehaviour {

    public static GameplayController instance;

    [SerializeField]
    private Text scoreText, lifeScore, coinScore, gameOverScoreText, gameOverCoinScoreText;

    [SerializeField]
    private GameObject pausePanel, gameOverPanel;

    [SerializeField]
    private GameObject readyButton;

    void Awake () {
    MakeInstance ();
    // Time.timeScale = 0f;
    }

    void Start() {
    Time.timeScale = 0f;
    }

    void MakeInstance() {
    if (instance == null) {
    instance = this;
    }
    }

    public void SetScore(int score) {
    scoreText.text = "" + score;
    }

    public void SetCoinScore(int score) {
    coinScore.text = "x" + score;
    }

    public void SetLifeScore(int score) {
    lifeScore.text = "x" + score;
    }

    public void GameOverShowPanel (int gameOverScore, int gameOverCoinScore) {
    gameOverPanel.SetActive (true);
    gameOverScoreText.text = "" + gameOverScore;
    gameOverCoinScoreText.text = "" + gameOverCoinScore;
    StartCoroutine (GameOverLoadMainMenu ());
    }

    IEnumerator GameOverLoadMainMenu() {
    yield return StartCoroutine(MyCoroutine.WaitForRealSeconds(3f));
    SceneFader.instance.LoadLevel ("MainMenu");
    }

    public void PlayerDiedRestartLevel() {
    StartCoroutine (PlayerDiedRestart ());
    }

    IEnumerator PlayerDiedRestart() {
    yield return StartCoroutine(MyCoroutine.WaitForRealSeconds(1f));
    SceneFader.instance.LoadLevel ("Gameplay");
    }

    public void PauseGame() {
    Time.timeScale = 0f;
    pausePanel.SetActive (true);
    }

    public void ResumeGame() {
    Time.timeScale = 1f;
    pausePanel.SetActive (false);
    }

    public void QuitGame() {
    Time.timeScale = 1f;
    Application.LoadLevel ("MainMenu");
    // SceneFader.instance.LoadLevel ("MainMenu");
    }

    public void StartTheGame() {
    Time.timeScale = 1f;
    readyButton.SetActive (false);
    }

    } // GameplayController

    using UnityEngine;
    using System.Collections;

    public class MusicController : MonoBehaviour {

    public static MusicController instance;

    private AudioSource audioSource;

    void Awake () {
    MakeSingleton ();
    audioSource = GetComponent<AudioSource> ();
    }

    void MakeSingleton() {
    if (instance != null) {
    Destroy(gameObject);
    } else {
    instance = this;
    DontDestroyOnLoad(gameObject);
    }
    }

    public void PlayMusic(bool play) {
    if (play) {
    if (!audioSource.isPlaying) {
    audioSource.Play ();
    }
    } else {
    if (audioSource.isPlaying) {
    audioSource.Stop ();
    }
    }
    }

    } // MusicController



    using UnityEngine;
    using System.Collections;

    public class OptionsController : MonoBehaviour {

    [SerializeField]
    private GameObject easySign, mediumSign, hardSign;

    void Start() {
    SetInitialDifficultyInOptionsMenu ();
    }

    public void InitialDifficulty(string difficulty) {
    switch (difficulty) {
    case "easy":
    easySign.SetActive (true);
    mediumSign.SetActive (false);
    hardSign.SetActive (false);
    break;

    case "medium":
    easySign.SetActive (false);
    mediumSign.SetActive (true);
    hardSign.SetActive (false);
    break;

    case "hard":
    easySign.SetActive (false);
    mediumSign.SetActive (false);
    hardSign.SetActive (true);
    break;
    }
    }

    void SetInitialDifficultyInOptionsMenu() {
    if(GamePreferences.GetEasyDifficultyState() == 0) {
    InitialDifficulty("easy");
    }

    if(GamePreferences.GetMediumDifficultyState() == 0) {
    InitialDifficulty("medium");
    }

    if(GamePreferences.GetHardDifficultyState() == 0) {
    InitialDifficulty("hard");
    }
    }

    public void EasyDifficulty() {

    GamePreferences.SetEasyDifficultyState (0);
    GamePreferences.SetMediumDifficultyState (1);
    GamePreferences.SetHardDifficultyState (1);

    easySign.SetActive (true);
    mediumSign.SetActive (false);
    hardSign.SetActive (false);
    }

    public void MediumDifficulty() {

    GamePreferences.SetEasyDifficultyState (1);
    GamePreferences.SetMediumDifficultyState (0);
    GamePreferences.SetHardDifficultyState (1);

    easySign.SetActive (false);
    mediumSign.SetActive (true);
    hardSign.SetActive (false);
    }

    public void HardDifficulty() {

    GamePreferences.SetEasyDifficultyState (1);
    GamePreferences.SetMediumDifficultyState (1);
    GamePreferences.SetHardDifficultyState (0);

    easySign.SetActive (false);
    mediumSign.SetActive (false);
    hardSign.SetActive (true);
    }

    public void GoBack() {
    Application.LoadLevel ("MainMenu");
    }


    } // OptionsController



    ================
     
  6. vyphius92

    vyphius92

    Joined:
    Dec 2, 2012
    Posts:
    8
    Also Here is the Game Over Panel,
    How Do I:
    1) Allow the player to see a panel that pops up after they lost last life that gives them chance to get 1up if they watch ad?
    2) Make Panel disappear after 7 secs and show a countdown timer of 7 seconds?
    3) Allow Game Over Panel to Appear if they don't choose yes, or if they choose no
    4) Allow the player to only get chance to use 1 up panel to get extra life if they watch ad once and not everytime they lose their last life? However which is better in your opinion. Ads that give extra life EVERY time playerlife < 0 or once per game?
     

    Attached Files: