How do you manage the amount of time Ads appear in your app?
I am new to using Unity Ads, so sorry if I am a bit confused. I am currently working on a 2d game. After every round you lose, an ad shows up after you die. Instead of this, I would like for ads to show up every two rounds out of five. I was wondering if someone knew the code to make this happen. Here is the current code with the ad codes implemented in it:
void GameOver() { GameState = GameState.GameOver; Advertisement.Show(); }
public void CheckGameOver(GameObject ball) { listBall.Remove(ball);
if (listBall.Count == 0)
{
SoundManager.Instance.PlaySound(SoundManager.Instance.gameOver);
gameOver = true;
currentTargetPoint.SetActive(false);
ParticleSystem particle = Instantiate(hitGold, currentTarget.transform.position, Quaternion.identity) as ParticleSystem;
particle.startColor = currentTarget.gameObject.GetComponent<SpriteRenderer>().color;
particle.Play();
Destroy(particle.gameObject, 1f);
Destroy(currentTarget.gameObject);
Advertisement.Show ();
GameOver();
}
}
if (!gameOver) { SoundManager.Instance.PlaySound(SoundManager.Instance.gameOver); gameOver = true; for (int i = 0; i < listBall.Count; i++) { listBall[i].GetComponent().Exploring(); }
currentTargetPoint.SetActive(false);
ParticleSystem particle = Instantiate(hitGold, currentTarget.transform.position, Quaternion.identity) as ParticleSystem;
particle.startColor = currentTarget.gameObject.GetComponent<SpriteRenderer>().color;
particle.Play();
Destroy(particle.gameObject, 1f);
Destroy(currentTarget.gameObject);
q`
Advertisement.Show();
GameOver();
}
I would like to say thank you in advance. If you need any extra photos or code, just tell me and I will get it to you.
Answer by EDevJogos · Nov 24, 2017 at 01:41 AM
This should do it, it will show ads in 2 random rounds out of 5:
//If you restart the round by reloading the Scene then make
//this 2 variables static.
int adsControl = 5;
int roundShowAds = 0;
//Make this test everytime you start a round
if(adsControl < 5)
{
adsControl++;
}
else
{
adsControl = 0;
roundShowAds = Random.Range(0, 5);
}
//Encapsule in this if your ShowAds call
if(adsControl == roundShowAds || adsControl == (roundShowAds + 1) % 5)
{
//ShowAds();
}
@Search Thank you for your response! I am still a bit confused. I placed both "int" line along with my other ones. I believe I messed up with one thing however. I replaced every "Advertisement.Show()" with:
//$$anonymous$$ake this test everytime you start a round if(adsControl < 5) { adsControl++; } else { adsControl = 0; roundShowAds = Random.Range(0, 5); }
That is probably incorrect. I also do not know where to place the lines:
//Encapsule in this if your ShowAds call if(adsControl == roundShowAds || adsControl == (roundShowAds + 1) % 5) { //ShowAds(); }
Sorry that I do not understand. Thank you for the help in advance.
Ok, what this code bellow does is count on which round you're, and randomize 2 values that will be the rounds where a ads will be displayed. This test has to be made everytime you start a new round and there only, i guess you have something like StartRound();
method.
//$$anonymous$$ake this test everytime you start a round
if(adsControl < 5)
{
adsControl++;
}
else
{
adsControl = 0;
roundShowAds = Random.Range(0, 5);
}
This other code, what it does is test if the current round is equal to one of the randomized values on the previous code, if so that means this round have to show a advertisement when gameover happens. So everytime on gameover you do this test to see if the current round will or not display a advertisement.
if(adsControl == roundShowAds || adsControl == (roundShowAds + 1) % 5)
{
//Replace this comment with the call you use to display a advertisement.
//I guess it is Advertisement.Show ();
}