unity-game-engineadmobgoogleads-mobile-unity

How to display Unity Google AdMob banner in center of screen


I have a Google AdMob Banner that I want to display on a custom position. The line below works just fine in my...

...
    bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
...

Though when I want to place the banner at a custom position it never displays on the screen. I am doing:

...
    int w = 0;
    int h = Screen.height/2;

    bannerView = new BannerView(adUnitId, AdSize.Banner, w, -h);
...

What am I doing wrong?

Docs for AdMob Banner ad can be found here: https://developers.google.com/admob/unity/banner

Any help is much appreciated! :)


Solution

  • As the documentation that you provided states:

    The top-left corner of the BannerView will be positioned at the x and y values passed to the constructor, where the origin is the top-left of the screen.

    Your error probably is that your passing a negative value to the y parameter, causing the BannerView to be positioned outside of the top border of the Screen.
    So, your code should look like this, if you want the banner view to be centered only by height:

    ...
    int w = 0;
    int h = Screen.height/2;
    
    bannerView = new BannerView(adUnitId, AdSize.Banner, w, h);
    ...
    

    Remind that your not taking care of the actual height of the BannerView. To have it centered taking care of its height, this should work:

    ...
    int w = 0;
    int bannerHeight = 50; //Because AdSize.Banner measures 320x50
    int h = Screen.height/2 - bannerHeight/2;
    
    bannerView = new BannerView(adUnitId, AdSize.Banner, w, h);
    ...
    

    Banner sizes documentation