Skip to content

Instantly share code, notes, and snippets.

@pedro15
Created March 31, 2025 18:29
Show Gist options
  • Save pedro15/d1cc66e44703a85be49d396342bb7b0d to your computer and use it in GitHub Desktop.
Save pedro15/d1cc66e44703a85be49d396342bb7b0d to your computer and use it in GitHub Desktop.
MaxSdk Mrec RectTransform Unity
/// <summary>
/// Provides utilities to position mrec ads on rect transform areas
/// </summary>
public static class MaxSdkMrecUtils
{
private static readonly Vector2 BASE_SCREEN_SIZE = new Vector2(1242, 2688);
private static readonly Vector2 BASE_AD_SCREEN_SIZE = new Vector2(720, 1080);
private static readonly Vector2 AD_AREA_SIZE = new Vector2(300f, 250f);
public static Vector2 GetMrecTarget(RectTransform _target)
{
float notch_size = Screen.height - (Screen.safeArea.y + Screen.safeArea.height);
float screen_density = MaxSdkUtils.GetScreenDensity();
Vector2 screen_point = RectTransformUtility.WorldToScreenPoint(Camera.main, _target.position);
screen_point.y = Screen.height - screen_point.y;
screen_point.y -= notch_size;
screen_point.x /= screen_density;
screen_point.y /= screen_density;
screen_point.x -= AD_AREA_SIZE.x / 2f;
screen_point.y -= AD_AREA_SIZE.y / 2f;
return screen_point;
}
public static float GetMrecAreaHeight(RectTransform _target)
{
float ad_screen_ratio = Screen.height / BASE_AD_SCREEN_SIZE.y;
float ad_desired_size = ad_screen_ratio * (AD_AREA_SIZE.y + 120);
return ad_desired_size * (BASE_SCREEN_SIZE.y / Screen.height);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment