AppLovin / AppLovin-MAX-Unity-Plugin

130 stars 35 forks source link

Banner Ad height is not correct in all device #271

Closed cqtuan2011 closed 1 year ago

cqtuan2011 commented 1 year ago

MAX Plugin Version

5.10.1

Unity Version

2021.3.11f1

Device/Platform Info

Samsung Galaxy S20 FE, android version 13

Current Behavior

Hi AppLovin team,

I have a design which is placing a close button on the top right corner of the banner ad.

First, I get the height in pixels by using the code below, then apply it to the height of the background image but the result is not correct in every device.

I got the desired result on Redmi Note 10S, but when on the Samsung S20 FE, it was shorter than expected and the close button couldn't be displayed properly. Please see the attached images.

Screen Shot 2023-07-24 at 16 50 42

Expected Behavior

The close button lays above banner ad

Screen Shot 2023-07-24 at 16 57 32

How to Reproduce

public float GetHeightInPixels() { var heightDp = MaxSdkUtils.GetAdaptiveBannerHeight(); var density = MaxSdkUtils.GetScreenDensity(); var heightPx = heightDp * density; return heightPx; }

Additional Info

No response

JonathanLiuApp commented 1 year ago

Hi @cqtuan2011, this issue occurs when your Canvas Scaler’s UI Scale Mode is set to Scale with Screen Size instead of Constant Pixel Size. Because the ads are not rendered using Unity’s Canvas, the mapping of pixels won’t be 1:1. To resolve this, if you still want to use Scale with Screen Size, you can calculate the true pixel height required for the banner by determining the screen height percentage it should occupy and then converting it to a pixel height based on your canvas size. This should ensure consistency across different screen resolutions. You can use the following code for the calculations:

public RectTransform canvasTransform;

public float GetHeightInPixelsUsingPercent() { // Get the height of the banner in pixels var bannerHeight = MaxSdkUtils.GetAdaptiveBannerHeight(); var density = MaxSdkUtils.GetScreenDensity(); var heightPixels = bannerHeight * density;

// Calculate the height of the banner in terms of percentage of the screen var percentHeight = heightPixels / Screen.height;

// Use the canvas transform to determine the real pixel size needed to match the banner height var truePixelSize = percentHeight * canvasTransform.sizeDelta.y; return truePixelSize; }