Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ZakWoolley1 · Apr 07, 2017 at 11:22 PM · admobevent-handling

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)
   {

   }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SinopGames · May 16, 2017 at 10:55 AM 0
Share

I have the same problem. you found any solution?

4 Replies

· Add your reply
  • Sort: 
avatar image
2

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 :)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sagrell · Mar 31, 2018 at 10:24 PM 0
Share

Thank you! U saved my time. I don't understand why ur answer was ignored;)

avatar image tuan_dq · Jun 01, 2021 at 04:35 AM 0
Share

Thanks man, it really helpful!

avatar image
0

Answer by SinopGames · May 16, 2017 at 04:40 PM

I have the same problem. you found any solution?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by wuym67123 · Jul 15, 2017 at 02:50 AM

Follow this https://github.com/unity-plugins/Unity-Admob and have a try.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image WaqasHaiderDev · Aug 11, 2020 at 12:43 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is checking button click in Unity done this way? 1 Answer

How do I 'use an item'? 1 Answer

Why does my code hang when calling a script in a multi-nested event function 0 Answers

Admob user question ? 1 Answer

A first chance exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges