Unity AdMob plugin - Positioning Banner, Banner Size vs Screen Pixels, don't match.. Ideas?

Hi,

I’m using the AdMob Unity Plugin to show banners, this is simple to configure and works, except custom positions don’t seem to match the screen pixels.

I’m wanting a horizontally centred banner somewhat below the top of the screen. This is the code I’d expect to use to achieve that:

bannerView = new BannerView(bannerAdUnitId, AdSize.Banner, (Screen.width - AdSize.Banner.Width) /2, 80);

But the banner size is defined in the plugin as AdSize Banner = new AdSize(320, 50); and yet, a 320 unit wide banner nearly fills the width of my 1080 device.

As such, (Screen.width - AdSize.Banner.Width) /2 does not give the correct left padding to centre the Ad.

How are these AdSize units mapped to screen pixels?

Any ideas?

Ok, so I think I’ve found a hacky solution (from various posts around the interwebs)

Seems AdMob gives different size banners based on the device’s dpi. The following is working ok on my devices, not sure how applicable this is for other devices though… The code I’m using is:

int bannerSizeToPixels(int size)
{
  return size * Mathf.RoundToInt(Screen.dpi / 160);
}

int pixelsToBannerSize(int size)
{
  return size / Mathf.RoundToInt(Screen.dpi / 160);
}

It’d be nice if their API could give us the size in pixels to avoid this hackery which is bound to fail somewhere :frowning:

3 Likes

I dont know why Screen.dpi / 160 ???

1 Like

The 160 has to do with the ‘dp’ unit. DP unit is meant to represent device independent pixel size. The idea is that:

1 dp = 1 pixel in a 160 dots-per-inch screen.
1 dp * x dpi / 160 dpi = 1px Where x is your screen dpi

E.g in a 400 dpi screen, a 300 dp size =
300 * 400 / 160 = 750 pixels

whereas in a 300 dpi screen, a 300 dp size =
300 * 300 / 160 = 562.5 pixels. Smaller, so that it looks “as big” on the lower pixel-density device.

Hi,

How you implement this to ad location? cant figured it out