So I'm trying to add a "Rewarded video ad" with the reward being more time to play in the next game. my RewardedAd.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.SceneManagement;
public class RewardedAd : MonoBehaviour
{
public static RewardedAd singleton;
private void Awake()
{
if (singleton == null)
{
singleton = this;
}
else if (singleton != this)
{
Destroy(gameObject);
}
}
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.");
StartCoroutine(NewGameWithTime());
break;
case ShowResult.Skipped:
Debug.Log("The ad was skipped before reaching the end.");
SceneManager.LoadScene(0);
break;
case ShowResult.Failed:
Debug.LogError("The ad failed to be shown.");
SceneManager.LoadScene(0);
break;
}
}
IEnumerator NewGameWithTime()
{
Debug.Log("NewGame Started");
GameOverManager.singleton.GameOverAfterRewardAd();
SceneManager.LoadScene(1);
Timer.singleton.AddBonusTime(20);
Debug.Log("NewGame Ended");
yield return new WaitForSeconds(1);
}
}
and my Timer.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System;
public class Timer : MonoBehaviour
{
public static Timer singleton;
public Text timeTF;
private float seconds = 10f;
private void Awake()
{
if (singleton == null)
{
singleton = this;
}
else if (singleton != this)
{
Destroy(gameObject);
}
}
private void Update()
{
if (seconds > 0f)
{
seconds -= Time.deltaTime;
TimeSpan timeSpan = TimeSpan.FromSeconds(seconds);
string timeLeft = string.Format("{0:D2}",timeSpan.Seconds);
timeTF.text = timeLeft;
TimeLeftCheck(seconds);
}
}
public void AddBonusTime(float bonusTime)
{
seconds += bonusTime;
Debug.Log("New Timer script:");
Debug.Log(seconds);
}
private void TimeLeftCheck(float _time)
{
if (_time < 5)
{
//Debug.Log("RunningOutTime sound");
//SoundManager.singleton.Sound_RunningOutofTime();
}
if (_time < 2)
{
//Debug.Log("Out of Time sound");
//SoundManager.singleton.Sound_OutofTime();
}
if (_time < 1)
{
Debug.Log("Game Over");
GameOverManager.singleton.GameOver();
}
}
The video ad is playing fine and it's resetting my ad counter in my game over manager but I can't seem to figure out how to start the next game with more time (20 seconds to so) but ONLY if they've watched the ad.
Also, I'm still learning so any suggestions or things I can improve, more than happy to take that criticism too!
GameOverManager.singleton.GameOverAfterRewardAd();
do? \$\endgroup\$