- Home /
Help with ads in restart button.
Hey guys, i'm making a 2D action game... i'm not a programmer so i been doing my game thanks to Lord Google and St. Youtube.
My problem is... when the player looses the live two buttons appears... one to restart the level, and a second one to earn extra coins by watching an ad.
I'd like to add a no reward video ad to the restart button, and make it aleatory.... i mean if you hit restart.. the video will play or not randomly. Been trying to doing by my self with no luck.
Can someone please help me?
This is the code working fine in the 'get coins' button:
public class GetCoinsButton : MonoBehaviour, IUnityAdsListener {
public string gameId = "xxxxxxx";
public string placementId = "rewardedVideo";
public int CoinsEarned = 25;
void Start() {
Advertisement.AddListener(this);
Advertisement.Initialize(gameId);
}
public void ShowAd() {
GlobalCoins.adbug = false;
if(Advertisement.IsReady("rewardedVideo")) {
Advertisement.Show("rewardedVideo");
}
}
public void OnUnityAdsReady (string theplacementId) {
// If the ready Placement is rewarded, show the ad:
}
public void OnUnityAdsDidError (string message) {
}
public void OnUnityAdsDidStart (string placementId) {
}
public void OnUnityAdsDidFinish (string placementId, ShowResult showResult) {
if(!GlobalCoins.adbug) {
if (showResult == ShowResult.Finished) {
GlobalCoins.coins = GlobalCoins.coins + CoinsEarned;
GlobalCoins.adbug = true;
PlayerPrefs.SetInt("coins", GlobalCoins.coins);
} else if (showResult == ShowResult.Skipped) {
// Do not reward the user for skipping the ad.
} else if (showResult == ShowResult.Failed) {
Debug.LogWarning ("The ad did not finish due to an error.");
}
}
}
}
and this is the code for the Restart Button:
public class RestartAdButton : MonoBehaviour
{
public string sceneName;
public string gameId = "xxxxxxx";
public string placementId = "intersitialAd";
private bool alreadySeen = false;
void Start() {
Advertisement.Initialize(gameId);
}
public void RestartLevel() {
GlobalCoins.adbug = true;
if(!alreadySeen) {
if(Advertisement.IsReady(placementId)) {
Advertisement.Show(placementId);
alreadySeen = true;
}
else {
Codevenient.BasicSceneFade.FadeTransition.Instance.FadeToScene(sceneName);
}
} else {
Codevenient.BasicSceneFade.FadeTransition.Instance.FadeToScene(sceneName);
}
}
}
This how it is working for now: https://www.youtube.com/watch?v=jK_AYAX70hY
Thanks in advance!
Comment