锚定自适应横幅广告
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
自适应横幅广告是新一代自适应广告,可针对每台设备优化广告尺寸,从而最大限度地提升广告效果。自适应横幅广告在智能横幅广告(仅支持固定高度)的基础上进行了改进,您可以指定广告宽度,进而确定最佳广告尺寸。
为了选择最佳广告尺寸,自适应横幅广告使用的是固定宽高比,而非固定高度。因此,这种横幅广告在不同设备上占据的屏幕区域更为一致,也带来了提升广告效果的机会。
请注意,使用自适应横幅广告时,如果给定设备类型和宽度,则返回的广告尺寸将始终固定不变。在给定设备上测试布局后,便可以确定广告尺寸不会发生变化。不过,横幅广告素材的尺寸在不同设备上可能会有所不同。因此,建议您使广告布局能够适应各种广告高度。在极少数情况下,广告素材可能填不满整个自适应广告单元的尺寸,此时会改而展示一个标准尺寸的广告素材,并在广告位中居中显示它。
前提条件
自适应横幅广告适用情形
自适应横幅广告旨在直接取代符合行业标准的 320x50 横幅广告尺寸及智能横幅广告格式。
这些横幅广告尺寸常用于锚定横幅广告,锚定横幅广告通常固定在屏幕的顶部或底部展示。
对于此类锚定横幅广告,当使用自适应横幅广告时,宽高比与标准 320x50 广告大致相当,如以下三个示例所示:

320x50 横幅广告
|

自适应横幅广告
|

