Search Unity

Question Unity Ads Initializing not working

Discussion in 'Unity Ads & User Acquisition' started by jdg23, Jan 21, 2023.

  1. jdg23

    jdg23

    Joined:
    Feb 10, 2020
    Posts:
    16
    Unity version : 2020.3.25f1
    Advertisement 3.7.5

    Hi,

    I can't manage to get the ads initializer script to work, the OnInitiliazationComplete or OnInitializeationFailed are never fired.

    Build settings is on Android
    Ads Service is on.
    Project ads is set in unity dashboard
    Game ID for android and ios is correctly set in Projet Settings and on the gameobject with the ads initializer class.
    Testmode is set to true in the game object inspector.
    Debug log in awake and in InitializeAds can be seen when in Play.


    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Advertisements;
    4. public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener
    5. {
    6.     [SerializeField] string _androidGameId;
    7.     [SerializeField] string _iOSGameId;
    8.     [SerializeField] bool _testMode = true;
    9.     private string _gameId;
    10.     void Awake()
    11.     {
    12.         InitializeAds();
    13.         Debug.Log("Start initializer");
    14.     }
    15.     public void InitializeAds()
    16.     {
    17.         _gameId = (Application.platform == RuntimePlatform.IPhonePlayer)
    18.             ? _iOSGameId
    19.             : _androidGameId;
    20.         Advertisement.Initialize(_gameId, _testMode, this);
    21.         Debug.Log("Initializer has been fired");
    22.     }
    23.     public void OnInitializationComplete()
    24.     {
    25.         Debug.Log("Unity Ads initialization complete.");
    26.     }
    27.     public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    28.     {
    29.         Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
    30.     }
    31. }
    32.  
    Thanks
     
  2. jdg23

    jdg23

    Joined:
    Feb 10, 2020
    Posts:
    16
    I've kept working to try to see if playing ads would function despite not having initialization feedback and it works as intented, so I guess it might just be a bug on the initialization callback
     
  3. aylin_unity3d

    aylin_unity3d

    Unity Technologies

    Joined:
    Apr 6, 2015
    Posts:
    48
    Hi @jdg23 Are you using the embedded unity ads SDK version in Unity by just turning on Unity Ads on the service window?
    Anyway, you have to update unityads SDK to at least 4.0.1 (or upper than 4.0.1) to comply with google's data policy.
    So could you download unityads SDK through the package manager and try to run your code again?
    Please refer to this to know how to get unityads SDK through package manager
    https://docs.unity.com/ads/en/manual/InstallingTheUnitySDK
     
  4. jdg23

    jdg23

    Joined:
    Feb 10, 2020
    Posts:
    16
    Hi,

    Thank you for your answer.

    As I said in my first answer, I saw that the ads actually worked as intented despite not having initialization confirmation at the launch of the app so I thought that problem was resolved.

    But as you indicated, you recommend to update the SDK to at least version 4.0.1. When we first imported the Advertissement package, we saw in Project Settings / Services/ Ads that we could update it from 3.7.5 to 4.4.1 which actually is a different package if I understood correctly, with different functions.

    For instance, after upgrading to 4.4.1, the initialization script was not working anymore as IUnityAdsInitializationListener does not seem to be in the new version.

    I was able to revert back to a previous version of the project with source control but if you say that we really need to upgrade the package, then what would be the new code for initialization and playing ads?

    Thanks
     
  5. aylin_unity3d

    aylin_unity3d

    Unity Technologies

    Joined:
    Apr 6, 2015
    Posts:
    48
    Hi @jdg23 IUnityAdsInitializationListener should work on AdsSdk 4.4.1
    Could you test your script with live ads on a real device? (in your code, _testMode=false)
     
  6. jdg23

    jdg23

    Joined:
    Feb 10, 2020
    Posts:
    16
    Hi again,
    Thank you, it's working. My mistake was that a pop up appeared asking me if I wanted to use the new ad with mediation or the legacy one and it was because I chose to use the new ad with mediation instead of the legacy.
     
  7. ShinyOpal

    ShinyOpal

    Joined:
    Dec 17, 2022
    Posts:
    32
    There is not much you can do with the above code.
    Couple of listeners are missing.
    Here is a code snippet which works well in the test mode, even if you wish to show ads multiple times.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Advertisements;
    3.  
    4. public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
    5. {
    6.     [SerializeField] string _androidGameId;
    7.     [SerializeField] string _iOSGameId;
    8.     [SerializeField] bool _testMode = true;
    9.     private string _gameId;
    10.  
    11.     [SerializeField] string _androidAdUnitId = "Interstitial_Android";
    12.     [SerializeField] string _iOsAdUnitId = "Interstitial_iOS";
    13.     string _adUnitId;
    14.  
    15.     void Awake()
    16.     {
    17.         _adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
    18.             ? _iOsAdUnitId
    19.             : _androidAdUnitId;
    20.  
    21.         InitializeAds();
    22.     }
    23.  
    24.     public void InitializeAds()
    25.     {
    26.         _gameId = (Application.platform == RuntimePlatform.IPhonePlayer)
    27.             ? _iOSGameId
    28.             : _androidGameId;
    29.        
    30.         if (!Advertisement.isInitialized) {
    31.             Advertisement.Initialize(_gameId, _testMode, this);
    32.         }
    33.         else {
    34.             LoadAd();
    35.             ShowAd();
    36.         }
    37.     }
    38.  
    39.     public void OnInitializationComplete()
    40.     {
    41.         LoadAd();
    42.     }
    43.  
    44.     public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    45.     {
    46.         Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
    47.     }
    48.  
    49.     // Load content to the Ad Unit:
    50.     public void LoadAd()
    51.     {
    52.         // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
    53.         Advertisement.Load(_adUnitId, this);
    54.     }
    55.  
    56.     // Show the loaded content in the Ad Unit:
    57.     public void ShowAd()
    58.     {
    59.         // Note that if the ad content wasn't previously loaded, this method will fail
    60.         Advertisement.Show(_adUnitId, this);
    61.     }
    62.  
    63.     // Implement Load Listener and Show Listener interface methods:
    64.     public void OnUnityAdsAdLoaded(string adUnitId)
    65.     {
    66.         // Optionally execute code if the Ad Unit successfully loads content.
    67.         ShowAd();
    68.     }
    69.  
    70.     public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
    71.     {
    72.         Debug.Log($"Error loading Ad Unit: {adUnitId} - {error.ToString()} - {message}");
    73.         // Optionally execute code if the Ad Unit fails to load, such as attempting to try again.
    74.         //TODO go to next scene instead
    75.     }
    76.  
    77.     public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
    78.     {
    79.         Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
    80.         // Optionally execute code if the Ad Unit fails to show, such as loading another ad.
    81.         //TODO go to next scene instead
    82.     }
    83.  
    84.     public void OnUnityAdsShowStart(string adUnitId) {
    85.     }
    86.     public void OnUnityAdsShowClick(string adUnitId) {
    87.     }
    88.     public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState) {
    89.         SceneManager.LoadScene("SceneName", LoadSceneMode.Single);
    90.     }
    91.  
    92.  
    93.  
    94. }
    95.  
     
    blamapps18 likes this.
  8. blamapps18

    blamapps18

    Joined:
    Jan 16, 2019
    Posts:
    3
    Hello. I installed 4.4.1 Unity Sdk and is seems that ads initialization never completes:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Advertisements;
    4.  
    5. public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
    6. {
    7.  
    8.     [SerializeField] string _iOSGameId;
    9.     [SerializeField] bool _testMode = true;
    10.     private readonly string _adUnitId = "Interstitial_iOS";
    11.  
    12.     void Awake()
    13.     {
    14.         InitializeAds();
    15.     }
    16.  
    17.     public void InitializeAds()
    18.     {
    19.  
    20.         if (!Advertisement.isInitialized)
    21.         {
    22.             Advertisement.Initialize(_iOSGameId, _testMode, this);
    23.             Debug.Log("ADS: Advertisement.Initialize launched");
    24.         }
    25.         else
    26.         {
    27.             LoadAd();
    28.          
    29.         }
    30.  
    31.  
    32.  
    33.     }
    34.  
    35.     // Show the loaded content in the Ad Unit:
    36.     public void ShowAd()
    37.     {
    38.  
    39.         Debug.Log("ADS: ShowAd launched!");
    40.         // Note that if the ad content wasn't previously loaded, this method will fail
    41.         Advertisement.Show(_adUnitId, this);
    42.     }
    43.  
    44.  
    45.  
    46.     // Load content to the Ad Unit:
    47.     public void LoadAd()
    48.     {
    49.         Debug.Log("ADS: in LoadAd");
    50.         // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
    51.      
    52.         if (Advertisement.isInitialized)
    53.         {
    54.            
    55.             Advertisement.Load(_adUnitId, this);
    56.             Debug.Log("ADS: Advertisement.Load launched");
    57.         }
    58.         else
    59.         {
    60.             InitializeAds();
    61.         }
    62.     }
    63.  
    64.     void IUnityAdsInitializationListener.OnInitializationComplete()
    65.     {
    66.         Debug.Log("ADS: INITIALIZATION COMPLETE!");
    67.         LoadAd();
    68.     }
    69.  
    70.     void IUnityAdsInitializationListener.OnInitializationFailed(UnityAdsInitializationError error, string message)
    71.     {
    72.         Debug.Log("ADS: in OnInitializationFailed");
    73.         InitializeAds();
    74.     }
    75.  
    76.     void IUnityAdsLoadListener.OnUnityAdsAdLoaded(string placementId)
    77.     {
    78.         Debug.Log("ADS: Ads loaded!");
    79.        
    80.     }
    81.  
    82.     void IUnityAdsLoadListener.OnUnityAdsFailedToLoad(string placementId, UnityAdsLoadError error, string message)
    83.     {
    84.         LoadAd();
    85.     }
    86.  
    87.     void IUnityAdsShowListener.OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message)
    88.     {
    89.         LoadAd();
    90.     }
    91.  
    92.     void IUnityAdsShowListener.OnUnityAdsShowStart(string placementId)
    93.     {
    94.        
    95.     }
    96.  
    97.     void IUnityAdsShowListener.OnUnityAdsShowClick(string placementId)
    98.     {
    99.      
    100.     }
    101.  
    102.     void IUnityAdsShowListener.OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState)
    103.     {
    104.         LoadAd();
    105.        
    106.     }
    107. }
    108.  
     
  9. ShinyOpal

    ShinyOpal

    Joined:
    Dec 17, 2022
    Posts:
    32
    You are missing ShowAd on the line 28.

    Is there any particular reason that you decided to change my code snippet ?
    Have you tried it out first ?
    Even if you will release only on iOS it will still function properly.
    Did you setup the _iOSGameId in the Unity Editor ?

    Which version of the Unity Editor are you using ?
    I have the Advertisement sdk 4.3.0 in the Package Manager.
     
  10. blamapps18

    blamapps18

    Joined:
    Jan 16, 2019
    Posts:
    3
    Hello, thanks for your message.
    I have 2021.3.17f1. And Advertisement Legacy 4.4.1
    One month ago the iOS build didn't have PODS folder and the ads worked. Now the build comes with PODS, unity mediation and I think that this is the cause of the problem. I'm not an expert in iOS XCODE, so I can't change the code to go back to a build without pods
     
  11. ShinyOpal

    ShinyOpal

    Joined:
    Dec 17, 2022
    Posts:
    32
    I am using 2021.3.18f1, however I ditched the Advertisement Legacy and use the Advertisement 4.3.0 instead.
    The code above works with the Advertisement 4.3.0.
    Have not tested it with the Advertisement Legacy.
    Somewhere I read that Unity recommends using Advertisement instead of the Advertisement Legacy.

    PODS sounds like the CocoaPods, the dependency manager. Very unlikely that could make the problem.
     
  12. heltonluizsb

    heltonluizsb

    Joined:
    Dec 20, 2018
    Posts:
    23
    Hi

    This line
    Advertisement.Initialize(myGameIdIOS, testMode, this);
    doesn't work in APK. I created a loop to run this each 2 seconds and when I try to start the banner, it shows "UnityAds is not initialized" (OnBannerError)

    In Editor is just fine.
     
    SkyWingDev likes this.
  13. dick

    dick

    Joined:
    Sep 6, 2014
    Posts:
    91

    I had an initialization problem on android too, but it turns out it was an internet problem. (my internet worked fine, as it went through perfectly on the editor) but I had a VPN running on my phone. So I turned off the VPN and it worked. Also heard someone else say that it didn't work when testing on a public network. Hopefully this helps someone