Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Example of 4.x Unity Ads interface implementation, can't make it work

Discussion in 'Unity Ads & User Acquisition' started by arcandio, Sep 2, 2022.

  1. arcandio

    arcandio

    Joined:
    Aug 30, 2009
    Posts:
    89
    I'm trying to update my old banner ads implementation to 4.3, but I don't understand how the interfaces are supposed to be used. I've implemented IUnityAdsInitializationListener, IUnityAdsLoadListener, and IUnityAdsShowListener, and removed the callbacks from the old version. I had assumed that the interface methods like OnUnityAdsAdLoaded would be called by methods like Advertisement.Banner.Load(adUnitId), but in my case, they are not. Code for my implementation follows.

    Questions:
    • Are there implementation examples for Ads 4.0+, and if so, where?
    • Do I need to use the interfaces instead of the callbacks?
    • Do I need to use the callbacks instead of the interfaces?
    • Do I need to use BOTH the interfaces and the callbacks?
    My implementation follows. It reaches OnInitializationComplete but then fails to ever send
    OnUnityAdsAdLoaded

    Code (CSharp):
    1.         void Initialize() {
    2.             Debug.Log("Ads Initialize");
    3.             adBuffers = Resources.FindObjectsOfTypeAll<AdBuffer>();
    4.             Advertisement.Banner.SetPosition(bannerPosition);
    5.  
    6.             foreach (AdBuffer adBuffer in adBuffers)
    7.             {
    8.                 if (adBuffer.gameObject.scene.name != null)
    9.                 {
    10.                     adBuffer.Initialize(this, bannerPosition, offset);
    11.                 }
    12.             }
    13.             Advertisement.Initialize(gameId, false, this);
    14.         }
    15.  
    16.         public void OnInitializationComplete() {
    17.             DebugAds("OnInitializationComplete");
    18.             Advertisement.Banner.Load(adUnitId);
    19.         }
    20.  
    21.         public void OnInitializationFailed(UnityAdsInitializationError error, string message) {
    22.             Debug.LogError($"OnInitializationFailed: {error.ToString()} - {message}");
    23.         }
    24.  
    25.         public void OnUnityAdsAdLoaded(string placementId) {
    26.             Debug.Log("OnUnityAdsAdLoaded: " + placementId);
    27.             Advertisement.Banner.Show(adUnitId);
    28.         }
    29.  
    30.         public void OnUnityAdsFailedToLoad(string placementId, UnityAdsLoadError error, string message) {
    31.             Debug.LogError($"OnUnityAdsFailedToLoad: {error.ToString()} - {message}");
    32.         }
    33.  
    34.         public void OnUnityAdsShowStart(string placementId) {
    35.             Debug.Log("OnUnityAdsShowStart callback Advertisement.isShowing: " + Advertisement.isShowing);
    36.             OpenBannerDrawer();
    37.             adsAreShowing = true;
    38.         }
    39.  
    40.         public void OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message) {
    41.             Debug.LogError($"OnUnityAdsShowFailure: {error.ToString()} - {message}");
    42.         }
    43.  
    44.         public void OnUnityAdsShowClick(string placementId) {
    45.             Debug.Log("OnUnityAdsShowClick");
    46.         }
    47.  
    48.         public void OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState) {
    49.             Debug.Log("OnUnityAdsShowComplete");
    50.         }
    51.  
     
  2. ShinyOpal

    ShinyOpal

    Joined:
    Dec 17, 2022
    Posts:
    32
    Haven't tried banners yet, however this snippet for interstitial ads works for me

    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.