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.

Question When i should call again LoadBanner function

Discussion in 'Unity Ads & User Acquisition' started by Bokky-T, Dec 17, 2022.

  1. Bokky-T

    Bokky-T

    Joined:
    Apr 1, 2018
    Posts:
    9
    I'm calling LoadBanner function from another script when the ads are initialized. Should i call that function later again to load another (new) banner ad. I think i shoud call that function when the banner is clicked, hided or shown, but i'm not sure. I'm not shure when the "OnBannerShown" function is called. When it's shown in the beginning or competly shown, so i can call LoadBanner to load another ad.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Advertisements;
    4. using System.Collections;
    5.  
    6. public class BannerAd : MonoBehaviour
    7. {
    8.     [SerializeField] BannerPosition _bannerPosition = BannerPosition.BOTTOM_CENTER;
    9.  
    10.     [SerializeField] string _androidAdUnitId = "Banner_Android";
    11.     [SerializeField] string _iOSAdUnitId = "Banner_iOS";
    12.     string _adUnitId = null; // This will remain null for unsupported platforms.
    13.  
    14.     void Start()
    15.     {
    16.         // Get the Ad Unit ID for the current platform:
    17. #if UNITY_IOS
    18.         _adUnitId = _iOSAdUnitId;
    19. #elif UNITY_ANDROID
    20.         _adUnitId = _androidAdUnitId;
    21. #endif
    22.  
    23.         // Set the banner position:
    24.         Advertisement.Banner.SetPosition(_bannerPosition);
    25.     }
    26.  
    27.     // Implement a method to call when the Load Banner button is clicked:
    28.     public void LoadBanner()
    29.     {
    30.         // Set up options to notify the SDK of load events:
    31.         BannerLoadOptions options = new BannerLoadOptions
    32.         {
    33.             loadCallback = OnBannerLoaded,
    34.             errorCallback = OnBannerError
    35.         };
    36.  
    37.         // Load the Ad Unit with banner content:
    38.         Advertisement.Banner.Load(_adUnitId, options);
    39.     }
    40.  
    41.     // Implement code to execute when the loadCallback event triggers:
    42.     void OnBannerLoaded()
    43.     {
    44.         Debug.Log("Banner loaded");
    45.         ShowBannerAd();
    46.     }
    47.  
    48.     // Implement code to execute when the load errorCallback event triggers:
    49.     void OnBannerError(string message)
    50.     {
    51.         Debug.Log($"Banner Error: {message}");
    52.         // Optionally execute additional code, such as attempting to load another ad.
    53.         LoadBanner();
    54.     }
    55.  
    56.     // Implement a method to call when the Show Banner button is clicked:
    57.     void ShowBannerAd()
    58.     {
    59.         // Set up options to notify the SDK of show events:
    60.         BannerOptions options = new BannerOptions
    61.         {
    62.             clickCallback = OnBannerClicked,
    63.             hideCallback = OnBannerHidden,
    64.             showCallback = OnBannerShown
    65.         };
    66.  
    67.         // Show the loaded Banner Ad Unit:
    68.         Advertisement.Banner.Show(_adUnitId, options);
    69.     }
    70.  
    71.     // Implement a method to call when the Hide Banner button is clicked:
    72.     void HideBannerAd()
    73.     {
    74.         // Hide the banner:
    75.         Advertisement.Banner.Hide();
    76.     }
    77.  
    78.     void OnBannerClicked() { }
    79.     void OnBannerShown() { }
    80.     void OnBannerHidden() { }
    81.  
    82.     void OnDestroy()
    83.     {
    84.  
    85.     }
    86. }
     
  2. Yasuyuki

    Yasuyuki

    Unity Technologies

    Joined:
    Sep 11, 2014
    Posts:
    90
    Hi,

    You only need to call Banner.Load() before Show(). The banner will automatically refresh every 30 seconds, that setting can be changed from AdUnits dashboard.

     
    Bokky-T likes this.