Search Unity

Bug Banner not displayed

Discussion in 'Unity Ads & User Acquisition' started by gamesuria, Feb 18, 2023.

  1. gamesuria

    gamesuria

    Joined:
    Feb 11, 2023
    Posts:
    5
    I could get the Interstitial ads to work on my Android device, but I could not see any banner ads.

    The banner ads are displayed on the Editor but not on Android devices. I am using test mode.

    Below, no error message was displayed either. What is wrong???



    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Advertisements;
    4.  
    5. public class adsBanner : MonoBehaviour
    6. {
    7.     public Text ct;
    8.     public static string em = "";
    9.     [SerializeField] BannerPosition _bannerPosition = BannerPosition.BOTTOM_CENTER;
    10.  
    11.     [SerializeField] string _androidAdUnitId = "Banner_Android";
    12.     [SerializeField] string _iOSAdUnitId = "Banner_iOS";
    13.     string _adUnitId = null; // This will remain null for unsupported platforms.
    14.  
    15.     void Start()
    16.     {
    17.         // Get the Ad Unit ID for the current platform:
    18. #if UNITY_IOS
    19.         _adUnitId = _iOSAdUnitId;
    20. #elif UNITY_ANDROID
    21.         _adUnitId = _androidAdUnitId;
    22. #endif
    23.  
    24.         // Set the banner position:
    25.         Advertisement.Banner.SetPosition(_bannerPosition);
    26.         LoadBanner();
    27.     }
    28.  
    29.     void OnEnable(){
    30.         if (PlayerPrefs.GetInt("ads") == 0) {
    31.             RectTransform r = this.GetComponent<RectTransform>();
    32.             r.offsetMin = new Vector2(0f, 0f);
    33.             return;
    34.         }
    35.         LoadBanner();
    36.     }
    37.  
    38.     void OnDisable(){
    39.         HideBannerAd();
    40.     }
    41.  
    42.     public void LoadBanner()
    43.     {
    44.         BannerLoadOptions options = new BannerLoadOptions
    45.         {
    46.             loadCallback = OnBannerLoaded,
    47.             errorCallback = OnBannerError
    48.         };
    49.  
    50.         Advertisement.Banner.Load(_adUnitId, options);
    51.     }
    52.  
    53.     void OnBannerLoaded()
    54.     {
    55.         if (GameObject.Find("Main Menu")) {
    56.             HideBannerAd();
    57.             return;
    58.         }
    59.         Debug.Log("Banner loaded");
    60.         ShowBannerAd();
    61.         RectTransform r = this.GetComponent<RectTransform>();
    62.         r.offsetMin = new Vector2(r.offsetMin.x, adHeight());
    63.     }
    64.  
    65.     public static float adHeight()
    66.     {
    67.         float f = Screen.dpi / 160f;
    68.         float dp = Screen.height / f;
    69.         return (dp > 720f) ? 90f * f
    70.               : (dp > 400f) ? 50f * f
    71.               : 32f * f;
    72.     }
    73.  
    74.     void OnBannerError(string message)
    75.     {
    76.         em= message;
    77.     }
    78.  
    79.     void Updata()
    80.     {
    81.         ct.text = em;
    82.     }
    83.  
    84.     void ShowBannerAd()
    85.     {
    86.         BannerOptions options = new BannerOptions
    87.         {
    88.             clickCallback = OnBannerClicked,
    89.             hideCallback = OnBannerHidden,
    90.             showCallback = OnBannerShown
    91.         };
    92.  
    93.         Advertisement.Banner.SetPosition(_bannerPosition);
    94.         Advertisement.Banner.Show(_adUnitId, options);
    95.         Advertisement.Banner.SetPosition(_bannerPosition);
    96.  
    97.     }
    98.  
    99.     void HideBannerAd()
    100.     {
    101.         Advertisement.Banner.Hide();
    102.     }
    103.  
    104.     void OnBannerClicked() { }
    105.     void OnBannerShown() { }
    106.     void OnBannerHidden() { }
    107.  
    108.     void OnDestroy()
    109.     { }
    110. }
     
  2. gamesuria

    gamesuria

    Joined:
    Feb 11, 2023
    Posts:
    5
    Do I need to publish the app on Google Play Store first?
     
  3. gamesuria

    gamesuria

    Joined:
    Feb 11, 2023
    Posts:
    5
    I deleted Google Mobile Ads. Do I need to keep it?
     
  4. gamesuria

    gamesuria

    Joined:
    Feb 11, 2023
    Posts:
    5
    I turned on Unity Ads on the Services window, and updated the library to the latest version. It is working now.