Search Unity

Unity Ads 3.4.1 No IUnityAdsListener events would be fired

Discussion in 'Unity Ads & User Acquisition' started by jinming_cao, Dec 18, 2019.

  1. jinming_cao

    jinming_cao

    Joined:
    Oct 2, 2019
    Posts:
    17
    Just updated to Unity Ads 3.4.1. Glad to see the canvas overlay issue is fixed, but....
    None of events defined in IUnityAdsListener would be fired at least when tested in editor.
    This issue totally blocked the process.
     
  2. robertsze

    robertsze

    Joined:
    Jul 8, 2013
    Posts:
    83
    Same here, 3.3.0 works fine regarding callbacks
     
  3. luispedrofonseca

    luispedrofonseca

    Joined:
    Aug 29, 2012
    Posts:
    943
    Getting the same error. I honestly don't understand how such a bug gets released.
     
  4. jinming_cao

    jinming_cao

    Joined:
    Oct 2, 2019
    Posts:
    17
    I guess they have neither a QA nor a project manager in their team.
     
  5. ap-unity

    ap-unity

    Unity Technologies

    Joined:
    Aug 3, 2016
    Posts:
    1,519
    @jinming_cao, @robertsze, @luispedrofonseca

    Thanks for reporting this issue. We'll need some more information:

    Which version of Unity are you using?
    Can you provide the script you are using to test the callbacks?
    Can you provide the manifest.json file in your Packages folder?
     
  6. luispedrofonseca

    luispedrofonseca

    Joined:
    Aug 29, 2012
    Posts:
    943
    @ap-unity I'm using the latest Unity version (2019.3.0f3)

    This the code I used to handle the ads (hasn't changed from the previous version):
    Code (csharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4. using UnityEngine.Advertisements;
    5. using Zenject;
    6.  
    7. public class AdsManager : IInitializable, IUnityAdsListener
    8. {
    9.    public Action<string> OnAdAvailable;
    10.    public Action<ShowResult> OnAdCompleted;
    11.    
    12.    public static bool IsAdAvailable => Advertisement.IsReady(ConfigAsset.config.AdPlacementID);
    13.    public static bool IsRewardedAdAvailable => Advertisement.IsReady(ConfigAsset.config.RewardedAdPlacementID);
    14.    
    15.    public void Initialize()
    16.    {
    17. #if UNITY_IOS
    18.       Advertisement.Initialize(ConfigAsset.config.AppleGameID, ConfigAsset.config.AdsTestMode);
    19. #elif UNITY_ANDROID
    20.         Advertisement.Initialize (ConfigAsset.config.AndroidGameID, ConfigAsset.config.AdsTestMode);
    21. #else
    22.         Debug.LogWarning("AdsManager: No ads on this platform - " + Application.platform);
    23. #endif
    24.  
    25.       Advertisement.AddListener(this);
    26.    }
    27.  
    28.    public void ShowAd()
    29.    {
    30.       Advertisement.Show();
    31.    }
    32.  
    33.    public void ShowRewardedAd()
    34.    {
    35.       Advertisement.Show (ConfigAsset.config.RewardedAdPlacementID);
    36.    }
    37.  
    38.    public void OnUnityAdsReady(string placementId)
    39.    {
    40.       OnAdAvailable?.Invoke(placementId);
    41.    }
    42.  
    43.    public void OnUnityAdsDidError(string message)
    44.    {
    45.       Debug.LogError(message);
    46.    }
    47.  
    48.    public void OnUnityAdsDidStart(string placementId)
    49.    {
    50.       Debug.Log($"Ad started: {placementId}");
    51.    }
    52.  
    53.    public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    54.    {
    55.       OnAdCompleted?.Invoke(showResult);
    56.    }
    57. }
    58.  
    And this is my manifest.json file:
    Code (csharp):
    1.  
    2. {
    3. "dependencies": {
    4. "com.unity.2d.animation": "3.0.8",
    5. "com.unity.2d.sprite": "1.0.0",
    6. "com.unity.ads": "3.4.1",
    7. "com.unity.analytics": "3.3.4",
    8. "com.unity.device-simulator": "2.0.0-preview",
    9. "com.unity.ide.rider": "1.1.4",
    10. "com.unity.quicksearch": "1.4.1",
    11. "com.unity.render-pipelines.core": "7.1.6",
    12. "com.unity.render-pipelines.universal": "7.1.6",
    13. "com.unity.shadergraph": "7.1.6",
    14. "com.unity.test-framework": "1.1.8",
    15. "com.unity.textmeshpro": "2.0.1",
    16. "com.unity.timeline": "1.2.9",
    17. "com.unity.ugui": "1.0.0",
    18. "com.unity.modules.ai": "1.0.0",
    19. "com.unity.modules.androidjni": "1.0.0",
    20. "com.unity.modules.animation": "1.0.0",
    21. "com.unity.modules.assetbundle": "1.0.0",
    22. "com.unity.modules.audio": "1.0.0",
    23. "com.unity.modules.cloth": "1.0.0",
    24. "com.unity.modules.director": "1.0.0",
    25. "com.unity.modules.imageconversion": "1.0.0",
    26. "com.unity.modules.imgui": "1.0.0",
    27. "com.unity.modules.jsonserialize": "1.0.0",
    28. "com.unity.modules.particlesystem": "1.0.0",
    29. "com.unity.modules.physics": "1.0.0",
    30. "com.unity.modules.physics2d": "1.0.0",
    31. "com.unity.modules.screencapture": "1.0.0",
    32. "com.unity.modules.terrain": "1.0.0",
    33. "com.unity.modules.terrainphysics": "1.0.0",
    34. "com.unity.modules.tilemap": "1.0.0",
    35. "com.unity.modules.ui": "1.0.0",
    36. "com.unity.modules.uielements": "1.0.0",
    37. "com.unity.modules.umbra": "1.0.0",
    38. "com.unity.modules.unityanalytics": "1.0.0",
    39. "com.unity.modules.unitywebrequest": "1.0.0",
    40. "com.unity.modules.unitywebrequestassetbundle": "1.0.0",
    41. "com.unity.modules.unitywebrequestaudio": "1.0.0",
    42. "com.unity.modules.unitywebrequesttexture": "1.0.0",
    43. "com.unity.modules.unitywebrequestwww": "1.0.0",
    44. "com.unity.modules.vehicles": "1.0.0",
    45. "com.unity.modules.video": "1.0.0",
    46. "com.unity.modules.vr": "1.0.0",
    47. "com.unity.modules.wind": "1.0.0",
    48. "com.unity.modules.xr": "1.0.0"
    49. }
    50. }
    51.  
    52.  
     
  7. sbankhead

    sbankhead

    Unity Technologies

    Joined:
    Jul 27, 2014
    Posts:
    97
    So I was able to use your script and was able to get callbacks just fine in the editor. I'll share the script with you below, but here are some things that I observed.
    1) Make sure your testing on a proper platform (Android or IOS) in your build settings or your code will just dump a warning that your on an unsupported platform.
    2) Nowhere in your script are you setting OnAdAvailable or OnAdComplete objects which means while the callbacks are happening from the ads sdk, they may not be calling into your code properly because of this.
    3) I have no idea what Zenject does exactly, but it seems to be a dependency injection framework??, but it looks like your using it to manage the ads sdk? are you sure that your script is properly being called by zenject? Id try testing this without zenject to ensure your project is working as you expect.

    Here is what I modified your code to. Simply attach this to a gameobject and it will show an ad on playmode start, and will essentially spam you with ad after ad after ad each time you close. You can click the close button and see your console spammed with the finish state as well as the ready state for each placement right after. Please let me know if your still having issues.

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.Advertisements;
    4.  
    5. public class AdsManager : MonoBehaviour, IUnityAdsListener
    6. {
    7.     public Action<string> OnAdAvailable;
    8.     public Action<ShowResult> OnAdCompleted;
    9.  
    10.     public static bool IsAdAvailable => Advertisement.IsReady("video");
    11.     public static bool IsRewardedAdAvailable => Advertisement.IsReady("rewardedVideo");
    12.  
    13.     public void Start()
    14.     {
    15. #if UNITY_IOS
    16.         Advertisement.Initialize ("14850", false);
    17. #elif UNITY_ANDROID
    18.         Advertisement.Initialize ("14851", false);
    19. #else
    20.         Debug.LogWarning("AdsManager: No ads on this platform - " + Application.platform);
    21. #endif
    22.         Advertisement.AddListener(this);
    23.     }
    24.     public void ShowAd()
    25.     {
    26.         Advertisement.Show();
    27.     }
    28.     public void ShowRewardedAd()
    29.     {
    30.         Advertisement.Show ("rewardedVideo");
    31.     }
    32.     public void OnUnityAdsReady(string placementId)
    33.     {
    34.         Debug.Log("OnUnityAdsReady: " + placementId);
    35.         if (placementId == "rewardedVideo") {
    36.             ShowRewardedAd();
    37.         }
    38.     }
    39.     public void OnUnityAdsDidError(string message)
    40.     {
    41.         Debug.Log("OnUnityAdsDidError: " + message);
    42.     }
    43.     public void OnUnityAdsDidStart(string placementId)
    44.     {
    45.         Debug.Log("OnUnityAdsDidStart: " + placementId);
    46.     }
    47.     public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    48.     {
    49.         Debug.Log("OnUnityAdsDidFinish [" + showResult + "]: " + placementId);
    50.     }
    51. }
    52.  
     
  8. jinming_cao

    jinming_cao

    Joined:
    Oct 2, 2019
    Posts:
    17
    Hi sbankhead,

    I'm using 2018.3.8 and I have exactly same issue with luispedrofonseca, please note that both our code works without issue in 3.4.0. The callback is missing only in 3.4.1.

    I'm using Android platform in editor, and I can't receive any events from any API defined in the listener.
     
    Last edited: Dec 20, 2019
  9. luispedrofonseca

    luispedrofonseca

    Joined:
    Aug 29, 2012
    Posts:
    943
    @sbankhead Please notice that my code didn't change from 3.4.0 to 3.4.1!

    Also, not sure what you mean with:
     
  10. ap-unity

    ap-unity

    Unity Technologies

    Joined:
    Aug 3, 2016
    Posts:
    1,519
  11. jinming_cao

    jinming_cao

    Joined:
    Oct 2, 2019
    Posts:
    17
    I'll when I get some time today.

    BTW, this issue exists only with Advertisement API in 3.4.1
    Monetization API in 3.4.0 and 3.4.1 and Adv API in 3.4.0 works just fine regarding event callbacks.

    All cases tested with identical code. (I use a boolean to select which API should be used)
     
  12. jinming_cao

    jinming_cao

    Joined:
    Oct 2, 2019
    Posts:
    17
    I guess he meant that the issue is caused by missing the assignment to the two Action variables. But I believe these two public members are assigned outside AdManager class. Am I right? They are apparently not the cause of this issue.
     
  13. tessellation

    tessellation

    Joined:
    Aug 11, 2015
    Posts:
    390
    For the record I'm not seeing the described problem in Unity 2017.4.35f1. I have not tested it with other Unity versions.
     
  14. XSPR

    XSPR

    Joined:
    Jan 1, 2020
    Posts:
    5
    Hi Everyone ( @AdXxRi123 @jinming_cao @luispedrofonseca ),
    Today, I think I finally solved an issue I had with IUnityAdsListener callbacks not occurring in specific conditions. I'm not sure if this will help anyone, but may provide some insight, so I'll share my experience in trying to figure out Unity Ads.

    My fix seems to have been upgrading my project from Unity 2019.1.8f1 to 2019.2.19f1 (and then turning on/off some lightbulb icon in the Scene view), using Unity Hub.

    The problem I had in 2019.1.8f1 only occurred when I played the game _after_ the first time on Android. The first time playing my game after Build and Run to my Android device, IUnityAdsListener callback functions would work fine as expected (confirmed via logcat debug lines). If I pressed the Back button, however, and touched the icon to restart the (cached/still exists in Recent Apps/swipe list) game from the Android homescreen, the IUnityAdsListener callbacks would NOT happen. (If I swiped the game off the recent apps list, and played it again, it would work fine but only the first time.)

    At some point, I added .RemoveListener(this); in an OnDestroy() of my ads script. I can't remember exactly, but I think this allowed me to get it working correctly if I replayed the level with the Ad in it (i.e., reloaded the scene) during the first time running the game, i.e. correctly get IUnityAdsListener callbacks. But that was all occurring _before_ a Back button press and restarting the game from the homescreen.

    I was about to try .RemoveAllListeners btw, but it looks like I don't need to go that route. I had been using .AddListener very early in the script, but discovered that, in the problem cases (i.e. non-first fresh runs), Advertiser.listeners already existed. Also, in both working and problem cases, the Ads themselves were already cached or stored from the last time, i.e. .IsReady worked and was true, so basically I would SEE the ads get shown, just not know when they finished in the problem cases. (it seems like the ads got readied from the last time, very soon after Advertisement.Show() was called, and that would survive through restarting the game from homescreen.)

    I had been using Unity Ads 3.4.1 and still use it now. In the older install (Unity 2019.1.8f1), I used the Package Manager to downgrade to various other Unity Ads versions mentioned in these threads like 3.4.0, 3.3.1 I think, and 2.0.8 but none solved the problem so I stayed with 3.4.1. I did turn off the extra option in Services "Enable built-in Ads extension" under Advanced, but I don't think that had much effect actually. I was worried about potential conflicting installs of the libraries/package, and I can't even remember how I first obtained Unity Ads, whether via the Asset Store or Package Manager, just that I deleted some files/contents after getting it ( @Kaymax knows the process isn't exactly clear), and it worked mostly, except for my problem. I think I have 3.4.1, but who knows exactly, cause sometimes .version tells me 3.4.0, but I read somewhere .version isn't always accurate (does Charles know, who is Charles, something about a method to confirm-- but I didn't do the Charles thing, in any case, to get it working as, well, you know. advertised).

    Related threads on IUnityAdsListener callbacks not happening as expected:
    https://forum.unity.com/threads/iunityadslistener-is-not-getting-callbacks.794514/
    https://forum.unity.com/threads/unity-video-ad-works-fine-until-scene-is-reloaded.804267/