Unity 3D - Get Smart Banner size in pixels

4,658 views
Skip to first unread message

iulian dima

unread,
Feb 8, 2016, 3:29:53 PM2/8/16
to Google Mobile Ads SDK Developers
Could someone post a working script for calculating the Smart Banner size in pixels?
I want to place a banner at the bottom of the screen and rearrange my UI layout based on the banner's height.

I come up with the following code after reading the Admob guide and looking into other sources, but it's not giving the correct height when testing on some devices. 

    int CalculateBannerHeight() {
 
         
if (Screen.height <= 400*Screen.dpi/160) {
 
             
return Mathf.RoundToInt(32*Screen.dpi/160);
         
         
} else if (Screen.height <= 720*Screen.dpi/160) {
             
             
return Mathf.RoundToInt(50*Screen.dpi/160);
         
         
} else {
 
             
return Mathf.RoundToInt(90*Screen.dpi/160);
         
         
}
 
     
}  


Veer Arjun Busani

unread,
Feb 8, 2016, 5:26:54 PM2/8/16
to Google Mobile Ads SDK Developers
Hi Iulian,

Would you be able to send us more details, such as logs and the type of device in question? Also you could retry with the following snippet - 

int CalculateBannerHeight() {
    if (Screen.height <= 720 * (Screen.dpi / 160)) {
        if (Screen.height > 400 * (Screen.dpi / 160)) {
            return Mathf.RoundToInt (50 * (Screen.dpi / 160));
        } else if (Screen.height <= 400 * (Screen.dpi / 160)) {
            return Mathf.RoundToInt (32 * (Screen.dpi / 160));
        }
    } else {
        return Mathf.RoundToInt(90* (Screen.dpi/160));
    }
}  

Thanks,
Veer Arjun Busani
Mobile Ads SDK Team

iulian dima

unread,
Feb 10, 2016, 5:23:13 AM2/10/16
to Google Mobile Ads SDK Developers
Hi,

For example when making a test on iPad Air, using this code I get 148 pixels for the banner height ( the screen dpi is 264, resulting  Mathf.RoundToInt(90* 264/160) = 148) while the true height is 180 pixels, I got this value by making a screenshot and manually measuring it.

Thanks,
Iulian

Veer Arjun Busani

unread,
Feb 10, 2016, 12:57:53 PM2/10/16
to Google Mobile Ads SDK Developers
Hi Iulian.

If you simply want to know the height of a SMART_BANNER, you read more about Banner size considerations. Also the size mentioned there are already in density-pixel[DP]. Now coming to your point, while the formula is right, you forgot to consider the scale factor for retina devices. 

For iPad Air, the scale would be @2x. So now the DPI would be 160 * 2(scale) = 320 DPI. Now convert using the formula px = dp * (Screen.dpi/ 160). Banner height (px) = 90 * (320/160) = 180px. You can read more here.

Thanks ,

iulian dima

unread,
Feb 23, 2016, 4:26:12 AM2/23/16
to Google Mobile Ads SDK Developers
I was able to find the right way to calculate the banner's height. It seems that the screen dpi is considered as a multiple of 160, so in this case the script would be the following:

    int CalculateBannerHeight() {
        if (Screen.height <= 400*Mathf.RoundToInt(Screen.dpi/160)) {
            return 32*Mathf.RoundToInt(Screen.dpi/160);
        } else if (Screen.height <= 720*Mathf.RoundToInt(Screen.dpi/160)) {
            return 50*Mathf.RoundToInt(Screen.dpi/160);
        } else {
            return 90*Mathf.RoundToInt(Screen.dpi/160);
        }
    }

Cheers,
Iulian
Reply all
Reply to author
Forward
0 new messages