Skip to content

Instantly share code, notes, and snippets.

@0xF6
Last active November 2, 2020 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xF6/7f165cd0a38bd7c068d6019fcb353ddb to your computer and use it in GitHub Desktop.
Save 0xF6/7f165cd0a38bd7c068d6019fcb353ddb to your computer and use it in GitHub Desktop.
Apply a safe region calculation using classic ad banner.
/// <summary>
/// Only <see cref="BannerPosition.BOTTOM_*"/>
/// </summary>
public static void ApplyAdsSafeRegion(this RectTransform target, Rect safeRegion)
{
static float getAdHeight() =>
((Screen.height / (Screen.dpi / 160f)) > 720f) ? 90f * (Screen.dpi / 160f)
: ((Screen.height / (Screen.dpi / 160f)) > 400f) ? 50f * (Screen.dpi / 160f)
: 32f * (Screen.dpi / 160f);
r.y += getAdHeight();
r.height -= getAdHeight();
var anchorMin = r.position;
var anchorMax = r.position + r.size;
anchorMin.x /= Screen.width;
anchorMin.y /= Screen.height;
anchorMax.x /= Screen.width;
anchorMax.y /= Screen.height;
target.anchorMin = anchorMin;
target.anchorMax = anchorMax;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment