Search Unity

Resolved public async Task InitServices() not working

Discussion in 'Unity Mediation' started by Valentinhon, Jan 19, 2023.

  1. Valentinhon

    Valentinhon

    Joined:
    Oct 20, 2019
    Posts:
    76
    Hello guys,

    I face two issues I don't understand :
    1°) Banner ad issue :
    I'm using the banner ad example but no banner appear :

    Here the script that "Code Generator" of Unity gives me :

    Code (CSharp):
    1. public class BannerAdDisplay : MonoBehaviour, IDisposable
    2. {
    3.    
    4. IBannerAd ad;
    5.     string adUnitId = "Banner_Android";
    6.     string gameId = "4493651";
    7.  
    8.     public async Task InitServices()
    9.     {
    10.         Debug.Log("Test initservice");
    11.         try
    12.         {
    13.             InitializationOptions initializationOptions = new InitializationOptions();
    14.             initializationOptions.SetGameId(gameId);
    15.             await UnityServices.InitializeAsync(initializationOptions);
    16.  
    17.             InitializationComplete();
    18.         }
    19.         catch (Exception e)
    20.         {
    21.             InitializationFailed(e);
    22.         }
    23.     }
    24.  
    25.     public void SetupAd()
    26.     {
    27.         Debug.Log("Test SetupAd");
    28.         //Create
    29.         ad = MediationService.Instance.CreateBannerAd(
    30.             adUnitId,
    31.             BannerAdPredefinedSize.Banner.ToBannerAdSize(),
    32.             BannerAdAnchor.TopCenter,
    33.             Vector2.zero);
    34.  
    35.         //Subscribe to events
    36.         ad.OnRefreshed += AdRefreshed;
    37.         ad.OnClicked += AdClicked;
    38.         ad.OnLoaded += AdLoaded;
    39.         ad.OnFailedLoad += AdFailedLoad;
    40.  
    41.         // Impression Event
    42.         MediationService.Instance.ImpressionEventPublisher.OnImpression += ImpressionEvent;
    43.     }
    44.  
    45.     public void Dispose() => ad?.Dispose();
    46.  
    47.  
    48.     void InitializationComplete()
    49.     {
    50.         Debug.Log("Test init complete");
    51.         SetupAd();
    52.         LoadAd();
    53.     }
    54.  
    55.     async Task LoadAd()
    56.     {
    57.         Debug.Log("Test LoadAd");
    58.         try
    59.         {
    60.             await ad.LoadAsync();
    61.         }
    62.         catch (LoadFailedException)
    63.         {
    64.             // We will handle the failure in the OnFailedLoad callback
    65.         }
    66.     }
    67.  
    68.     void InitializationFailed(Exception e)
    69.     {
    70.         Debug.Log("Test init failed");
    71.         Debug.Log("Initialization Failed: " + e.Message);
    72.     }
    73.  
    74.     void AdLoaded(object sender, EventArgs e)
    75.     {
    76.         Debug.Log("Test AdLoaded");
    77.         Debug.Log("Ad loaded");
    78.     }
    79.  
    80.     void AdFailedLoad(object sender, LoadErrorEventArgs e)
    81.     {
    82.         Debug.Log("Test AdFailedLoad");
    83.         Debug.Log("Failed to load ad");
    84.         Debug.Log(e.Message);
    85.     }
    86.  
    87.     void AdRefreshed(object sender, LoadErrorEventArgs e)
    88.     {
    89.         Debug.Log("Refreshed ad");
    90.         Debug.Log(e.Message);
    91.     }
    92.  
    93.     void AdClicked(object sender, EventArgs e)
    94.     {
    95.         Debug.Log("Ad has been clicked");
    96.         // Execute logic after an ad has been clicked.
    97.     }
    98.  
    99.     void ImpressionEvent(object sender, ImpressionEventArgs args)
    100.     {
    101.         var impressionData = args.ImpressionData != null ? JsonUtility.ToJson(args.ImpressionData, true) : "null";
    102.         Debug.Log("Impression event from ad unit id " + args.AdUnitId + " " + impressionData);
    103.     }
    104.  
    105.  
    106. }
    Not error appear in console even all the Debug.Log don't appear

    2°) Reward ad
    There is a error at line cs:57 "
    NullReferenceException: Object reference not set to an instance of an object
    AdDisplay.ShowAd () (at Assets/ScriptGame/AdDisplay.cs:57)"
    The line 57 is "if (ad.AdState == AdState.Loaded)"
    Here's the script give by "Code Generator" in Unity



    Code (CSharp):
    1. public class AdDisplay : MonoBehaviour, IDisposable
    2. {
    3.     IRewardedAd ad;
    4.     string adUnitId = "supportAd";
    5.     string gameId = "4493651";
    6.     int oneMoreLife;
    7.  
    8.     public GameObject panel;
    9.  
    10.     public async Task InitServices()
    11.     {
    12.         Debug.Log("Test InitService !");
    13.         try
    14.         {
    15.             InitializationOptions initializationOptions = new InitializationOptions();
    16.             initializationOptions.SetGameId(gameId);
    17.             await UnityServices.InitializeAsync(initializationOptions);
    18.  
    19.             InitializationComplete();
    20.         }
    21.         catch (Exception e)
    22.         {
    23.             InitializationFailed(e);
    24.         }
    25.     }
    26.  
    27.     public void SetupAd()
    28.     {
    29.         Debug.Log("Test Setup !");
    30.         //Create
    31.         ad = MediationService.Instance.CreateRewardedAd(adUnitId);
    32.  
    33.         //Subscribe to events
    34.         ad.OnClosed += AdClosed;
    35.         ad.OnClicked += AdClicked;
    36.         ad.OnLoaded += AdLoaded;
    37.         ad.OnFailedLoad += AdFailedLoad;
    38.         ad.OnUserRewarded += UserRewarded;
    39.  
    40.         // Impression Event
    41.         MediationService.Instance.ImpressionEventPublisher.OnImpression += ImpressionEvent;
    42.     }
    43.  
    44.     public void Dispose() => ad?.Dispose();
    45.  
    46.  
    47.     public async void ShowAd()
    48.     {
    49.         Debug.Log("Test ShowAd !");
    50.         [COLOR=#ff0000][U]if (ad.AdState == AdState.Loaded)[/U][/COLOR]
    51.         {
    52.             try
    53.             {
    54.                 RewardedAdShowOptions showOptions = new RewardedAdShowOptions();
    55.                 showOptions.AutoReload = true;
    56.                 await ad.ShowAsync(showOptions);
    57.                 AdShown();
    58.             }
    59.             catch (ShowFailedException e)
    60.             {
    61.                 AdFailedShow(e);
    62.             }
    63.         }
    64.     }
    65.  
    66.     void InitializationComplete()
    67.     {
    68.         Debug.Log("Test InitilizationComplete !");
    69.         SetupAd();
    70.         LoadAd();
    71.     }
    72.  
    73.     async Task LoadAd()
    74.     {
    75.         Debug.Log("Test LoadAd !");
    76.         try
    77.         {
    78.             await ad.LoadAsync();
    79.         }
    80.         catch (LoadFailedException)
    81.         {
    82.             // We will handle the failure in the OnFailedLoad callback
    83.         }
    84.     }
    85.  
    86.     void InitializationFailed(Exception e)
    87.     {
    88.         Debug.Log("Initialization Failed: " + e.Message);
    89.     }
    90.  
    91.     void AdLoaded(object sender, EventArgs e)
    92.     {
    93.         Debug.Log("Coucou Ad loaded");
    94.     }
    95.  
    96.     void AdFailedLoad(object sender, LoadErrorEventArgs e)
    97.     {
    98.         Debug.Log("Coucou Failed to load ad");
    99.         Debug.Log(e.Message);
    100.     }
    101.  
    102.     public void AdShown()
    103.     {
    104.         Debug.Log("Ad shown!");
    105.        
    106.     }
    107.  
    108.     void AdClosed(object sender, EventArgs e)
    109.     {
    110.         Debug.Log("Ad has closed");
    111.         // Execute logic after an ad has been closed.
    112.     }
    113.  
    114.     void AdClicked(object sender, EventArgs e)
    115.     {
    116.         Debug.Log("Ad has been clicked");
    117.         // Execute logic after an ad has been clicked.
    118.     }
    119.  
    120.     void AdFailedShow(ShowFailedException e)
    121.     {
    122.         Debug.Log("Test AdFailedShow !");
    123.         Debug.Log(e.Message);
    124.     }
    125.  
    126.     void ImpressionEvent(object sender, ImpressionEventArgs args)
    127.     {
    128.         var impressionData = args.ImpressionData != null ? JsonUtility.ToJson(args.ImpressionData, true) : "null";
    129.         Debug.Log("Impression event from ad unit id " + args.AdUnitId + " " + impressionData);
    130.     }
    131.  
    132.     void UserRewarded(object sender, RewardEventArgs e)
    133.     {
    134.         Debug.Log($"Received reward: type:{e.Type}; amount:{e.Amount}");
    135.     }
    136. }
    Once again all the "Debug.log" don't appear in console.

    For the two issues, it seems like InitServices() is never call, so InitServices() never call InitializationComplete(), so InitializationComplete() never call SetupAd() so the IBannerAd variable "ad" is never assign
    Could you please help me to solve that ?

    Regards,

    Val
     
  2. Valentinhon

    Valentinhon

    Joined:
    Oct 20, 2019
    Posts:
    76
    Solution : Call the "InitServices()" function