Space Ad Mobile 9.5.1
Space Ad Mobile makes ads easy to add into your mobile game: one unified workflow for LevelPlay, AdMob, Yandex.Ads across banners, interstitials and rewarded.
Loading...
Searching...
No Matches
YandexBannerSizeHelper.cs
1using UnityEngine;
2using YandexMobileAds;
3using YandexMobileAds.Base;
4
5namespace SCG.SpaceAdMobile
6{
12 internal static class YandexBannerSizeHelper
13 {
15 private const int AdaptiveMinHeightDp = 50;
16
18 private const float AdaptiveMaxScreenHeightMultiplier = 0.15f;
19
21 private const float AdaptiveBaselineHeightToWidthRatio = 50f / 320f;
22
24 private const float RuntimeSizeToleranceMultiplier = 1.1f;
25
33 public static BannerAdSize CreateRuntimeBannerSize(BannerOptions bannerOptions)
34 {
35 var width = GetBannerWidthDp(bannerOptions);
36#if !LEGACY_YANDEX_MOBILE_ADS
37 return bannerOptions.Type == BannerType.Inline
38 ? BannerAdSize.Inline(width, bannerOptions.InlineMaxHeight)
39 : BannerAdSize.Sticky(width);
40#else
41 return bannerOptions.Type == BannerType.Inline
42 ? BannerAdSize.InlineSize(width, bannerOptions.InlineMaxHeight)
43 : BannerAdSize.StickySize(width);
44#endif
45 }
46
56 public static float GetBannerHeightPixels(
57 BannerOptions bannerOptions,
58 BannerAdSize runtimeBannerSize,
59 float adaptiveReserveBias) =>
60 bannerOptions.Type == BannerType.Inline
61 ? GetEstimatedInlineHeightPixels(bannerOptions, adaptiveReserveBias)
62 : GetEstimatedStickyHeightPixels(bannerOptions, runtimeBannerSize, adaptiveReserveBias);
63
72 public static float GetBannerWidthPixels(BannerOptions bannerOptions, BannerAdSize runtimeBannerSize)
73 {
74 var runtimeWidth = GetRuntimeBannerWidthPixels(runtimeBannerSize);
75 return runtimeWidth > 0f
76 ? runtimeWidth
77 : GetConfiguredBannerWidthPixels(bannerOptions);
78 }
79
86 private static float GetScreenWidthPixels() => Screen.safeArea.width > 0f
87 ? Screen.safeArea.width
88 : Screen.width;
89
96 private static float GetScreenHeightPixels() => Screen.safeArea.height > 0f
97 ? Screen.safeArea.height
98 : Screen.height;
99
104 private static int GetScreenWidthDp() => ScreenUtils.ConvertPixelsToDp((int)GetScreenWidthPixels());
105
113 private static float ConvertDpToPixels(int dp)
114 {
115 if (dp <= 0)
116 return 0f;
117
118 var safeAreaWidthDp = GetScreenWidthDp();
119 return safeAreaWidthDp > 0
120 ? dp * (GetScreenWidthPixels() / safeAreaWidthDp)
121 : dp;
122 }
123
131 private static int GetBannerWidthDp(BannerOptions bannerOptions) => bannerOptions.Width < 0
132 ? GetScreenWidthDp()
133 : bannerOptions.Width;
134
142 private static float GetConfiguredBannerWidthPixels(BannerOptions bannerOptions) => bannerOptions.Width > 0
143 ? ConvertDpToPixels(bannerOptions.Width)
144 : GetScreenWidthPixels();
145
154 private static float ConvertRuntimeDimensionToPixels(int value, float maxPixels)
155 {
156 if (value <= 0 || maxPixels <= 0f)
157 return 0f;
158
159 var convertedValue = ConvertDpToPixels(value);
160 if (convertedValue <= maxPixels * RuntimeSizeToleranceMultiplier)
161 return convertedValue;
162
163 return value <= maxPixels * RuntimeSizeToleranceMultiplier
164 ? value
165 : 0f;
166 }
167
175 private static float GetRuntimeBannerWidthPixels(BannerAdSize bannerSize) =>
176 bannerSize is not { Width: > 0 }
177 ? 0f
178 : ConvertRuntimeDimensionToPixels(bannerSize.Width, GetScreenWidthPixels());
179
187 private static float GetRuntimeStickyHeightPixels(BannerAdSize bannerSize)
188 {
189 if (bannerSize is not { Height: > 0 })
190 return 0f;
191
192 var minHeight = GetAdaptiveMinHeightPixels();
193 var maxHeight = GetAdaptiveMaxHeightPixels();
194 var runtimeHeight = ConvertRuntimeDimensionToPixels(bannerSize.Height, maxHeight);
195
196 return runtimeHeight > 0f
197 ? ClampAdaptiveHeightPixels(runtimeHeight, minHeight, maxHeight)
198 : 0f;
199 }
200
207 private static float GetAdaptiveMinHeightPixels() => ConvertDpToPixels(AdaptiveMinHeightDp);
208
215 private static float GetAdaptiveMaxHeightPixels() =>
216 GetScreenHeightPixels() * AdaptiveMaxScreenHeightMultiplier;
217
225 private static float GetInlineReserveMaxHeightPixels(BannerOptions bannerOptions)
226 {
227 var screenMaxHeight = GetAdaptiveMaxHeightPixels();
228
229 var configuredMaxHeight = ConvertDpToPixels(bannerOptions.InlineMaxHeight);
230 return configuredMaxHeight > 0f
231 ? Mathf.Min(screenMaxHeight, configuredMaxHeight)
232 : screenMaxHeight;
233 }
234
244 private static float ClampAdaptiveHeightPixels(float height, float minHeight, float maxHeight) =>
245 maxHeight <= 0f ? Mathf.Max(0f, height) :
246 minHeight > maxHeight ? maxHeight : Mathf.Clamp(height, minHeight, maxHeight);
247
257 private static float EstimateAdaptiveHeightPixels(
258 BannerOptions bannerOptions,
259 float maxHeightPixels,
260 float adaptiveReserveBias)
261 {
262 var width = GetBannerWidthPixels(bannerOptions, null);
263 var minHeight = GetAdaptiveMinHeightPixels();
264 var widthBasedHeight = width * AdaptiveBaselineHeightToWidthRatio;
265 var reserveBias = Mathf.Clamp01(adaptiveReserveBias);
266 var biasedHeight = Mathf.Lerp(widthBasedHeight, maxHeightPixels, reserveBias);
267
268 return ClampAdaptiveHeightPixels(biasedHeight, minHeight, maxHeightPixels);
269 }
270
279 private static float GetEstimatedInlineHeightPixels(BannerOptions bannerOptions, float adaptiveReserveBias) =>
280 EstimateAdaptiveHeightPixels(
281 bannerOptions,
282 GetInlineReserveMaxHeightPixels(bannerOptions),
283 adaptiveReserveBias);
284
294 private static float GetEstimatedStickyHeightPixels(
295 BannerOptions bannerOptions,
296 BannerAdSize runtimeBannerSize,
297 float adaptiveReserveBias)
298 {
299 var runtimeHeight = GetRuntimeStickyHeightPixels(runtimeBannerSize);
300 return runtimeHeight > 0f
301 ? runtimeHeight
302 : EstimateAdaptiveHeightPixels(bannerOptions, GetAdaptiveMaxHeightPixels(), adaptiveReserveBias);
303 }
304 }
305}