Search Unity

Admob Rewarded video not calling events after close

Discussion in 'Unity Ads & User Acquisition' started by Punicon, Aug 15, 2018.

  1. Punicon

    Punicon

    Joined:
    Jul 12, 2018
    Posts:
    1
    I'm trying to add an Admob reward video ad to my android game made in Unity. The displays fine but when I close the ad, the reward is never given (coin).
    This is my unity c# script.


    -- AdmobRewardedVideo:

    using System.Collections;
    using System;
    using UnityEngine;
    using GoogleMobileAds.Api;

    public class AdmobRewardedVideo : MonoBehaviour
    {

    private RewardBasedVideoAd rewardBasedVideo;
    public AddCoinScript Controller;

    public void Start()
    {
    DontDestroyOnLoad(gameObject);
    #if UNITY_ANDROID
    string appId = "ca-app-pub-6144446801473222~4916984641";
    #elif UNITY_IPHONE
    string adUnitId = "";
    #else
    string adUnitId = "";
    #endif

    // Initialize the Google Mobile Ads SDK.
    MobileAds.Initialize(appId);

    // Get singleton reward based video ad reference.
    this.rewardBasedVideo = RewardBasedVideoAd.Instance;

    // Called when the user should be rewarded for watching a video.
    rewardBasedVideo_OnAdRewarded += HandleRewardBasedVideoRewarded;

    // Called when the ad is closed.
    rewardBasedVideo_OnAdClosed += HandleRewardBasedVideoClosed;

    this.RequestRewardBasedVideo();
    }

    private void RequestRewardBasedVideo()
    {
    #if UNITY_ANDROID
    //Test;
    string adUnitId = "ca-app-pub-3940256099942544/5224354917";
    #elif UNITY_IPHONE
    string adUnitId = "";
    #else
    string adUnitId = "";
    #endif

    // Create an empty ad request.
    AdRequest request = new AdRequest.Builder().Build();

    // Load the rewarded video ad with the request.
    this.rewardBasedVideo.LoadAd(request, adUnitId);
    }

    public void ShowRewardBasedAd()
    {
    if (rewardBasedVideo.IsLoaded())
    {
    rewardBasedVideo.Show();
    }
    }

    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {
    Controller.AddsCoin();
    }

    public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
    {
    this.RequestRewardBasedVideo();
    }
    }


    -- AddCoinScript:

    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    using System;

    public class AddCoinScript : MonoBehaviour
    {

    public Text scoreTextMesh, txtCoin;
    public int coin;
    int numberOfCoins;

    void OnEnable ()
    {
    scoreTextMesh.text = "" + 0;
    numberOfCoins = PlayerPrefs.GetInt("Coin", coin); // Load archive coins
    coin = coin + numberOfCoins;
    txtCoin.text = coin.ToString();
    }

    public void AddsCoin()
    {
    coin += 30;
    txtCoin.text = "" + coin;
    PlayerPrefs.SetInt("Coin", coin); // Archive the Score coins in "PlayerPrefs.SetInt"
    }
    }



    My setup...

    Building for Android (internal)
    Unity 2017.1.Of3
    Google Mobile Ads Plugin
    Windows
     

    Attached Files:

  2. renyangZheng

    renyangZheng

    Joined:
    Oct 11, 2017
    Posts:
    1
    Did you resolve this problem? I have the same issure..
     
  3. StartStart

    StartStart

    Joined:
    Jan 2, 2013
    Posts:
    150
    Is anything error? Did you try logcat it or not.
     
  4. Kailric

    Kailric

    Joined:
    Aug 12, 2015
    Posts:
    17
    Ok, I 'm writing this for others and for future me so if I have issues again there will be a record. There are a few points to remember when coding Admob reward ads.

    1. The reward instance needs to be a singleton placed on an object with DoNotDestory set. In other words the same instance needs to move from scene to scene. I had issues with using the original instance with new callbacks in a new scene. The ad would show but there would be no call backs.

    2. Important: within the call backs, if you add in method calls from another class this can stop the callback from functioning. I am not sure exactly what causes this, but I have been testing this. If I add in calls to other object classes the callback breaks. When I remove them it works.

    So what I do is start a StartCoroutine before the ad is shown that checks for local variables that are set in the callbacks. I have only local variables in the callback.

    To the OP, I see you have calls outside the instance like Controller.AddsCoin(); This maybe what is causing the callbacks to fail. And btw Controller is never assigned within your code. Perhaps you assign it in the editor.

    3. Apparently OnAdRewarded is called after OnAdClosed so you need to be sure that you have your timing setup correct. I learned this from this post here.

    4. Don't forget to use Test Ads as you don't want to get your account in trouble with Google. The reward test ad ID is: ca-app-pub-3940256099942544/5224354917
     
    Last edited: Apr 3, 2019
    khalidhex and Deleted User like this.
  5. khalidhex

    khalidhex

    Joined:
    Apr 13, 2016
    Posts:
    11
    Christ ! i was suspecting it would be something like that !
     
  6. unity_2gwfm78N_rD_Ww

    unity_2gwfm78N_rD_Ww

    Joined:
    Oct 25, 2020
    Posts:
    1
    Jo, apparently they still didn't fix it, so I had the same problem...
    @Kailric's suggestions didn't help, but I fixed it using LeanTween, but probably any tweening tool will do.

    It seems like it has something to do with threads, so wrapping all critical functions (Logs and calls to other classes) in a LeanTween delayed function, will have those successfully executed on a working thread.

    Example:

    Code (CSharp):
    1. private static void LoadRewardedAd(string _adUnitId)
    2. {
    3.     // Clean up the old ad before loading a new one.
    4.     if (rewardedAd != null)
    5.     {
    6.         rewardedAd.Destroy();
    7.         rewardedAd = null;
    8.     }
    9.  
    10.     LeanTween.delayedCall(0.01f, () => {    // <- HERE
    11.         Debug.Log("Loading the rewarded ad.");
    12.         GameObject.Find("DARKEN").GetComponent<DARKEN>().darken(
    13.              new DARKEN.overlay(null, null, false)
    14.         );
    15.     });
    16.  
    17.     // create our request used to load the ad.
    18.     var adRequest = new AdRequest();
    19.     adRequest.Keywords.Add("unity-admob-sample");
    20.  
    21.     // send the request to load the ad.
    22.     RewardedAd.Load(_adUnitId, adRequest, (RewardedAd ad, LoadAdError error) => {
    23.         if (error != null || ad == null)
    24.         {
    25.             LeanTween.delayedCall(0.01f, () => {    // <- HERE
    26.                 Debug.LogError("Couldn't load ad: " + error);
    27.                 GameObject.Find("DARKEN").GetComponent<DARKEN>().lighten();
    28.             });
    29.             return;
    30.         }
    31.  
    32.         LeanTween.delayedCall(0.01f, () => {    // <- HERE
    33.             Debug.Log("Ad response : " + ad.GetResponseInfo());
    34.         });
    35.  
    36.         rewardedAd = ad;
    37.         ShowRewardedAd();
    38.     });
    39. }

    Code (CSharp):
    1. private static void ShowRewardedAd()
    2. {
    3.     const string rewardMsg = "Rewarded ad rewarded the user. Type: {0}, amount: {1}.";
    4.  
    5.     LeanTween.delayedCall(0.01f, () => {    // <- HERE
    6.         print("Tryna show rewarded ad");
    7.         print("rewardedAd: " + rewardedAd + ", CanShow: " + rewardedAd.CanShowAd());
    8.     });
    9.  
    10.     if (rewardedAd != null && rewardedAd.CanShowAd())
    11.     {
    12.         rewardedAd.Show((Reward reward) =>
    13.         {
    14.             LeanTween.delayedCall(0.01f, () => {    // <- HERE
    15.                 Debug.Log(rewardMsg + ", " + reward.Type + ", " + reward.Amount);
    16.                 GameObject.Find("DARKEN").GetComponent<DARKEN>().lighten();
    17.             });
    18.         });
    19.     }
    20. }
     
  7. Ziucay

    Ziucay

    Joined:
    Feb 18, 2018
    Posts:
    2
    I believe I found a workaround. For certain tasks Unity forbids doing it and throws and error like "...can only be called from the main thread". However, here it does not show this error, but it seems like a similar issue. So, I resolved it by making a counter, which increases in a callback, and if statement in Update(), which gives the reward and decreases the counter