Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question ANR at onUnityAdsFailedToLoad

Discussion in 'Unity Ads & User Acquisition' started by andrey_prokopyev, Jul 20, 2023.

  1. andrey_prokopyev

    andrey_prokopyev

    Joined:
    Apr 19, 2017
    Posts:
    3
    Unity 2020.3.42f1
    Package Advertisment Legacy 4.4.2

    After implementing ads I get around 2-3% ANRs according to Play Console. Most of them have same stack trace telling that the cause is in onUnityAdsFailedToLoad handler. However I don't see and problems on my implementation of this handler.
    I've attached stack trace and my ads handler source to this post. Can anyone tell me if there are any problems?

    Code (CSharp):
    1. using System;
    2. using Timer;
    3. using UnityEngine;
    4. using UnityEngine.Advertisements;
    5. using Zenject;
    6.  
    7. namespace Shop.Ads
    8. {
    9.     public class UnityAdvertiser : IInitializable, IAdvertiser, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
    10.     {
    11.         private readonly UnityAdvertiserSettings _settings;
    12.         private readonly SignalBus _bus;
    13.         private readonly TimerService _timerService;
    14.  
    15.         private Action<bool> _onComplete;
    16.  
    17.         private bool _isAdLoaded;
    18.  
    19.         public UnityAdvertiser(UnityAdvertiserSettings settings, SignalBus bus, TimerService timerService)
    20.         {
    21.             _settings = settings;
    22.             _bus = bus;
    23.             _timerService = timerService;
    24.         }
    25.  
    26.         public void ShowAds(string placement, Action<bool> onComplete)
    27.         {
    28.             if (!_isAdLoaded)
    29.             {
    30.                 onComplete(false);
    31.                 return;
    32.             }
    33.            
    34.             _onComplete = onComplete;
    35.            
    36.             var adUnitId = GetAdUnitId();
    37.             Advertisement.Show(adUnitId, this);
    38.         }
    39.  
    40.         private void LoadAds()
    41.         {
    42.             _isAdLoaded = false;
    43.             var adUnitId = GetAdUnitId();
    44.  
    45.             try
    46.             {
    47.                 Advertisement.Load(adUnitId, this);
    48.             }
    49.             catch (Exception e)
    50.             {
    51.                 Debug.LogError($"Could not load ads: {e}");
    52.             }
    53.         }
    54.  
    55.         private void LoadAdsDelayed()
    56.         {
    57.             _timerService.SetTimer(TimeSpan.FromSeconds(1f), LoadAds);
    58.         }
    59.  
    60.         private string GetAdUnitId()
    61.         {
    62.             return
    63. #if UNITY_ANDROID
    64.                 _settings.AndroidAdUnitId
    65. #elif UNITY_IOS
    66.                 _settings.IosAdUnitId
    67. #endif
    68.                 ;
    69.         }
    70.  
    71.         private void CallOnComplete(bool isCompleted)
    72.         {
    73.             var onComplete = _onComplete;
    74.             if (onComplete != null)
    75.             {
    76.                 _timerService.SetTimer(TimeSpan.FromSeconds(0.1f), () => onComplete.Invoke(isCompleted));
    77.             }
    78.         }
    79.  
    80.         void IUnityAdsInitializationListener.OnInitializationComplete()
    81.         {
    82.             Debug.Log("Ads initialization complete");
    83.             LoadAdsDelayed();
    84.         }
    85.  
    86.         void IUnityAdsInitializationListener.OnInitializationFailed(UnityAdsInitializationError error, string message)
    87.         {
    88.             Debug.LogError($"Error when initializing ads: {error}. {message}");
    89.         }
    90.  
    91.         void IUnityAdsLoadListener.OnUnityAdsAdLoaded(string placementId)
    92.         {
    93.             Debug.Log("Ads loaded");
    94.             _bus.Fire(new AdsStage(placementId, "loaded"));
    95.  
    96.             _isAdLoaded = true;
    97.         }
    98.  
    99.         void IUnityAdsLoadListener.OnUnityAdsFailedToLoad(string placementId, UnityAdsLoadError error, string message)
    100.         {
    101.             Debug.LogError($"Ads '{placementId}' failed loading {error}. {message}");
    102.             _bus.Fire(new AdsStage(placementId, "loading_failed", $"{error}. {message}"));
    103.            
    104.             LoadAdsDelayed();
    105.         }
    106.  
    107.         void IUnityAdsShowListener.OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message)
    108.         {
    109.             Debug.LogError($"Ads '{placementId}' failed showing {error}. {message}");
    110.             _bus.Fire(new AdsStage(placementId, "show_failed", $"{error}. {message}"));
    111.            
    112.             CallOnComplete(false);
    113.             LoadAdsDelayed();
    114.         }
    115.  
    116.         void IUnityAdsShowListener.OnUnityAdsShowStart(string placementId)
    117.         {
    118.             Debug.Log($"Ads '{placementId}' started showing");
    119.             _bus.Fire(new AdsStage(placementId, "show_started"));
    120.         }
    121.  
    122.         void IUnityAdsShowListener.OnUnityAdsShowClick(string placementId)
    123.         {
    124.             Debug.Log($"Ads '{placementId} show clicked");
    125.             _bus.Fire(new AdsStage(placementId, "show_clicked"));
    126.         }
    127.  
    128.         public void OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState)
    129.         {
    130.             Debug.Log($"Ads '{placementId} show completed {showCompletionState}");
    131.             CallOnComplete(true);
    132.             LoadAdsDelayed();
    133.            
    134.             _bus.Fire(new AdsStage(placementId, "show_completed"));
    135.             _bus.Fire(new AdsShown(placementId));
    136.         }
    137.  
    138.         void IInitializable.Initialize()
    139.         {
    140.             if (!Advertisement.isInitialized && Advertisement.isSupported)
    141.             {
    142.                 var gameId =
    143. #if UNITY_ANDROID
    144.                     _settings.AndroidGameId
    145. #elif UNITY_IOS
    146.                     _settings.IosGameId
    147. #endif
    148.                     ;
    149.                 Advertisement.Initialize(gameId, _settings.TestMode, this);
    150.                 LoadAds();
    151.             }
    152.         }
    153.     }
    154. }
     
  2. pixeldashbilling

    pixeldashbilling

    Joined:
    Apr 19, 2017
    Posts:
    6
    I’m currently getting this exact same ANR. Did you ever find a solution?
     
  3. andrey_prokopyev

    andrey_prokopyev

    Joined:
    Apr 19, 2017
    Posts:
    3
    Yes, threw away Unity Ads and integrated Applovin MAX