Determining the actual height of Admob SmartBanner in Unity for Android

So, I am embedding admob banners in my Android Unity 4.6 game. I have a banner below and need to consider it in my user interface. In particular, I need to know exactly how tall he is, but I am having problems with this.

Based on the information found here, the Smart Banner should be 32, 50 or 90 pixels in height, depending on the height of the device. This is usually not the case.

Some searches seem to indicate that this is due to pixel density. So I'm trying to convert the specified pixel height using px = dp * (Screen.dpi / 160). So, for example, if I determine that the banner height should be 90 pixels, I would use bannerHeight = 90 * (Screen.dpi / 160). This seems to work on some devices, but not on others.

For example, my Nexus 4 has a DPI 320. Using the above, it looks like the banner should be 180 pixels high, but the banner seems to be about 90 pixels. But on the Nexus 7 (which has an 166 inch), the banner, as you can see, is about 120 pixels when the formula indicates that it should be ~ 93.

So, I don’t know how to understand how high the banner really is, and I did not find a way to get this information from the API. My code for calling the banner is pretty good:

 string adUnitId = "my_id";

 BannerView bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);
 AdRequest request = new AdRequest.Builder().Build();
 bannerView.LoadAd(request);
+4
6

: fooobar.com/questions/934297/...

int heightPixels = AdSize.SMART_BANNER.getHeightInPixels(this);
+4

, Google (32 50 90?) .

/dpi =

Google , 720 ( dp 160dpi) acctauly 720/160 = 4,5 , , - 90 (dp!) 50 ! 400/160 = 2,5, 32 dp

SO! Xiaomi mi3 1920x1080 , 1920/480dpi = 4 , 50 dp-.

DP

px = dp * (Screen.dpi/160)

50 * (480/160) = 150 !

"" 1080 1920 1080/480dpi = 2,25 , 32 dp

:

32* (480/160) = 96 pixels in landscape

Google , .

:

https://developers.google.com/admob/android/banner

+2

, , , -

  • 32 dp .
  • 50 dp .
  • 90

, .

Nexus 4 DPI ~ 320 . , , 50 . , 50 * 320/160 = 100 ( )

Nexus 7s, , 216 (2012), 323 (2013), , 166 .
, 216 .., , dp 90. = 90 * 216/160 = 121,5, .

, Google

  • 400 720 50.
  • 720 90.

, , 400 720 MIGHT dp? 400 mdpi. Nexus 4, xhdpi (320 dp), 1280 , , mdpi 640 , 50 .

, . , .

0
bannerView.SetPosition(AdPosition.Top); 

- ,

0

, :

private BannerView banner;

// Gives the REAL pixel height of the banner ON THE PHONE
banner.GetHeightInPixels());

// Gives the REAL pixel width of the banner ON THE PHONE
banner.GetWidthInPixels());

GUI, :

float bannerHeight = banner.GetHeightInPixels() * canvasScaler.referenceResolution.y / Screen.height
float bannerWidth = banner.GetWidthInPixels() * canvasScaler.referenceResolution.x / Screen.width
0

, Admob Smart Banners:

public static float adHeight(){
    float f = Screen.dpi / 160f;
    float dp = Screen.height / f;
    return (dp>720f)? 90f * f
          :(dp>400f)? 50f * f
          :32f * f;
}

:

r.offsetMin = new Vector2(r.offsetMin.x, adHeight());

r - RectTransform Canvas/Panel.

loadBannerAd , .

0

Source: https://habr.com/ru/post/1569854/


All Articles