Search Unity

Question no line item filled! only at first load

Discussion in 'Unity Mediation' started by Danny9421, Nov 13, 2022.

  1. Danny9421

    Danny9421

    Joined:
    Mar 21, 2022
    Posts:
    36
    I have build my app as Appbundle and uploaded to google play console for internal test. When running a rewarded android mediation ad (ad source: unity only) it does work, but:
    - The first ad which i try to load does not load and response with "no line item filled!"
    The error occurs at: async void LoadAd() - > catch (LoadFailedException erg)

    Afterwards every following ad load witouth any issue.

    I have a button where a player can click to watch a ad for rewards. I dont preload/preintialize ads because i dont want my user to waste traffic/performance load/battery... which dont use the rewarded ad button at all. So i call the "StartAds" function the first time on button press and fill the wait time with a loading animation.

    What could be the reason for this problem and how can i fix it ?

    My simplified code:
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using Unity.Services.Core;
    4. using System.Threading.Tasks;
    5. using UnityEngine.UI;
    6.  
    7. namespace Unity.Services.Mediation.Samples
    8. {
    9.     public class AdsShow : MonoBehaviour
    10.     {
    11.         public MapsDrop mapsDrop;
    12.         public int whichQuest;
    13.         public string adUnitId = "Rewarded_Android";
    14.         public string androidAdUnitId = "REMOVED";
    15.         public string iosAdUnitId = "REMOVED";
    16.  
    17.         IRewardedAd m_RewardedAd;
    18.  
    19.         public async void StartAds(int actWhichQuest)
    20.         {
    21.             try
    22.             {
    23.                 InitializationOptions initializationOptions = new InitializationOptions();
    24.                 if(Application.platform == RuntimePlatform.Android)
    25.                 {
    26.                     initializationOptions.SetGameId(androidAdUnitId);
    27.                 }else if (Application.platform == RuntimePlatform.IPhonePlayer)
    28.                 {
    29.                     initializationOptions.SetGameId(iosAdUnitId);
    30.                 }else if(Application.platform == RuntimePlatform.WindowsEditor)
    31.                 {
    32.                     initializationOptions.SetGameId(androidAdUnitId);
    33.                 }
    34.  
    35.                 await UnityServices.InitializeAsync();
    36.                 whichQuest = actWhichQuest;
    37.                 InitializationComplete();
    38.             }
    39.             catch (Exception e)
    40.             {
    41.                 InitializationFailed(e);
    42.             }
    43.         }
    44.  
    45.         void OnDestroy()
    46.         {
    47.             m_RewardedAd?.Dispose();
    48.         }
    49.  
    50.         async void ShowRewarded()
    51.         {
    52.             if (m_RewardedAd?.AdState == AdState.Loaded)
    53.             {
    54.                 try
    55.                 {
    56.                     RewardedAdShowOptions showOptions = new RewardedAdShowOptions();
    57.                     showOptions.AutoReload = true;
    58.                     await m_RewardedAd.ShowAsync(showOptions);
    59.                 }
    60.                 catch (ShowFailedException erg)
    61.                 {
    62.                 }
    63.             }
    64.         }
    65.  
    66.         async void LoadAd()
    67.         {
    68.             try
    69.             {
    70.                 await m_RewardedAd.LoadAsync();
    71.                 ShowRewarded();
    72.             }
    73.             catch (LoadFailedException erg)
    74.             {
    75.                 // Error occurs here
    76.             }
    77.         }
    78.  
    79.         void InitializationComplete()
    80.         {
    81.             MediationService.Instance.ImpressionEventPublisher.OnImpression += ImpressionEvent;
    82.  
    83.             m_RewardedAd = MediationService.Instance.CreateRewardedAd(adUnitId);
    84.  
    85.             m_RewardedAd.OnLoaded += AdLoaded;
    86.             m_RewardedAd.OnFailedLoad += AdFailedLoad;
    87.             m_RewardedAd.OnUserRewarded += UserRewarded;
    88.             m_RewardedAd.OnClosed += AdClosed;
    89.  
    90.             LoadAd();
    91.         }
    92.  
    93.         void InitializationFailed(Exception error)
    94.         {
    95.             SdkInitializationError initializationError = SdkInitializationError.Unknown;
    96.             if (error is InitializeFailedException initializeFailedException)
    97.             {
    98.                 initializationError = initializeFailedException.initializationError;
    99.             }
    100.         }
    101.  
    102.         void UserRewarded(object sender, RewardEventArgs e)
    103.         {
    104.             mapsDrop.CompleteQuestByAdFinish(whichQuest);
    105.         }
    106.  
    107.         void AdClosed(object sender, EventArgs args)
    108.         {
    109.  
    110.         }
    111.  
    112.         void AdLoaded(object sender, EventArgs e)
    113.         {
    114.         }
    115.  
    116.         void AdFailedLoad(object sender, LoadErrorEventArgs e)
    117.         {
    118.  
    119.         }
    120.  
    121.         void ImpressionEvent(object sender, ImpressionEventArgs args)
    122.         {
    123.             var impressionData = args.ImpressionData != null ? JsonUtility.ToJson(args.ImpressionData, true) : "null";
    124.         }
    125.     }
    126. }
    127.  
     
  2. DeclanMcPartlin

    DeclanMcPartlin

    Unity Technologies

    Joined:
    Nov 19, 2020
    Posts:
    146
    Hi @Danny9421, we don't recommend you only initialize at the time of showing the ad, instead, we recommend initializing and loading at the beginning of the launch of the app, these requests are not very expensive in terms of bandwidth. In your case, there's a good chance that there may be some timeouts occurring, but without more information, it would be hard to say.

    I hope that helps, thanks for reaching out!
     
  3. Danny9421

    Danny9421

    Joined:
    Mar 21, 2022
    Posts:
    36
    Hi, thanks for your answer.
    The user return to the affected main scene every 20-30 seconds. Loading a 30 second video uses enough bandwidth to cause traffic, writing and battery drain and watching is fully optional. If the ad isnt cached over other scenes ad traffic would be more then 10 times higher then my own.
    There have to be a better solution, i definitly wont preload the ad.

    What informations do you need ? Is there a way to avoid a timeout by delaying any methode in the first run ?
     
  4. DeclanMcPartlin

    DeclanMcPartlin

    Unity Technologies

    Joined:
    Nov 19, 2020
    Posts:
    146
    Hi @Danny9421,

    I wouldn't recommend storing the ad in a specific scene, for sure. Use a singleton for example to initialize and store the ad across scenes. You should only initialize once per launch cycle (launch of the app -> kill the app).

    If you absolutely want to only load on the user event click, I would still recommend doing the above for initializing the SDK, then call load then show on the user event click. You'll also want to keep your waterfall as short as possible, since longer waterfalls significantly impact load times, so keep target eCPMs to a minimum (and low values only, preferably just backfill), and use header bidding as much as possible.

    Let us know if that works for you, thanks!
     
    Danny9421 likes this.