智能横幅广告
|
自适应横幅广告可更好地利用可用的屏幕尺寸。此外,与智能横幅广告相比,自适应横幅广告是一种更好的选择,原因如下:
广告植入注意事项
在应用中植入自适应横幅广告时,请注意以下几点:
- 您必须知道要展示广告的视图的宽度,并且应在设置视图宽度时考虑到设备宽度,以及任何适用的安全区域或刘海屏。
- 当较小尺寸的广告不能完全填充广告位时,请务必将广告视图背景设为不透明,以符合 AdMob 政策。
- 确保您使用的是最新版本的 Google 移动广告 Unity 插件。对于中介,请使用最新版本的中介适配器。
- 按照自适应横幅广告尺寸的设计,占满可用宽度时效果最佳。在大多数情况下,这指的是所用设备的屏幕全宽。请务必考虑适用的安全区域。
- 使用自适应 AdSize API 时,Google 移动广告 SDK 会根据给定的宽度使用经优化的广告高度来调整横幅广告的大小。
- 获取自适应广告尺寸的方法有以下三种:
AdSize.GetLandscapeAnchoredAdaptiveBannerAdSizeWithWidth
(适用于横向屏幕)、AdSize.GetPortraitAnchoredAdaptiveBannerAdSizeWithWidth
(适用于纵向屏幕)和 AdSize.GetCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth
(适用于执行操作时的屏幕方向)。
- 在给定设备上针对给定宽度返回的广告尺寸始终相同,因此在给定设备上测试布局后,您可以确定广告尺寸不会发生变化。
- 锚定横幅广告的高度始终不会超过设备高度的 15% 或 90 密度无关像素(以二者中的较小者为准),且始终不会小于 50 密度无关像素。
- 对于全宽横幅广告,您可以使用
AdSize.FullWidth
标记,而不是提供特定宽度。
快速入门
要植入简单的自适应锚定横幅广告,请按照以下步骤操作:
- 获取自适应横幅广告尺寸。您获取的尺寸将用于请求自适应横幅广告。要获取自适应广告尺寸,请务必执行以下操作:
- 获取所用设备的宽度(以密度无关像素为单位),或者自行设置宽度(如果您不想使用屏幕的全宽)。由于 Unity API(例如
Screen.width
)将返回绝对像素大小,因此您需要将其除以设备比例因子(可通过 MobileAds.Utils.GetDeviceScale()
获取)。
- 对于全宽横幅广告,请使用
AdSize.FullWidth
标记。
- 针对广告尺寸类使用相应的静态方法(例如
AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(width)
),以获取当前屏幕方向的自适应 AdSize
对象。
- 使用广告单元 ID、自适应广告尺寸以及广告的相关位置创建
BannerView
对象。
- 在预先准备的广告视图中,使用
LoadAd()
方法创建广告请求对象并加载横幅广告,其处理方式与常规横幅广告请求一样。
代码示例
以下的示例脚本会加载及重新加载自适应横幅广告以适应屏幕宽度,且考虑了安全区域:
using UnityEngine;
using System;
using GoogleMobileAds.Api;
public class AdaptiveBannerSample : MonoBehaviour
{
private BannerView _bannerView;
// Use this for initialization
void Start()
{
// Set your test devices.
// https://developers.google.com/admob/unity/test-ads
RequestConfiguration requestConfiguration = new RequestConfiguration
{
TestDeviceIds = new List<string>
{
AdRequest.TestDeviceSimulator,
// Add your test device IDs (replace with your own device IDs).
#if UNITY_IPHONE
"96e23e80653bb28980d3f40beb58915c"
#elif UNITY_ANDROID
"75EF8D155528C04DACBBA6F36F433035"
#endif
}
};
MobileAds.SetRequestConfiguration(requestConfiguration);
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize((InitializationStatus status) =>
{
RequestBanner();
});
}
public void OnGUI()
{
GUI.skin.label.fontSize = 60;
Rect textOutputRect = new Rect(
0.15f * Screen.width,
0.25f * Screen.height,
0.7f * Screen.width,
0.3f * Screen.height);
GUI.Label(textOutputRect, "Adaptive Banner Example");
}
private void RequestBanner()
{
// These ad units are configured to always serve test ads.
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-3212738706492790/6113697308";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-3212738706492790/5381898163";
#else
string adUnitId = "unexpected_platform";
#endif
// Clean up banner ad before creating a new one.
if (_bannerView != null)
{
_bannerView.Destroy();
}
AdSize adaptiveSize =
AdSize.GetCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(AdSize.FullWidth);
_bannerView = new BannerView(adUnitId, adaptiveSize, AdPosition.Bottom);
// Register for ad events.
_bannerView.OnBannerAdLoaded += OnBannerAdLoaded;
_bannerView.OnBannerAdLoadFailed += OnBannerAdLoadFailed;
AdRequest adRequest = new AdRequest();
// Load a banner ad.
_bannerView.LoadAd(adRequest);
}
#region Banner callback handlers
private void OnBannerAdLoaded(object sender, EventArgs args)
{
Debug.Log("Banner view loaded an ad with response : "
+ _bannerView.GetResponseInfo());
Debug.Log("Ad Height: {0}, width: {1}",
_bannerView.GetHeightInPixels(),
_bannerView.GetWidthInPixels());
}
private void OnBannerAdLoadFailed(LoadAdError error)
{
Debug.LogError("Banner view failed to load an ad with error : "
+ error);
}
#endregion
}
在本示例中,函数 AdSize.GetCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth
用于获取适用于当前界面方向的锚定位置横幅广告的尺寸。要按给定屏幕方向预加载锚定横幅广告,请使用 AdSize.GetPortraitAnchoredAdaptiveBannerAdSizeWithWidth
和 AdSize.GetLandscapeAnchoredAdaptiveBannerAdSizeWithWidth
中的相关函数。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-18。
[null,null,["最后更新时间 (UTC):2025-08-18。"],[[["\u003cp\u003eAdaptive banners optimize ad size for each device, maximizing performance by using fixed aspect ratios instead of fixed heights.\u003c/p\u003e\n"],["\u003cp\u003eThey are drop-in replacements for standard 320x50 banners and smart banners, offering better screen space utilization and accounting for safe areas and cutouts.\u003c/p\u003e\n"],["\u003cp\u003eAdaptive banners dynamically adjust height to the device, providing a more consistent ad experience while requiring developers to accommodate height variances in their layout.\u003c/p\u003e\n"],["\u003cp\u003eImplementation involves getting the device width, using the appropriate AdSize method for orientation, and creating a BannerView with the adaptive size and ad unit ID.\u003c/p\u003e\n"]]],["Adaptive banners optimize ad size per device using fixed aspect ratios instead of fixed heights, improving performance and screen space usage. Key actions include: determining the view's width (considering device width, safe areas, and cutouts), and selecting the appropriate `AdSize` method (`GetLandscapeAnchoredAdaptiveBannerAdSizeWithWidth`, `GetPortraitAnchoredAdaptiveBannerAdSizeWithWidth`, or `GetCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth`). Once loaded, the ad's size remains constant for a given device and width. You can also use `AdSize.FullWidth` flag for full width banners.\n"],null,["# Anchored adaptive banners\n\nAdaptive banners are the next generation of responsive ads, maximizing\nperformance by optimizing ad size for each device. Improving on smart banners,\nwhich supported only fixed heights, adaptive banners let you specify the\nad width and use this to determine the optimal ad size.\n\nTo choose the best ad size, adaptive banners use fixed aspect ratios instead of\nfixed heights. This results in banner ads that occupy a more consistent portion\nof the screen across devices and provides opportunities for improved\nperformance.\n\nWhen working with adaptive banners, note that they will always return a\nconstant size for a given device and width. Once you've tested your layout on a\ngiven device, you can be sure that the ad size will not change. However, the\nsize of the banner creative may change across different devices. Consequently,\nwe recommend that your layout accommodate variances in ad height. In rare cases,\nthe full adaptive size may not be filled and a standard size creative will be\ncentered in this slot instead.\n\nPrerequisites\n-------------\n\n- Follow the instructions from the [Get Started guide](/admob/unity/quick-start) on how to [Import the Mobile Ads Unity plugin](/admob/unity/quick-start#import_the_mobile_ads_unity_plugin) and [Include the Mobile Ads SDK](/admob/unity/quick-start#include_the_mobile_ads_sdk).\n\nWhen to use adaptive banners\n----------------------------\n\nAdaptive banners are designed to be a drop-in replacement for both the industry\nstandard 320x50 banner size and the smart banner format which they supersede.\n\nThese banner sizes are commonly used as anchored banners, which are usually\nlocked to the top or bottom of the screen.\n\nFor such anchored banners, the aspect ratio when using adaptive banners will be\nsimilar to that of a standard 320x50 ad, as can be seen in the three examples\nbelow:\n\n|---------------|-----------------|--------------|\n| 320x50 banner | Adaptive banner | Smart banner |\n\nThe adaptive banner makes better use of the available screen size. Additionally,\ncompared to a smart banner, an adaptive banner is a better choice because:\n\n- It uses any width you provide, rather than forcing the width to be full\n screen, enabling you to account for the safe area on iOS, and display cutouts\n on Android.\n\n- It selects an optimal height for the specific device, rather than having a\n constant height across different size devices, mitigating the effects of\n device fragmentation.\n\nImplementation notes\n--------------------\n\nWhen implementing adaptive banners in your app, keep the following points in mind:\n\n- You must know the width of the view that the ad will be placed in, **and this should take into account the device width and any safe areas or\n cutouts that are applicable**.\n- Ensure that your ad view background is opaque to be compliant with AdMob policies when smaller ad sizes serve that do not fill the ad slot.\n- Ensure you are using the latest version of the Google Mobile Ads Unity plugin. For mediation, use the latest version of each mediation adapter.\n- The adaptive banner sizes are designed to work best when using the full available width. In most cases, this will be the full width of the screen of the device in use. Be sure to take into account applicable safe areas.\n- The Google Mobile Ads SDK will size the banner with an optimized ad height for the given width when using the adaptive AdSize APIs.\n- There are three methods to get an ad size for adaptive: `AdSize.GetLandscapeAnchoredAdaptiveBannerAdSizeWithWidth` for landscape, `AdSize.GetPortraitAnchoredAdaptiveBannerAdSizeWithWidth` for portrait, and `AdSize.GetCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth` for the current orientation at the time of execution.\n- The size returned for a given width on a given device will always be the same, hence once you've tested your layout on a given device, you can be sure that the ad size will not change.\n- Anchored banner height is never larger than the lesser of 15% of the device's height or 90 density independent pixels and never smaller than 50 density independent pixels.\n- For full width banners, you can use the `AdSize.FullWidth` flag instead of supplying a specific width.\n\nQuickstart\n----------\n\nFollow the steps below to implement a simple adaptive anchor banner.\n\n1. **Get an adaptive banner ad size.** The size you get will be used to request your adaptive banner. To get the adaptive ad size, make sure that you:\n 1. Get the width of the device in use in density independent pixels, or set your own width if you don't want to use the full width of the screen. Since Unity APIs such as `Screen.width` return absolute pixels, you need to divide by device scale (retrievable from `MobileAds.Utils.GetDeviceScale()`).\n 2. Use the `AdSize.FullWidth` flag for full width banners.\n 3. Use the appropriate static methods on the ad size class, such as `AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(width)` to get an adaptive `AdSize` object for the current orientation.\n2. Create a `BannerView` object with your ad unit ID, the adaptive size, and the relevant position for your ad.\n3. Create an ad request object and load your banner using the `LoadAd()` method on your prepared ad view, just like you would with a normal banner request.\n\nCode example\n------------\n\nHere's an example script that loads and reloads an adaptive banner to fit the\nwidth of the screen, taking into account safe area: \n\n using UnityEngine;\n using System;\n using GoogleMobileAds.Api;\n\n public class AdaptiveBannerSample : MonoBehaviour\n {\n private BannerView _bannerView;\n\n // Use this for initialization\n void Start()\n {\n // Set your test devices.\n // https://developers.google.com/admob/unity/test-ads\n RequestConfiguration requestConfiguration = new RequestConfiguration\n {\n TestDeviceIds = new List\u003cstring\u003e\n {\n AdRequest.TestDeviceSimulator,\n // Add your test device IDs (replace with your own device IDs).\n #if UNITY_IPHONE\n \"96e23e80653bb28980d3f40beb58915c\"\n #elif UNITY_ANDROID\n \"75EF8D155528C04DACBBA6F36F433035\"\n #endif\n }\n };\n MobileAds.SetRequestConfiguration(requestConfiguration);\n\n // Initialize the Google Mobile Ads SDK.\n MobileAds.Initialize((InitializationStatus status) =\u003e\n {\n RequestBanner();\n });\n }\n\n public void OnGUI()\n {\n GUI.skin.label.fontSize = 60;\n Rect textOutputRect = new Rect(\n 0.15f * Screen.width,\n 0.25f * Screen.height,\n 0.7f * Screen.width,\n 0.3f * Screen.height);\n GUI.Label(textOutputRect, \"Adaptive Banner Example\");\n }\n\n private void RequestBanner()\n {\n // These ad units are configured to always serve test ads.\n #if UNITY_EDITOR\n string adUnitId = \"unused\";\n #elif UNITY_ANDROID\n string adUnitId = \"ca-app-pub-3212738706492790/6113697308\";\n #elif UNITY_IPHONE\n string adUnitId = \"ca-app-pub-3212738706492790/5381898163\";\n #else\n string adUnitId = \"unexpected_platform\";\n #endif\n\n // Clean up banner ad before creating a new one.\n if (_bannerView != null)\n {\n _bannerView.Destroy();\n }\n\n AdSize adaptiveSize =\n AdSize.GetCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(AdSize.FullWidth);\n\n _bannerView = new BannerView(adUnitId, adaptiveSize, AdPosition.Bottom);\n\n // Register for ad events.\n _bannerView.OnBannerAdLoaded += OnBannerAdLoaded;\n _bannerView.OnBannerAdLoadFailed += OnBannerAdLoadFailed;\n\n AdRequest adRequest = new AdRequest();\n\n // Load a banner ad.\n _bannerView.LoadAd(adRequest);\n }\n\n #region Banner callback handlers\n\n private void OnBannerAdLoaded(object sender, EventArgs args)\n {\n Debug.Log(\"Banner view loaded an ad with response : \"\n + _bannerView.GetResponseInfo());\n Debug.Log(\"Ad Height: {0}, width: {1}\",\n _bannerView.GetHeightInPixels(),\n _bannerView.GetWidthInPixels());\n }\n\n private void OnBannerAdLoadFailed(LoadAdError error)\n {\n Debug.LogError(\"Banner view failed to load an ad with error : \"\n + error);\n }\n\n #endregion\n }\n\nHere the function\n`AdSize.GetCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth` is used\nto get the size for a banner in an anchored position for the current interface\norientation. For pre-loading an anchored banner in a given orientation, use the\nrelevant function from `AdSize.GetPortraitAnchoredAdaptiveBannerAdSizeWithWidth`\nand `AdSize.GetLandscapeAnchoredAdaptiveBannerAdSizeWithWidth`."]]