|
| 1 | +// Copyright (C) 2026 Google, LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using System; |
| 16 | +using UnityEngine; |
| 17 | + |
| 18 | +using GoogleMobileAds.Api; |
| 19 | +using GoogleMobileAds.Common; |
| 20 | + |
| 21 | +namespace GoogleMobileAds.Android |
| 22 | +{ |
| 23 | + public class NextGenNativeOverlayAdClient : AndroidJavaProxy, INativeOverlayAdClient |
| 24 | + { |
| 25 | + private AndroidJavaObject nativeOverlayAd; |
| 26 | + |
| 27 | + public NextGenNativeOverlayAdClient() : base(NextGenUtils.UnityNativeTemplateAdCallbackClassName) |
| 28 | + { |
| 29 | + AndroidJavaClass playerClass = new AndroidJavaClass(Utils.UnityActivityClassName); |
| 30 | + AndroidJavaObject activity = playerClass.GetStatic<AndroidJavaObject>("currentActivity"); |
| 31 | + this.nativeOverlayAd = |
| 32 | + new AndroidJavaObject(NextGenUtils.UnityNativeTemplateAdClassName, activity, this); |
| 33 | + } |
| 34 | + |
| 35 | +#region INativeOverlayAdClient implementation |
| 36 | + // Ad event fired when the native ad has loaded. |
| 37 | + public event EventHandler<EventArgs> OnAdLoaded; |
| 38 | + // Ad event fired when the native ad has failed to load. |
| 39 | + public event EventHandler<LoadAdErrorClientEventArgs> OnAdFailedToLoad; |
| 40 | + // Ad event fired when an ad impression has been recorded. |
| 41 | + public event EventHandler<EventArgs> OnAdDidRecordImpression; |
| 42 | + // Ad event fired when the full screen content has been presented. |
| 43 | + public event EventHandler<EventArgs> OnAdDidPresentFullScreenContent; |
| 44 | + // Ad event fired when the full screen content has been dismissed. |
| 45 | + public event EventHandler<EventArgs> OnAdDidDismissFullScreenContent; |
| 46 | + // Ad event fired when an ad has been clicked. |
| 47 | + public event Action OnAdClicked; |
| 48 | + |
| 49 | + public event Action<AdValue> OnPaidEvent; |
| 50 | + |
| 51 | + // A long integer provided by the AdMob UI for the configured placement. |
| 52 | + public long PlacementId |
| 53 | + { |
| 54 | + get |
| 55 | + { |
| 56 | + return this.nativeOverlayAd.Call<long>("getPlacementId"); |
| 57 | + } |
| 58 | + set |
| 59 | + { |
| 60 | + this.nativeOverlayAd.Call("setPlacementId", value); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Loads a native ad |
| 65 | + public void Load(string adUnitId, AdRequest request, NativeAdOptions options) |
| 66 | + { |
| 67 | + AndroidJavaObject nativeAdOptionsJava = new AndroidJavaObject("com.google.unity.ads.nextgen.NativeAdOptions"); |
| 68 | + nativeAdOptionsJava.Call("setMediaAspectRatio", (int)options.MediaAspectRatio); |
| 69 | + nativeAdOptionsJava.Call("setAdChoicesPlacement", (int)options.AdChoicesPlacement); |
| 70 | + |
| 71 | + if (options.VideoOptions != null) |
| 72 | + { |
| 73 | + AndroidJavaObject videoOptionsBuilder = new AndroidJavaObject("com.google.android.libraries.ads.mobile.sdk.common.VideoOptions$Builder"); |
| 74 | + videoOptionsBuilder.Call<AndroidJavaObject>("setStartMuted", (bool)options.VideoOptions.StartMuted); |
| 75 | + videoOptionsBuilder.Call<AndroidJavaObject>("setCustomControlsRequested", (bool)options.VideoOptions.CustomControlsRequested); |
| 76 | + videoOptionsBuilder.Call<AndroidJavaObject>("setClickToExpandRequested", (bool)options.VideoOptions.ClickToExpandRequested); |
| 77 | + AndroidJavaObject videoOptionsJava = videoOptionsBuilder.Call<AndroidJavaObject>("build"); |
| 78 | + nativeAdOptionsJava.Call("setVideoOptions", videoOptionsJava); |
| 79 | + } |
| 80 | + |
| 81 | + this.nativeOverlayAd.Call("loadAd", adUnitId, nativeAdOptionsJava, |
| 82 | + NextGenUtils.GetAdRequestJavaObject(request, adUnitId)); |
| 83 | + } |
| 84 | + |
| 85 | + // Hides the native overlay from the screen. |
| 86 | + public void Hide() |
| 87 | + { |
| 88 | + this.nativeOverlayAd.Call("hide"); |
| 89 | + } |
| 90 | + |
| 91 | + // Shows the Native overlay on the screen. |
| 92 | + public void Show() |
| 93 | + { |
| 94 | + this.nativeOverlayAd.Call("show"); |
| 95 | + } |
| 96 | + |
| 97 | + // Set the position of the Native overlay using standard position. |
| 98 | + public void SetPosition(AdPosition position) |
| 99 | + { |
| 100 | + this.nativeOverlayAd.Call("setPositionCode", (int)position); |
| 101 | + } |
| 102 | + |
| 103 | + // Set the position of the Native overlay using custom position. |
| 104 | + public void SetPosition(int x, int y) |
| 105 | + { |
| 106 | + this.nativeOverlayAd.Call("setPosition", x, y); |
| 107 | + } |
| 108 | + |
| 109 | + // Renders the Native overlay ad on the screen using the provided style, size and |
| 110 | + // AdPosition. |
| 111 | + public void Render(NativeTemplateStyle templateViewStyle, AdSize adSize, |
| 112 | + AdPosition adPosition) |
| 113 | + { |
| 114 | + this.nativeOverlayAd.Call("renderCustomSizeAtPositionCode", |
| 115 | + GetNativeTemplateStyleJavaObject(templateViewStyle), |
| 116 | + NextGenUtils.GetAdSizeJavaObject(adSize), (int)adPosition); |
| 117 | + } |
| 118 | + |
| 119 | + // Renders the Native overlay ad on the screen using the provided style, size and |
| 120 | + // coordinates. |
| 121 | + public void Render(NativeTemplateStyle templateViewStyle, AdSize adSize, int x, int y) |
| 122 | + { |
| 123 | + this.nativeOverlayAd.Call("renderCustomSizeAtPosition", |
| 124 | + GetNativeTemplateStyleJavaObject(templateViewStyle), |
| 125 | + NextGenUtils.GetAdSizeJavaObject(adSize), x, y); |
| 126 | + } |
| 127 | + |
| 128 | + // Renders the Native overlay ad on the screen using default size at preset position. |
| 129 | + public void Render(NativeTemplateStyle templateViewStyle, AdPosition adPosition) |
| 130 | + { |
| 131 | + this.nativeOverlayAd.Call("renderDefaultSizeAtPositionCode", |
| 132 | + GetNativeTemplateStyleJavaObject(templateViewStyle), |
| 133 | + (int)adPosition); |
| 134 | + } |
| 135 | + |
| 136 | + // Renders the Native overlay ad on the screen using default size at (x,y) coordinates. |
| 137 | + public void Render(NativeTemplateStyle templateViewStyle, int x, int y) |
| 138 | + { |
| 139 | + this.nativeOverlayAd.Call("renderDefaultSizeAtPosition", |
| 140 | + GetNativeTemplateStyleJavaObject(templateViewStyle), x, |
| 141 | + y); |
| 142 | + } |
| 143 | + |
| 144 | + // Destroys the native ad. |
| 145 | + public void DestroyAd() |
| 146 | + { |
| 147 | + this.nativeOverlayAd.Call("destroy"); |
| 148 | + } |
| 149 | + |
| 150 | + // Returns ad request Response info client. |
| 151 | + public IResponseInfoClient GetResponseInfoClient() |
| 152 | + { |
| 153 | + var responseInfoJavaObject = nativeOverlayAd.Call<AndroidJavaObject>("getResponseInfo"); |
| 154 | + return new NextGenResponseInfoClient(responseInfoJavaObject); |
| 155 | + } |
| 156 | + |
| 157 | + // Returns the height of the NativeTemplateView in pixels. |
| 158 | + public float GetHeightInPixels() |
| 159 | + { |
| 160 | + return this.nativeOverlayAd.Call<float>("getHeightInPixels"); |
| 161 | + } |
| 162 | + |
| 163 | + // Returns the width of the NativeTemplateView in pixels. |
| 164 | + public float GetWidthInPixels() |
| 165 | + { |
| 166 | + return this.nativeOverlayAd.Call<float>("getWidthInPixels"); |
| 167 | + } |
| 168 | + |
| 169 | +#endregion |
| 170 | + |
| 171 | +#region Callbacks from UnityNativeTemplateAdCallbackNextgen. |
| 172 | + void onNativeAdLoaded() |
| 173 | + { |
| 174 | + if (this.OnAdLoaded != null) |
| 175 | + { |
| 176 | + this.OnAdLoaded(this, EventArgs.Empty); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + void onNativeAdFailedToLoad(AndroidJavaObject error) |
| 181 | + { |
| 182 | + if (this.OnAdFailedToLoad != null) |
| 183 | + { |
| 184 | + LoadAdErrorClientEventArgs args = new LoadAdErrorClientEventArgs() |
| 185 | + { |
| 186 | + LoadAdErrorClient = new NextGenLoadAdErrorClient(error) |
| 187 | + }; |
| 188 | + this.OnAdFailedToLoad(this, args); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + void onAdImpression() |
| 193 | + { |
| 194 | + if (this.OnAdDidRecordImpression != null) |
| 195 | + { |
| 196 | + this.OnAdDidRecordImpression(this, EventArgs.Empty); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + void onAdClicked() |
| 201 | + { |
| 202 | + if (this.OnAdClicked != null) |
| 203 | + { |
| 204 | + this.OnAdClicked(); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + void onAdShowedFullScreenContent() |
| 209 | + { |
| 210 | + if (this.OnAdDidPresentFullScreenContent != null) |
| 211 | + { |
| 212 | + this.OnAdDidPresentFullScreenContent(this, EventArgs.Empty); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + void onAdDismissedFullScreenContent() |
| 217 | + { |
| 218 | + if (this.OnAdDidDismissFullScreenContent != null) |
| 219 | + { |
| 220 | + this.OnAdDidDismissFullScreenContent(this, EventArgs.Empty); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + void onAdFailedToShowFullScreenContent(AndroidJavaObject error) |
| 225 | + { |
| 226 | + // No-op |
| 227 | + } |
| 228 | + |
| 229 | + void onPaidEvent(int precision, long valueInMicros, string currencyCode) |
| 230 | + { |
| 231 | + if (this.OnPaidEvent != null) |
| 232 | + { |
| 233 | + AdValue adValue = new AdValue() |
| 234 | + { |
| 235 | + Precision = (AdValue.PrecisionType)precision, |
| 236 | + Value = valueInMicros, |
| 237 | + CurrencyCode = currencyCode |
| 238 | + }; |
| 239 | + this.OnPaidEvent(adValue); |
| 240 | + } |
| 241 | + } |
| 242 | +#endregion |
| 243 | + |
| 244 | +#region Native Template Styling Utilities |
| 245 | + |
| 246 | + private AndroidJavaObject GetNativeTemplateStyleJavaObject(NativeTemplateStyle tmplStyle) |
| 247 | + { |
| 248 | + AndroidJavaClass nativeTemplateTypeClass = |
| 249 | + new AndroidJavaClass(Utils.UnityNativeTemplateTypeClassName); |
| 250 | + AndroidJavaObject nativeTemplateType = null; |
| 251 | + if (tmplStyle.TemplateId == "small") |
| 252 | + { |
| 253 | + nativeTemplateType = |
| 254 | + nativeTemplateTypeClass.CallStatic<AndroidJavaObject>("fromIntValue", 0); |
| 255 | + } |
| 256 | + else |
| 257 | + { |
| 258 | + nativeTemplateType = |
| 259 | + nativeTemplateTypeClass.CallStatic<AndroidJavaObject>("fromIntValue", 1); |
| 260 | + } |
| 261 | + |
| 262 | + AndroidJavaObject mainBgColor = null; |
| 263 | + if (!tmplStyle.MainBackgroundColor.Equals(Color.clear)) |
| 264 | + { |
| 265 | + AndroidJavaClass colorClass = new AndroidJavaClass(Utils.ColorClassName); |
| 266 | + int color = colorClass.CallStatic<int>( |
| 267 | + "argb", (int)(255 * tmplStyle.MainBackgroundColor.a), |
| 268 | + (int)(255 * tmplStyle.MainBackgroundColor.r), |
| 269 | + (int)(255 * tmplStyle.MainBackgroundColor.g), |
| 270 | + (int)(255 * tmplStyle.MainBackgroundColor.b)); |
| 271 | + mainBgColor = new AndroidJavaObject(Utils.ColorDrawableClassName, color); |
| 272 | + } |
| 273 | + |
| 274 | + AndroidJavaObject primaryTextStyle = null; |
| 275 | + if (tmplStyle.PrimaryText != null) |
| 276 | + { |
| 277 | + primaryTextStyle = GetNativeTemplateTextStyleJavaObject(tmplStyle.PrimaryText); |
| 278 | + } |
| 279 | + |
| 280 | + AndroidJavaObject secondaryTextStyle = null; |
| 281 | + if (tmplStyle.SecondaryText != null) |
| 282 | + { |
| 283 | + secondaryTextStyle = GetNativeTemplateTextStyleJavaObject(tmplStyle.SecondaryText); |
| 284 | + } |
| 285 | + |
| 286 | + AndroidJavaObject tertiaryTextStyle = null; |
| 287 | + if (tmplStyle.TertiaryText != null) |
| 288 | + { |
| 289 | + tertiaryTextStyle = GetNativeTemplateTextStyleJavaObject(tmplStyle.TertiaryText); |
| 290 | + } |
| 291 | + |
| 292 | + AndroidJavaObject c2aTextStyle = null; |
| 293 | + if (tmplStyle.CallToActionText != null) |
| 294 | + { |
| 295 | + c2aTextStyle = GetNativeTemplateTextStyleJavaObject(tmplStyle.CallToActionText); |
| 296 | + } |
| 297 | + |
| 298 | + AndroidJavaObject nativeAdTemplateStyle = new AndroidJavaObject( |
| 299 | + Utils.UnityNativeTemplateStyleClassName, nativeTemplateType, mainBgColor, |
| 300 | + c2aTextStyle, primaryTextStyle, secondaryTextStyle, tertiaryTextStyle); |
| 301 | + |
| 302 | + return nativeAdTemplateStyle; |
| 303 | + } |
| 304 | + |
| 305 | + private AndroidJavaObject GetNativeTemplateTextStyleJavaObject(NativeTemplateTextStyle text) |
| 306 | + { |
| 307 | + AndroidJavaClass colorClass = new AndroidJavaClass(Utils.ColorClassName); |
| 308 | + AndroidJavaObject textColorDrawable = null; |
| 309 | + AndroidJavaObject bgColorDrawable = null; |
| 310 | + |
| 311 | + if (!text.TextColor.Equals(Color.clear)) |
| 312 | + { |
| 313 | + int color = colorClass.CallStatic<int>( |
| 314 | + "argb", (int)(255 * text.TextColor.a), (int)(255 * text.TextColor.r), |
| 315 | + (int)(255 * text.TextColor.g), (int)(255 * text.TextColor.b)); |
| 316 | + textColorDrawable = new AndroidJavaObject(Utils.ColorDrawableClassName, color); |
| 317 | + } |
| 318 | + |
| 319 | + if (!text.BackgroundColor.Equals(Color.clear)) |
| 320 | + { |
| 321 | + int bgColor = colorClass.CallStatic<int>( |
| 322 | + "argb", (int)(255 * text.BackgroundColor.a), (int)(255 * text.BackgroundColor.r), |
| 323 | + (int)(255 * text.BackgroundColor.g), (int)(255 * text.BackgroundColor.b)); |
| 324 | + bgColorDrawable = new AndroidJavaObject(Utils.ColorDrawableClassName, bgColor); |
| 325 | + } |
| 326 | + |
| 327 | + AndroidJavaClass fontStyleClass = |
| 328 | + new AndroidJavaClass(Utils.UnityNativeTemplateFontStyleClassName); |
| 329 | + AndroidJavaObject fontStyle = fontStyleClass.CallStatic<AndroidJavaObject>( |
| 330 | + "fromIntValue", (int)text.Style); |
| 331 | + |
| 332 | + AndroidJavaObject fontSize = |
| 333 | + new AndroidJavaObject(Utils.DoubleClassName, (double)text.FontSize); |
| 334 | + |
| 335 | + AndroidJavaObject templateTextStyle = |
| 336 | + new AndroidJavaObject(Utils.UnityNativeTemplateTextStyleClassName, |
| 337 | + textColorDrawable, bgColorDrawable, fontStyle, fontSize); |
| 338 | + return templateTextStyle; |
| 339 | + } |
| 340 | +#endregion |
| 341 | + } |
| 342 | +} |
0 commit comments