Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AdMob - Callback Handlers (Events) are not firing (Unity 5.3.3p3)

Discussion in 'Android' started by KrlinM, Mar 20, 2016.

  1. KrlinM

    KrlinM

    Joined:
    Oct 23, 2013
    Posts:
    12
    Good Day, i have problems with AdMob callback handlers and im getting mad from that! :O
    Callback Handlers are not firing, for example this:
    Code (CSharp):
    1.  
    2.  public void HandleInterstitialClosed(object sender, EventArgs args)
    3.     {
    4.         Cam.backgroundColor = Color.red;
    5.         Debug.Log("HandleInterstitialClosed event received");
    6.     }
    7.  
    8.  
    does nothing...
    However requesting, showing, hiding and deleting of Interstitial and Banner ads is working fine.
    Im using Unity 5.3.3p3 , Official Google Mobile Ads Unity Plugin v3.0.3, and Lunar Console plugin.
    In AndroidManifests of both plugins I repaired Min and Max SDK Version...
    Code (CSharp):
    1.   <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="23" />
    Then I followed instructions here. Im using script similar to official demo script, see below. Lunar Console is not writing any Debug log in event handlers. Tested on LG G3 (Android 6.0) and tablet Sencor ELEMENT 7Q001 (Android 4.4). Results both same, nothing fired. It must be some really stupid reason. Please help, and sorry for my english... Thanks

    Code (CSharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4. using GoogleMobileAds;
    5. using GoogleMobileAds.Api;
    6.  
    7.  
    8.  
    9. // Example script showing how to invoke the Google Mobile Ads Unity plugin.
    10. public class GoogleMobileAdsDemoScript : MonoBehaviour
    11. {
    12.     public Camera Cam;
    13.     private BannerView bannerView;
    14.     private InterstitialAd interstitial;
    15.     private float deltaTime = 0.0f;
    16.     private static string outputMessage = "";
    17.  
    18.     public static string OutputMessage
    19.     {
    20.         set { outputMessage = value; }
    21.     }
    22.  
    23.  
    24.     void Update()
    25.     {
    26.      
    27.         // Calculate simple moving average for time to render screen. 0.1 factor used as smoothing
    28.         // value.
    29.         deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
    30.     }
    31.  
    32.     void OnGUI()
    33.     {
    34.         GUIStyle style = new GUIStyle();
    35.  
    36.         Rect rect = new Rect(0, 0, Screen.width, Screen.height);
    37.         style.alignment = TextAnchor.LowerRight;
    38.         style.fontSize = (int)(Screen.height * 0.06);
    39.         style.normal.textColor = new Color(0.0f, 0.0f, 0.5f, 1.0f);
    40.         float fps = 1.0f / deltaTime;
    41.         string text = string.Format("{0:0.} fps", fps);
    42.         GUI.Label(rect, text, style);
    43.  
    44.         // Puts some basic buttons onto the screen.
    45.         GUI.skin.button.fontSize = (int)(0.03f * Screen.height);
    46.  
    47.         Rect requestBannerRect = new Rect(0.1f * Screen.width, 0.05f * Screen.height,
    48.                                      0.8f * Screen.width, 0.1f * Screen.height);
    49.         if (GUI.Button(requestBannerRect, "Request Banner"))
    50.         {
    51.             RequestBanner();
    52.         }
    53.  
    54.         Rect showBannerRect = new Rect(0.1f * Screen.width, 0.175f * Screen.height,
    55.                                   0.8f * Screen.width, 0.1f * Screen.height);
    56.         if (GUI.Button(showBannerRect, "Show Banner"))
    57.         {
    58.             bannerView.Show();
    59.         }
    60.  
    61.         Rect destroyBannerRect = new Rect(0.1f * Screen.width, 0.3f * Screen.height,
    62.                                      0.8f * Screen.width, 0.1f * Screen.height);
    63.         if (GUI.Button(destroyBannerRect, "Destroy Banner"))
    64.         {
    65.             bannerView.Destroy();
    66.         }
    67.  
    68.         Rect requestInterstitialRect = new Rect(0.1f * Screen.width, 0.425f * Screen.height,
    69.                                            0.8f * Screen.width, 0.1f * Screen.height);
    70.         if (GUI.Button(requestInterstitialRect, "Request Interstitial"))
    71.         {
    72.             RequestInterstitial();
    73.         }
    74.  
    75.         Rect showInterstitialRect = new Rect(0.1f * Screen.width, 0.55f * Screen.height,
    76.                                         0.8f * Screen.width, 0.1f * Screen.height);
    77.         if (GUI.Button(showInterstitialRect, "Show Interstitial"))
    78.         {
    79.             ShowInterstitial();
    80.         }
    81.  
    82.      
    83.         Rect textOutputRect = new Rect(0.1f * Screen.width, 0.925f * Screen.height,
    84.                                   0.8f * Screen.width, 0.05f * Screen.height);
    85.         GUI.Label(textOutputRect, outputMessage);
    86.     }
    87.  
    88.     private void RequestBanner()
    89.     {
    90.         #if UNITY_EDITOR
    91.             string adUnitId = "unused";
    92.         #elif UNITY_ANDROID
    93.         string adUnitId = "ca-app-pub-2456842918997107/xxxxxxxxx";
    94.         #elif UNITY_IPHONE
    95.         string adUnitId = "ca-app-pub-2456842918997107/xxxxxxxx";
    96.         #else
    97.             string adUnitId = "unexpected_platform";
    98.         #endif
    99.  
    100.         // Create a 320x50 banner at the top of the screen.
    101.         bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);
    102.         // Register for ad events.
    103.  
    104.         bannerView.OnAdLoaded += HandleAdLoaded;
    105.         bannerView.OnAdFailedToLoad += HandleAdFailedToLoad;
    106.         bannerView.OnAdOpening += HandleAdOpening;
    107.         //bannerView.OnAdOpened += ... ; //Causes error: `GoogleMobileAds.Api.InterstitialAd' does not contain a definition for `OnAdOpened' and no extension method `OnAdOpened' of type `GoogleMobileAds.Api.InterstitialAd' could be found
    108.         bannerView.OnAdClosed += HandleAdClosed;
    109.         bannerView.OnAdLeavingApplication += HandleAdLeftApplication;
    110.         // Load a banner
    111.         bannerView.LoadAd(createAdRequest());
    112.     }
    113.  
    114.     private void RequestInterstitial()
    115.     {
    116.         #if UNITY_EDITOR
    117.             string adUnitId = "unused";
    118.         #elif UNITY_ANDROID
    119.         string adUnitId = "ca-app-pub-2456842918997107/xxxxxxxxx";
    120.         #elif UNITY_IPHONE
    121.         string adUnitId = "ca-app-pub-2456842918997107/xxxxxxxxx";
    122.         #else
    123.             string adUnitId = "unexpected_platform";
    124.         #endif
    125.  
    126.         // Create an interstitial.
    127.         interstitial = new InterstitialAd(adUnitId);
    128.         // Register for ad events.
    129.         interstitial.OnAdLoaded += HandleInterstitialLoaded;
    130.         interstitial.OnAdFailedToLoad += HandleInterstitialFailedToLoad;
    131.         interstitial.OnAdOpening += HandleInterstitialOpening;
    132.         //interstitial.OnAdOpened += ... ; //Causes error: `GoogleMobileAds.Api.InterstitialAd' does not contain a definition for `OnAdOpened' and no extension method `OnAdOpened' of type `GoogleMobileAds.Api.InterstitialAd' could be found
    133.         interstitial.OnAdClosed += HandleInterstitialClosed;
    134.         interstitial.OnAdLeavingApplication += HandleInterstitialLeftApplication;      
    135.         // Load an interstitial ad.
    136.         interstitial.LoadAd(createAdRequest());
    137.     }
    138.  
    139.     // Returns an ad request with custom ad targeting.
    140.     private AdRequest createAdRequest()
    141.     {
    142.         return new AdRequest.Builder()
    143.                 .AddTestDevice(AdRequest.TestDeviceSimulator)
    144.                 .AddTestDevice("16564E08A2534032")
    145.                 .AddKeyword("game")
    146.                 .SetGender(Gender.Male)
    147.                 .SetBirthday(new DateTime(1985, 1, 1))
    148.                 .TagForChildDirectedTreatment(false)
    149.                 .AddExtra("color_bg", "9B30FF")
    150.                 .Build();
    151.     }
    152.  
    153.  
    154.     private void ShowInterstitial()
    155.     {
    156.         if (interstitial.IsLoaded())
    157.         {
    158.             interstitial.Show();
    159.             Debug.Log("Interstitial IsLoaded");
    160.         }
    161.         else
    162.         {
    163.             Debug.Log("Interstitial is not ready yet.");
    164.         }
    165.     }
    166.  
    167.  
    168.     #region Banner callback handlers
    169.  
    170.     public void HandleAdLoaded(object sender, EventArgs args)
    171.     {
    172.  
    173.         Cam.backgroundColor = Color.red;
    174.         Debug.Log("HandleAdLoaded event received.");
    175.     }
    176.  
    177.     public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    178.     {
    179.         Cam.backgroundColor = Color.red;
    180.         Debug.Log("HandleFailedToReceiveAd event received with message: " + args.Message);
    181.     }
    182.  
    183.     public void HandleAdOpening(object sender, EventArgs args)
    184.     {
    185.         Cam.backgroundColor = Color.red;
    186.         Debug.Log("HandleAdOpened event received");
    187.     }
    188.  
    189.  
    190.     public void HandleAdClosed(object sender, EventArgs args)
    191.     {
    192.         Cam.backgroundColor = Color.red;
    193.         Debug.Log("HandleAdClosed event received");
    194.     }
    195.  
    196.     public void HandleAdLeftApplication(object sender, EventArgs args)
    197.     {
    198.         Cam.backgroundColor = Color.red;
    199.         Debug.Log("HandleAdLeftApplication event received");
    200.     }
    201.  
    202.     #endregion
    203.  
    204.     #region Interstitial callback handlers
    205.  
    206.     public void HandleInterstitialLoaded(object sender, EventArgs args)
    207.     {
    208.         Cam.backgroundColor = Color.red;
    209.         Debug.Log("HandleInterstitialLoaded event received.");
    210.     }
    211.  
    212.     public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    213.     {
    214.         Cam.backgroundColor = Color.red;
    215.         Debug.Log("HandleInterstitialFailedToLoad event received with message: " + args.Message);
    216.     }
    217.  
    218.     public void HandleInterstitialOpening(object sender, EventArgs args)
    219.     {
    220.         Cam.backgroundColor = Color.red;
    221.         Debug.Log("HandleInterstitialOpening event received");
    222.     }
    223.  
    224.     public void HandleInterstitialClosed(object sender, EventArgs args)
    225.     {
    226.         Cam.backgroundColor = Color.red;
    227.         Debug.Log("HandleInterstitialClosed event received");
    228.     }
    229.  
    230.     public void HandleInterstitialLeftApplication(object sender, EventArgs args)
    231.     {
    232.         Cam.backgroundColor = Color.red;
    233.         Debug.Log("HandleInterstitialLeftApplication event received");
    234.     }
    235.  
    236.     #endregion
    237.  
    238.  
    239. }
    240.  
    241.  
    242.  
     
    Last edited: Mar 21, 2016
  2. KrlinM

    KrlinM

    Joined:
    Oct 23, 2013
    Posts:
    12
    Nobady have same troubles? Please try someone download Source Code.zip from here.
    And then run project Hello World in googleads-mobile-unity-3.0.3\samples, then import into that project GoogleMobileAds.package in googleads-mobile-unity-3.0.3 and import plugin LunarConsole from Asset Store.
    Then install the LunarConsole into the scene (Window > Lunar Mobile console > Install).
    Then you must repair minsdkversion and targetsdkversion in AndroidManifests of both plugins (in Plugins/Android folder) . Then in GoogleMobileAdsDemoScrip you must place your adUnitIds for banner and interstitial ad and id of testing device. Then you can do build on android and test it on device.
    Two fingers swipe down will run LunarConsole, but any of print() in event handlers callback voids has not been fired. Please help.
     
  3. KrlinM

    KrlinM

    Joined:
    Oct 23, 2013
    Posts:
    12
    Ok, after few days I probably figured it out. It was because really stupid reason:

    1) I must have unticked Development build and Script Debugging in Build Settings.

    2) LunarConsole is unable write print() and Debug.Log() in callback voids. That confused me, however LunarConsole is not the problem why event handlers has not been fired.
    When i was importing LunarConsole, i got these errors:


    Anyway thanks to all of you for help :) (sarcasm)
     
    Last edited: Mar 22, 2016
    vonSchlank, emanueldopater and rsodre like this.
  4. weeeBox

    weeeBox

    Joined:
    Jun 2, 2014
    Posts:
    271
    Hey, @KrlinM

    I'm the guy behind Lunar plugin. Would you be able to share the project so I can take a look?
     
  5. weeeBox

    weeeBox

    Joined:
    Jun 2, 2014
    Posts:
    271
    Here's your problem:

    Code (CSharp):
    1. public void HandleInterstitialClosed(object sender, EventArgs args)
    2.     {
    3.         Cam.backgroundColor = Color.red; // this line throws an exception
    4.         Debug.Log("HandleInterstitialClosed event received"); // this line never gets called
    5.     }
    Here's the logcat output from an actual device:

    03-23 16:53:51.264 11299-11299/com.test.foo I/Unity: ArgumentException: INTERNAL_set_backgroundColor can only be called from the main thread.
    Constructors and field initializers will be executed from the loading thread when loading a scene.
    Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
    at UnityEngine.Camera.set_backgroundColor (Color value) [0x00008] in /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineCameraBindings.gen.cs:178
    at GoogleMobileAdsDemoScript.HandleAdLoaded (System.Object sender, System.EventArgs args) [0x00000] in Assets/GoogleMobileAdsDemoScript.cs:172


    This issue has nothing to do with Lunar so, please, consider giving a better rating or remove your review from the plugin page

    Thanks,

    Alex
     
    gu likes this.
  6. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    I second changing your review of lunar mobile console in the asset store @KrlinM as it was not the fault with this issue and has in fact been a fantastic asset for me, which deserves a proper rating and review.
     
    weeeBox likes this.
  7. KrlinM

    KrlinM

    Joined:
    Oct 23, 2013
    Posts:
    12
    Thanks to @weeeBox for explanation of my issue. I already changed the rewiev to 5 stars, its really great tool :)
     
    weeeBox and MD_Reptile like this.
  8. weeeBox

    weeeBox

    Joined:
    Jun 2, 2014
    Posts:
    271
    No problem and thanks for changing the rating: I really appreciate that! If you would experience any other issues or have questions - feel free to post on the forum thread or send me an email (lunar.plugin@gmail.com)
     
  9. Marik_2015

    Marik_2015

    Joined:
    Oct 30, 2015
    Posts:
    4
    https://18220747100232649887.google...BRvHflitokZMslc6Pd07_m61QSIDE8cjbabbt4r-qyVYwI want to connect admob interstitial ads. I use Google Mobile Ads Unity Plugin v3.0.5.
    From this link: https://github.com/googleads/googleads-mobile-unity/releases/tag/3.0.5

    Everything is connected, but does not display advertising. Here is the error that provides:

    Dummy .ctor
    UnityEngine.Debug:Log(Object)
    GoogleMobileAds.Common.DummyClient:.ctor()
    (at Assets/GoogleMobileAds/Common/DummyClient.cs:47)
    GoogleMobileAds.GoogleMobileAdsClientFactory:BuildInterstitialClient()
    (at Assets/GoogleMobileAds/Platforms/GoogleMobileAdsClientFactory.cs:45)
    GoogleMobileAds.Api.InterstitialAd:.ctor(String)



    Dummy LoadAd
    UnityEngine.Debug:Log(Object)
    GoogleMobileAds.Common.DummyClient:LoadAd(AdRequest)
    (at Assets/GoogleMobileAds/Common/DummyClient.cs:57)
    GoogleMobileAds.Api.InterstitialAd:LoadAd(AdRequest)
    (at Assets/GoogleMobileAds/Api/InterstitialAd.cs:66)
     
    Last edited: Jul 30, 2016
  10. rahat

    rahat

    Joined:
    Aug 21, 2012
    Posts:
    16
    I don't know wither this issue is fixed or not, But i am having the same problem events are not fired. I think its the problem of google admob
     
    ankit92ios2 likes this.
  11. qleb1984

    qleb1984

    Joined:
    Aug 15, 2018
    Posts:
    15
    same here having same problem , close event not firing
     
    ankit92ios2 likes this.
  12. blinovalexei97

    blinovalexei97

    Joined:
    Apr 20, 2019
    Posts:
    8
    What Kind of methods can we call in the handlers? They seem useless if we can't do anything inside the handlers - can't reference anything and can only print some messages? Why not call your ShowInterstitial() method once it loads - ie when it handles? How else are you supposed to do it? Have a constantly running Update() method that checks if it's loaded? Man this is not easy lol