1

I have created a very small game in unity and everything is doing fine apart from Ad's displaying. Also, I m not getting error just a small warning 'Implement IUnityAdsListener and call Advertisement.AddListener()'. I have went through all the forums in unity and still it doesn't work in the new method. Please help.

My Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Advertisements;
using UnityEngine.UI;

public class App_Initialize : MonoBehaviour
{
    public GameObject inMenuUI;
    public GameObject inGameUI;
    public GameObject gameoverUI;
    public GameObject adButton;
    public GameObject restartButton;
    public GameObject player;
    public bool hasGameStarted = false;
    private bool hasSeenRewardedAd = false;

    void Awake()
    {
        Shader.SetGlobalFloat("_Curvature", 2.0f);
        Shader.SetGlobalFloat("_Trimming", 0.1f);
        Application.targetFrameRate = 60;
    }
    // Start is called before the first frame update
    void Start()
    {
        player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition;
        inMenuUI.gameObject.SetActive(true);
        inGameUI.gameObject.SetActive(false);
        gameoverUI.gameObject.SetActive(false);
    }

    public void PlayButton()
    {   
        if(hasGameStarted == true)
        {
            StartCoroutine(StartGame(1.0f));

        }
        else
        {
            StartCoroutine(StartGame(0.0f));
        }
    }

    public void PauseGame()
    {
        player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition;
        hasGameStarted = true;
        inMenuUI.gameObject.SetActive(true);
        inGameUI.gameObject.SetActive(false);
        gameoverUI.gameObject.SetActive(false);
    }

    public void GameOver()
    {
        player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition;
        hasGameStarted = true;
        inMenuUI.gameObject.SetActive(false);
        inGameUI.gameObject.SetActive(false);
        gameoverUI.gameObject.SetActive(true);
        if(hasSeenRewardedAd == true)
        {
            adButton.GetComponent<Image>().color = new Color(1, 1, 1, 0.5f);
            adButton.GetComponent<Button>().enabled = false;
            adButton.GetComponent<Animator>().enabled = false;
            restartButton.GetComponent<Animator>().enabled = true;
        }
    }

    public void RestartGame()
    {
        SceneManager.LoadScene(0);
    }

    public void ShowAd()
    {
        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");
                hasSeenRewardedAd = true;
                StartCoroutine(StartGame(1.5f));
                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;
        }
    }

    IEnumerator StartGame(float waitTime)
    {
        inMenuUI.gameObject.SetActive(false);
        inGameUI.gameObject.SetActive(true);
        gameoverUI.gameObject.SetActive(false);
        yield return new WaitForSeconds(waitTime);
        player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
        player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionY;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
            Application.Quit();
    }
}
1

1 Answer 1

1

First you need to add the namespace IUnityAdsListener to your class.

public class App_Initialize: MonoBehaviour, IUnityAdsListener {}

Next, add a listerner in your start function and initialize Unity Ads with your gameId and a bool testMode.

    void Start()
    {
        Advertisement.AddListener(this);
        Advertisement.Initialize(gameId, testMode);
    }

You will get a warning that you haven't yet implemented the right interfaces for IUnityAdsListener, so add those to your script.

    public void OnUnityAdsDidError(string message)
    {
        //YOUR CODE
    }

    public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    {
        //YOUR CODE
    }

    public void OnUnityAdsDidStart(string placementId)
    {
        //YOUR CODE
    }

    public void OnUnityAdsReady(string placementId)
    {
        //YOUR CODE
    }

Finally, remove the listener when you destroy the GameObject or Scene your script is in to avoid errors.

private void OnApplicationQuit()
{
    Advertisement.RemoveListener(this);
}
private void OnDestroy()
{
    Advertisement.RemoveListener(this);
}

So this should be your final result:

    using UnityEngine;
    using UnityEngine.Advertisements;

    public class App_Initialize : MonoBehaviour, IUnityAdsListener
    {
        public string gameId = "YOUR_GAME_ID";
        public string myPlacementId = "YOUR_PLACEMENT_ID";
        public bool testMode = true;


        void Start()
        {
            Advertisement.AddListener(this);
            Advertisement.Initialize(gameId, testMode);
        }
        private void OnApplicationQuit()
        {
            Advertisement.RemoveListener(this);
        }
        private void OnDestroy()
        {
            Advertisement.RemoveListener(this);
        }
        public void OnUnityAdsDidError(string message)
        {
            //YOUR CODE
        }

        public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
        {
            //YOUR CODE
        }

        public void OnUnityAdsDidStart(string placementId)
        {
            //YOUR CODE
        }

        public void OnUnityAdsReady(string placementId)
        {
            //YOUR CODE
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.