Alright, so here is what happens.
When the player finishes a match, they are sent to a scene that checks if there is an ad ready via the following script. If there is an available Ad it sends the player to the ads scene to watch the ad via the script after the following, otherwise the user is sent to the landing screen which redirects them to the main menu.
Theoretically the player should see an ad every time they finish a match unless one of the following is true: they have purchased a map, they are playing offline, or there is no available ad.
In situations where the player is both online and has not made a purchase, the ad will play fine after the first match, but no other ads will play after any other match unless the app is closed and reopened. The reason they don’t play is because no further ads get readied. This is the problem. There should be an ad after every match.
I’m not sure if I am allowed to direct you to the game but, If you want to see this in action, the game is ZomPow2 and its on both the app store and google play.
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class CheckForAds : MonoBehaviour
{
bool ran;
// Start is called before the first frame update
void Update()
{
if(GameObject.Find("FloatingManager") && ran == false)
{
if (GameObject.Find("FloatingManager").GetComponent<SaveInfo>())
{
ran = true;
StartCoroutine(Adwait());
}
}
}
public IEnumerator Adwait()
{
yield return new WaitForSeconds(.5f);
if (GameObject.Find("FloatingManager").GetComponent<SaveInfo>().VirtualGoods != null)
{
if (!GameObject.Find("FloatingManager").GetComponent<SaveInfo>().VirtualGoods.Contains("AD_FREE_PRODUCT"))
{
if (GameSparks.Core.GS.Authenticated)
{
Debug.Log("On to ads...");
SceneManager.LoadScene("Ads");
}
else
{
Debug.Log("Ads offline...");
Destroy(GameObject.Find("FloatingCanvas"));
Destroy(GameObject.Find("GameSparks"));
SceneManager.LoadScene("LandingScreen");
}
}
else
{
Debug.Log("Have ad free...");
Destroy(GameObject.Find("FloatingCanvas"));
Destroy(GameObject.Find("GameSparks"));
//yield return new WaitForSeconds(.5f);
if (GameSparks.Core.GS.Authenticated)
{
Debug.Log("Yay online and no ads...");
SceneManager.LoadScene("LandingScreen");
}
else
{
Debug.Log("Offline but hey no ads...");
SceneManager.LoadScene("LandingScreen");
}
}
}
else
{
Debug.Log("Have ad free...");
Destroy(GameObject.Find("FloatingCanvas"));
Destroy(GameObject.Find("GameSparks"));
//yield return new WaitForSeconds(.5f);
if (GameSparks.Core.GS.Authenticated)
{
Debug.Log("Yay online and no ads...");
SceneManager.LoadScene("LandingScreen");
}
else
{
Debug.Log("Offline but hey no ads...");
SceneManager.LoadScene("LandingScreen");
}
}
}
}
Here is the code that plays the Ad:
using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.SceneManagement;
public class RewardedAdsButton : MonoBehaviour, IUnityAdsListener
{
public string myPlacementId = "rewardedVideo";
bool adFinished;
bool adLoaded;
bool triedManualLoad;
string otherplacmentId;
float loadingAdTimer = 1;
private string gameId;
void Start()
{
if(Application.platform == RuntimePlatform.IPhonePlayer)
gameId = "3633307";
else
gameId = "3633306";
// Set interactivity to be dependent on the Placement’s status:
//if (Advertisement.IsReady(myPlacementId))
//{
//ShowRewardedVideo();
// Initialize the Ads listener and service:
Debug.Log("hit: " + adLoaded + "::" + gameId);
Advertisement.AddListener(this);
Advertisement.Initialize(gameId, true);
//}
}
private void Update()
{
if (adLoaded == false)
{
TimerManager(ref loadingAdTimer);
if (loadingAdTimer <= 0 && !adLoaded && triedManualLoad == false)
{
loadingAdTimer = 4;
Advertisement.Load(myPlacementId);
triedManualLoad = true;
}
else if (loadingAdTimer <= 0 && !adLoaded)
{
Debug.Log("Error: ad failed to load...");
Destroy(GameObject.Find("FloatingCanvas"));
Destroy(GameObject.Find("GameSparks"));
SceneManager.LoadScene("LandingScreen");
}
}
}
// Implement a function for showing a rewarded video ad:
void ShowRewardedVideo()
{
Debug.Log("hit3");
Advertisement.Show(otherplacmentId);
}
// Implement IUnityAdsListener interface methods:
public void OnUnityAdsReady(string placementId)
{
Debug.Log("hit2 " + "ids: " + placementId + "/" + myPlacementId + " adLoaded: " + adLoaded);
// If the ready Placement is rewarded, activate the button:
if (!adLoaded)
{
otherplacmentId = placementId;
adLoaded = true;
ShowRewardedVideo();
// Initialize the Ads listener and service:
Advertisement.AddListener(this);
Advertisement.Initialize(gameId, true);
}
}
public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
{
adFinished = true;
// Define conditional logic for each ad completion status:
if (showResult == ShowResult.Finished)
{
Debug.Log("fin");
Destroy(GameObject.Find("FloatingCanvas"));
Destroy(GameObject.Find("GameSparks"));
SceneManager.LoadScene("LandingScreen");
// Reward the user for watching the ad to completion.
}
else if (showResult == ShowResult.Skipped)
{
Debug.Log("skip");
Destroy(GameObject.Find("FloatingCanvas"));
Destroy(GameObject.Find("GameSparks"));
SceneManager.LoadScene("LandingScreen");
// Do not reward the user for skipping the ad.
}
else if (showResult == ShowResult.Failed)
{
Debug.Log("fail");
Destroy(GameObject.Find("FloatingCanvas"));
Destroy(GameObject.Find("GameSparks"));
SceneManager.LoadScene("LandingScreen");
}
}
public void OnUnityAdsDidError(string message)
{
Debug.LogError(message);
Destroy(GameObject.Find("FloatingCanvas"));
Destroy(GameObject.Find("GameSparks"));
SceneManager.LoadScene("LandingScreen");
// Log the error.
}
public void OnUnityAdsDidStart(string placementId)
{
// Optional actions to take when the end-users triggers an ad.
}
//private void OnDestroy()
//{
// if (!adFinished)
// {
// GameObject.Find("FloatingManager").GetComponent<SaveInfo>().discardScore();
// }
//}
private void TimerManager(ref float timer)
{
if (timer > 0)
{
timer -= Time.deltaTime;
}
}
}