在 Android 中实现横幅广告
Note: You must initialize Unity Ads before displaying a banner ad.
横幅广告需要特定类型的专用横幅 广告单元.
您可以像放置其他视图一样将横幅视图对象放入您的视图层次结构中。这允许您自定义每个横幅实例的位置,或显示多个横幅。
Note: 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 be significantly decreased.
脚本实现
以下脚本示例是在屏幕上显示两个横幅广告以及用于测试它们的按钮的示例实现。
此示例对多个横幅视图对象使用单个侦听器。您也可以为每个横幅视图对象使用不同的侦听器。
Note: Unity Ads requires access to the currently running Activity, so the following example uses getApplicationContext()
. Because this might not be suitable for all implementations, some customization might be required (depending on the integration).
实现 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) {
}
}