文档

​
​

Development

User Acquisition

Monetization

工业

Unity Ads 变现

Unity Ads Android SDK

Unity Ads iOS SDK

Unity Ads SDK

Unity Ads 变现

Unity Ads Monetization
​
​
Android 开发者指南
  • Android SDK 概述
  • 集成要求
  • 安装适用于 Android 的 Unity Ads SDK
  • 在 Android 中初始化 SDK
  • 在 Android 中实现插页式广告
  • 在 Android 中实现奖励广告
  • 在 Android 中实现横幅广告
Android SDK API 参考
  • Unity Ads for Android API 参考(Java)
  1. 实现游戏增长
  2. Unity Ads
  3. 适用于 Android 开发者的 Unity Ads SDK 集成指南

在 Android 中实现横幅广告

在 Android 应用程序中实现横幅广告。创建横幅广告视图对象,在布局中展示,并使用监听器管理广告事件。
阅读时间5 分钟
最后更新于 2 天前

注意
在展示横幅广告之前必须初始化 Unity Ads。
横幅广告需要特定类型的专用横幅广告单元。
您可以将横幅广告视图对象放入 View 层级视图中,就像放置任何其他视图一样。这样就可以自定义每个横幅广告实例的位置,或显示多个横幅广告。
注意
app-ads.txt 是一个 IAB 倡议文件,旨在打击欺诈,建立透明的广告生态系统。请务必按照要求实现 app-ads.txt。否则,横幅广告需求可能会显著下降。

脚本实现

以下脚本示例是用于在屏幕上显示两个横幅广告以及用于测试这些广告的按钮的示例实现方案:
此示例对多个横幅广告视图对象使用单个监听器。也可以为每个横幅广告视图对象设置不同的监听器。
注意
Unity Ads 需要访问当前正在运行的 Activity,所以下面的示例使用
getApplicationContext()
。 由于这可能不适合所有实现,因此可能需要进行一些定制(具体取决于集成情况)。

实现 MREC 横幅广告格式

要在应用中显示中等矩形 (MREC) 大小的广告格式,请确保使用适当的尺寸,例如 300x250、300x300 或 450x450。如果使用的是 300x250 的 MREC 横幅广告,将它替换为以下示例代码:
new UnityBannerSize(320, 50)
调整后的尺寸:
new UnityBannerSize(300, 250)

横幅广告示例

import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import com.unity3d.ads.IUnityAdsInitializationListener; import com.unity3d.ads.UnityAds; import com.unity3d.services.banners.BannerErrorInfo; import com.unity3d.services.banners.BannerView; import com.unity3d.services.banners.UnityBannerSize; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; public class ShowBanners extends AppCompatActivity implements IUnityAdsInitializationListener { String unityGameID = "1234567"; Boolean testMode = true; String topAdUnitId = "topBanner"; String bottomAdUnitId = "bottomBanner"; // Listener for banner events: private BannerView.IListener bannerListener = new BannerView.IListener() { @Override public void onBannerLoaded(BannerView bannerAdView) { // Called when the banner is loaded. Log.v("UnityAdsExample", "onBannerLoaded: " + bannerAdView.getPlacementId()); // Enable the correct button to hide the ad (bannerAdView.getPlacementId().equals("topBanner") ? hideTopBannerButton : hideBottomBannerButton).setEnabled(true); } @Override public void onBannerFailedToLoad(BannerView bannerAdView, BannerErrorInfo errorInfo) { Log.e("UnityAdsExample", "Unity Ads failed to load banner for " + bannerAdView.getPlacementId() + " with error: [" + errorInfo.errorCode + "] " + errorInfo.errorMessage); // Note that the BannerErrorInfo object can indicate a no fill (refer to the API documentation). } @Override public void onBannerClick(BannerView bannerAdView) { // Called when a banner is clicked. Log.v("UnityAdsExample", "onBannerClick: " + bannerAdView.getPlacementId()); } @Override public void onBannerLeftApplication(BannerView bannerAdView) { // Called when the banner links out of the application. Log.v("UnityAdsExample", "onBannerLeftApplication: " + bannerAdView.getPlacementId()); } }; // This banner view object will be placed at the top of the screen: BannerView topBanner; // This banner view object will be placed at the bottom of the screen: BannerView bottomBanner; // View objects to display banners: RelativeLayout topBannerView; RelativeLayout bottomBannerView; // Buttons to show the banners: Button showTopBannerButton; Button showBottomBannerButton; // Buttons to destroy the banners: Button hideTopBannerButton; Button hideBottomBannerButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize Unity Ads: UnityAds.initialize(getApplicationContext(), unityGameID, testMode, this); // Set up a button to load the top banner when clicked: showTopBannerButton = findViewById(R.id.loadTopBanner); // Set up a button to load the bottom banner when clicked: showBottomBannerButton = findViewById(R.id.loadBottomBanner); // Set up a button to destroy the top banner when clicked: hideTopBannerButton = findViewById(R.id.hideTopBanner); // Set up a button to destroy the bottom banner when clicked: hideBottomBannerButton = findViewById(R.id.hideBottomBanner); showTopBannerButton.setEnabled(false); showBottomBannerButton.setEnabled(false); hideTopBannerButton.setEnabled(false); hideBottomBannerButton.setEnabled(false); showTopBannerButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showTopBannerButton.setEnabled(false); // Create the top banner view object: topBanner = new BannerView(view.getContext(), topAdUnitId, new UnityBannerSize(320, 50)); // Set the listener for banner lifecycle events: topBanner.setListener(bannerListener); topBannerView = findViewById(R.id.topBanner); LoadBannerAd(topBanner, topBannerView); } }); showBottomBannerButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showBottomBannerButton.setEnabled(false); // Create the bottom banner view object: bottomBanner = new BannerView(view.getContext(), bottomAdUnitId, new UnityBannerSize(320, 50)); // Set the listener for banner lifecycle events: bottomBanner.setListener(bannerListener); bottomBannerView = findViewById(R.id.bottomBanner); LoadBannerAd(bottomBanner, bottomBannerView); } }); hideTopBannerButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Remove content from the banner view: topBannerView.removeAllViews(); // Remove the banner variables: topBannerView = null; topBanner = null; showTopBannerButton.setEnabled(true); } }); hideBottomBannerButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Remove content from the banner view: bottomBannerView.removeAllViews(); // Remove the banner variables: bottomBannerView = null; bottomBanner = null; showBottomBannerButton.setEnabled(true); } }); } public void LoadBannerAd(BannerView bannerView, RelativeLayout bannerLayout) { // Request a banner ad: bannerView.load(); // Associate the banner view object with the banner view: bannerLayout.addView(bannerView); } @Override public void onInitializationComplete() { // Enable the show ad buttons because ads can now be loaded showTopBannerButton.setEnabled(true); showBottomBannerButton.setEnabled(true); } @Override public void onInitializationFailed(UnityAds.UnityAdsInitializationError error, String message) { } }
后续步骤:查看变现策略指南或测试您的实现。

Copyright © 2026 Unity Technologies
法律信息隐私政策CookiesDocumentation Terms of Use请勿出售或分享我的个人信息您的隐私选择(Cookie 设置)

“Unity”、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属公司在美国和其他地方的商标或注册商标(此处查看更多信息)。其他名称或品牌是其各自所有者的商标。

为方便起见,一些页面是机器翻译的,可能包含不准确的内容。如有信息不一致的情况,以英文版本为准。

  • 在本页上
    • 脚本实现

      • 实现 MREC 横幅广告格式

        • 横幅广告示例


报告此页面的问题