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();
}
}