- Home /
Admob Reward Video Ads not calling event functions.
Hi. I'm trying to add an Admob reward video ads to my android game. The ad displays fine but when I close the ad, the reward is never given. I've tested the code in the function and the works fine so I think the problem is that is isn't getting called. Can anyone help me?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class textEdit : MonoBehaviour
{
public Image lifeAdUI;
static Image lifeAdUIStat;
public Text adFailUI;
static Text adFailUIStat;
public Button lifeButton;
private static RewardBasedVideoAd videoAd;
static bool adTime = false;
static bool adPlaying = false;
static int pass = 0;
bool watched;
// Use this for initialisation
void Start()
{
Button btn = lifeButton.GetComponent<Button>();
btn.onClick.AddListener(VideoAd);
videoAd = RewardBasedVideoAd.Instance;
videoAd.OnAdFailedToLoad += HandleOnAdFailedToLoad;
videoAd.OnAdOpening += HandleOnAdOpening;
videoAd.OnAdClosed += HandleOnAdClosed;
videoAd.OnAdRewarded += HandleOnAdReward;
videoAd.OnAdLeavingApplication += HandleOnAdLeavingApplication;
videoAd.OnAdLoaded += HandleOnAdLoaded;
videoAd.OnAdStarted += HandleOnAdStarted;
lifeAdUIStat = lifeAdUI;
adFailUIStat = adFailUI;
}
public static void LoadVideoAd()
{
#if UNITY_EDITOR
string adUnitID = "unused";
#elif UNITY_ANDROID
string adUnitID = "ca-app-pub-3025391748532285/9122766975";
#elif UNITY_IPHONE
string adUnitID = "";
#else
string adUnitID = "unexpected_platform";
#endif
videoAd.LoadAd(new AdRequest.Builder().Build(), adUnitID);
pass = pass + 1;
}
void VideoAd()
{
if (videoAd.IsLoaded())
{
videoAd.Show();
}
else
{
//ad not loaded
}
}
//Ad Events
public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
if (pass < 2)
{
LoadVideoAd();
}
else
{
StartCoroutine(adFailCoro());
}
}
public void HandleOnAdOpening(object ssender, EventArgs args)
{
adPlaying = true;
}
public void HandleOnAdClosed(object sender, EventArgs args)
{
adPlaying = false;
watched = true;
if (watched == true)
{
control controlScript = GameObject.FindGameObjectWithTag("Control").GetComponent<control>();
lifeAdUI.enabled = false;
StartCoroutine(controlScript.ExtraLife());
}
}
public void HandleOnAdReward(object sender, EventArgs args)
{
watched = true;
}
public void HandleOnAdLeavingApplication(object sender, EventArgs args)
{
}
public void HandleOnAdLoaded(object sender, EventArgs args)
{
}
public void HandleOnAdStarted(object sender, EventArgs args)
{
}
}
Answer by hazarartuner · Feb 18, 2018 at 08:09 PM
I have the same problem and when I add my code to try/catch block I get the message like "StartCoroutine_Auto_Internal can only be called from the main thread". I think AdMob event listeners do not work on main thread and this gives error if any unity api specific function call happens.
To achieve this I found this really basic solution: https://github.com/PimDeWitte/UnityMainThreadDispatcher
It executes your code in main thread. It sounds may look like complicated but please read the readme file, it is really easy to use and saved my day in a minute :)
Thank you! U saved my time. I don't understand why ur answer was ignored;)
Answer by wuym67123 · Jul 15, 2017 at 02:50 AM
Follow this https://github.com/unity-plugins/Unity-Admob and have a try.
Answer by ALIENPANDA · Jul 03, 2020 at 07:57 AM
Try to put the function definition inside a bool.
private void Update()
{
if(closed==true)
{
SceneManager.LoadScene("GamePlay");
Time.timeScale = 1;
RewardBtn.interactable = true;
RewardBtn.GetComponentInChildren<TextMeshProUGUI>().text = "Save Life";
closed = false;
}
}
public void HandleOnRewardedAdClosed(object sender, EventArgs args)
{
closed = true;
adReward.OnAdLoaded -= this.HandleOnRewardedAdLoaded;
adReward.OnAdRewarded -= this.HandleOnAdRewarded;
adReward.OnAdClosed -= this.HandleOnRewardedAdClosed;
}
Hope this solves ur issue.
Hi, the only comment I have, you can avoid Update() method because it will run through out the life cycle of script. Instead, initiate a Coroutine just before starting rewarded ad and use WaitWhile or WaitUntill or even a loop unless the boolean change is detect, in the same manner as you are doing it in Updat() loop. I had the same issue today and this approached helped me. This approach assures that Coroutine will end on its own once the bool is detected.