文档

Development

User Acquisition

Monetization

工业

Unity Ads Monetization

SDK 4.20.0

受支持
​

Unity API reference

Unity Ads Monetization

SDK 4.20.0

受支持
​

此页面不支持所选语言。
Unity developer guides
​
​
Unity developer guides
  • Unity SDK integration guide
  • Unity Ads privacy guide
  • Unity Ads SDK troubleshooting guide
  • Unity developer API references
  1. Unity Ads SDK Unity developer monetization
  2. Unity Ads SDK integration guide for Unity developers

Implement banner ads in Unity

Implement banner ads in your Unity project. Load ad content and display it through C# scripts in a fixed position on the screen.
阅读时间3 分钟
最后更新于 1 个月前

注意
Starting April 1, 2026, apps that monetize with Unity Ads through direct integration through the Advertisement Legacy Package might see reduced ad performance. To avoid reduction in performance, switch to integrating Unity Ads as a bidder.
The Unity Ads network will continue to support and provide fill for all apps using direct integration. However, to maximize revenue and performance, the recommended best practice is to install the Ads Mediation package for LevelPlay, Unity's ad mediation platform.
重要
app-ads.txt is an IAB initiative to combat fraud and create transparency in the advertising ecosystem. Ensure that you implement app-ads.txt as described. Otherwise, banner demand might significantly decrease.

Script implementation

In your script header, declare the
UnityEngine.Advertisements
namespace, which contains the
Banner
class. Next, initialize the SDK, then use the
Banner.Load
and
Banner.Show
methods to load and display a banner ad.
The following example script shows you how to set up buttons in a Scene to test this functionality. To create a button in the Unity Editor, select Game Object > UI > Button.
注意
Only load content after the SDK is initialized, otherwise the script will not work. In this example, initialization is handled in a different script.

Banner ad example

using UnityEngine;using UnityEngine.UI;using UnityEngine.Advertisements;public class BannerAdExample : MonoBehaviour{ // For the purpose of this example, these buttons are for functionality testing: [SerializeField] Button _loadBannerButton; [SerializeField] Button _showBannerButton; [SerializeField] Button _hideBannerButton; [SerializeField] BannerPosition _bannerPosition = BannerPosition.BOTTOM_CENTER; [SerializeField] string _androidAdUnitId = "Banner_Android"; [SerializeField] string _iOSAdUnitId = "Banner_iOS"; string _adUnitId = null; // This will remain null for unsupported platforms. void Start() { // Get the Ad Unit ID for the current platform: #if UNITY_IOS _adUnitId = _iOSAdUnitId; #elif UNITY_ANDROID _adUnitId = _androidAdUnitId; #endif // Disable the button until an ad is ready to show: _showBannerButton.interactable = false; _hideBannerButton.interactable = false; // Set the banner position: Advertisement.Banner.SetPosition(_bannerPosition); // Configure the Load Banner button to call the LoadBanner() method when clicked: _loadBannerButton.onClick.AddListener(LoadBanner); _loadBannerButton.interactable = true; } // Implement a method to call when the Load Banner button is clicked: public void LoadBanner() { // Set up options to notify the SDK of load events: BannerLoadOptions options = new BannerLoadOptions { loadCallback = OnBannerLoaded, errorCallback = OnBannerError }; // Load the Ad Unit with banner content: Advertisement.Banner.Load(_adUnitId, options); } // Implement code to execute when the loadCallback event triggers: void OnBannerLoaded() { Debug.Log("Banner loaded"); // Configure the Show Banner button to call the ShowBannerAd() method when clicked: _showBannerButton.onClick.AddListener(ShowBannerAd); // Configure the Hide Banner button to call the HideBannerAd() method when clicked: _hideBannerButton.onClick.AddListener(HideBannerAd); // Enable both buttons: _showBannerButton.interactable = true; _hideBannerButton.interactable = true; } // Implement code to execute when the load errorCallback event triggers: void OnBannerError(string message) { Debug.Log($"Banner Error: {message}"); // Optionally execute additional code, such as attempting to load another ad. } // Implement a method to call when the Show Banner button is clicked: void ShowBannerAd() { // Set up options to notify the SDK of show events: BannerOptions options = new BannerOptions { clickCallback = OnBannerClicked, hideCallback = OnBannerHidden, showCallback = OnBannerShown }; // Show the loaded Banner Ad Unit: Advertisement.Banner.Show(_adUnitId, options); } // Implement a method to call when the Hide Banner button is clicked: void HideBannerAd() { // Hide the banner: Advertisement.Banner.Hide(); } void OnBannerClicked() { } void OnBannerShown() { } void OnBannerHidden() { } void OnDestroy() { // Clean up the listeners: _loadBannerButton.onClick.RemoveAllListeners(); _showBannerButton.onClick.RemoveAllListeners(); _hideBannerButton.onClick.RemoveAllListeners(); }}

Banner position

By default, banner ads display anchored on the bottom-center of the screen, supporting 320 x 50 or 728 x 90 pixel resolution. To specify the banner anchor, use the
Banner.SetPosition
API. For example:
Advertisement.Banner.SetPosition (BannerPosition.TOP_CENTER);
Next steps: Review the monetization strategy guide and test your implementation.

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

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

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

  • 在本页上
    • Script implementation

      • Banner ad example

    • Banner position


报告此页面的问题
​
​