AdMob Rewarded Video
I'm trying to make it so that you can pass the level by watching rewarded ad.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
using GoogleMobileAds;
using System;
public class AdManager : MonoBehaviour
{
public GameManager gm;
public RewardBasedVideoAd rewardVideo;
public int adHasBeenShown;
public void Start()
{
adHasBeenShown = 0;
this.rewardVideo = RewardBasedVideoAd.Instance;
rewardVideo.OnAdRewarded += HandleRewardVideoRewarded;
rewardVideo.OnAdFailedToLoad += HandleOnAdFailedToLoad;
rewardVideo.OnAdClosed += HandleRewardVideoClosed;
this.RequestRewardVideoo();
}
public void RequestRewardVideoo()
{
#if UNITY_ANDROID
string rewardedAdID = "ca-app-pub-3940256099942544/5224354917";
#else
string rewardedAdID = "unexpected_platform";
#endif
AdRequest request = new AdRequest.Builder().Build();
this.rewardVideo.LoadAd(request, rewardedAdID);
}
public void OnCompleteButtonPressed()
{
if (rewardVideo.IsLoaded())
{
rewardVideo.Show();
}
}
public void HandleRewardVideoRewarded(object sender, Reward args)
{
gm.CompleteLevel();
}
public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
this.RequestRewardVideoo();
}
public void HandleRewardVideoClosed(object sender, EventArgs args)
{
this.RequestRewardVideoo();
}
}
gm.CompleteLevel() looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEngine.UI;
using DG.Tweening;
public class GameManager : MonoBehaviour {
public void CompleteLevel()
{
foreach (GameObject go in AllSymbols)
{
go.GetComponent<AllSymbolsScript>().canMove = true;
go.GetComponentInChildren<Text>().color = new Color32(255, 255, 255, 255);
}
foreach (GameObject go in UncorrectSymbols)
{
go.GetComponentInParent<Button>().interactable = true;
go.SetActive(true);
}
if (!currentLevel.Completed)
money += 2;
moneyText.text = money.ToString();
foreach (GameObject go in CurrSymbols)
{
if (go.GetComponent<CurrentCells>().selfChar != null)
{
go.GetComponent<CurrentCells>().selfChar.GetComponent<AllSymbolsScript>().ReturnToHome();
}
Destroy(go);
}
LevelDonePanel.alpha = 1;
LevelDonePanel.blocksRaycasts = true;
currLevel += 1;
levelText.text = "Уровень: " + currLevel;
bM.DisableGamePanel();
nlS.LevelDone();
}
}
The ad comes up, I wait until it's over, and I close it. All that is in the script works fine, except bM.DisableGamePanel() and nlS.LevelDone(). These two methods are not called.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonManager : MonoBehaviour {
public LevelPanelManager LPM;
public AudioManager audioManager;
public CanvasGroup friendHelpPanel;
public CanvasGroup PodskazkiPanel;
public CanvasGroup levelPanel;
public CanvasGroup moneyPanel;
public CanvasGroup gamePanel;
public CanvasGroup currSymbolPanel;
public CanvasGroup keyboardPanel;
public Button soundButton;
public Sprite soundOnImage;
public Sprite soundOffImage;
public bool soundBool = true;
public void DisableGamePanel()
{
audioManager.Play("MenuOpen1");
audioManager.Play("MenuOpen2");
gamePanel.alpha = 0;
gamePanel.blocksRaycasts = false;
currSymbolPanel.alpha = 0;
currSymbolPanel.blocksRaycasts = false;
keyboardPanel.alpha = 0;
keyboardPanel.blocksRaycasts = false;
}
}
public class NextLevelScript : MonoBehaviour {
public Image image;
public GameManager gm;
public Text nameText;
public GameObject moneyText;
public AudioManager audioManager;
public void LevelDone()
{
audioManager.Play("Win");
image.sprite = gm.currentLevel.image;
nameText.text = gm.currentLevel.name;
if (gm.currentLevel.Completed)
moneyText.SetActive(false);
else
moneyText.SetActive(true);
gm.currentLevel.Completed = true;
}
}
If I just bind a method CompleteLevel() to a button and call it, everything works fine.
Comment
Answer by Volosnya · Aug 05, 2018 at 09:26 PM
If anyone ever needs it. I solved it using coroutine
public void OnCompleteButtonPressed()
{
if (rewardVideo.IsLoaded())
{
rewardVideo.Show();
}
else
{
//Display error window
this.RequestRewardVideoo();
}
}
public void HandleRewardVideoRewarded(object sender, Reward args)
{
gm.CompleteLevel();
}
public void CompleteLevel()
{
bM.PodskazkiPanel.alpha = 0;
bM.PodskazkiPanel.blocksRaycasts = false;
StartCoroutine(CompleteLevelSecondPhase());
}
public IEnumerator CompleteLevelSecondPhase()
{
yield return new WaitForSeconds(0.1f);
foreach (GameObject go in AllSymbols)
{
go.GetComponent<AllSymbolsScript>().canMove = true;
go.GetComponentInChildren<Text>().color = new Color32(255, 255, 255, 255);
}
foreach (GameObject go in UncorrectSymbols)
{
go.GetComponentInParent<Button>().interactable = true;
go.SetActive(true);
}
if (!currentLevel.Completed)
money += 2;
moneyText.text = money.ToString();
foreach (GameObject go in CurrSymbols)
{
if (go.GetComponent<CurrentCells>().selfChar != null)
{
go.GetComponent<CurrentCells>().selfChar.GetComponent<AllSymbolsScript>().ReturnToHome();
}
Destroy(go);
}
LevelDonePanel.alpha = 1;
LevelDonePanel.blocksRaycasts = true;
currLevel += 1;
levelText.text = "Уровень: " + currLevel;
bM.DisableGamePanel();
nlS.LevelDone();
}
Your answer
Follow this Question
Related Questions
Interstitial Ads Network 0 Answers
Admob not working i not find a solution 1 Answer
can i use admob ads with unity ads in Unity3d Game? 0 Answers
Admob interstitial ads 2 Answers
Xcode build error 0 Answers