Unity Ads 3.0.0 在 Android 中展示横幅广告

Unity Ads 3.0.0 show banner ads in Android

我想为我的 Java Android 应用展示 Unity 横幅广告 (Unity ads 3.0.0)。

我已阅读https://unityads.unity3d.com/help/android/integration-guide-android

根据指南,必须添加以下代码才能获得横幅广告。

public class UnityBannerExample extends Activity {
private View bannerView;
private Button bannerButton;

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    setContentView (R.layout.unityads_example_layout);
    final Activity myActivity = this;

    final IUnityBannerListener unityBannerListener = new UnityBannerListener ();
    final IUnityMonetizationListener unityMonetizationListener = new UnityMonetizationListener ();

    UnityBanners.setBannerListener (unityBannerListener);

    bannerButton = (Button) findViewById (R.id.unityads_example_banner_button);
    bannerButton.setEnabled (true);
    bannerButton.setOnClickListener (new View.OnClickListener() {
        @Override
        public void onClick (View v) {
            if (bannerView == null) {
                UnityBanners.loadBanner (myActivity, "banner");
            } else {
                UnityBanners.destroy ();
            }
        }
    });

    final Button initializeButton = (Button) findViewById (R.id.unityads_example_initialize_button);
    initializeButton.setOnClickListener (new View.OnClickListener () {
        @Override
        public void onClick (View v) {
            UnityMonetization.initialize (myActivity, "1234567", unityMonetizationListener, true);
        }
    });
}

private class UnityBannerListener implements IUnityBannerListener {

    @Override
    public void onUnityBannerLoaded (String placementId, View view) {
        bannerView = view;
        ((ViewGroup) findViewById (R.id.unityads_example_layout_root)).addView (view);
    }

    @Override
    public void onUnityBannerUnloaded (String placementId) {
        bannerView = null;
    }

    @Override
    public void onUnityBannerShow (String placementId) {
    }

    @Override
    public void onUnityBannerClick (String placementId) {
    }

    @Override
    public void onUnityBannerHide (String placementId) {
    }

    @Override
    public void onUnityBannerError (String message) {

    }
}

private class UnityMonetizationListener implements IUnityMonetizationListener {

    @Override
    public void onPlacementContentReady (String placementId, PlacementContent placementContent) {
    }

    @Override
    public void onPlacementContentStateChange (String placementId, PlacementContent placementContent, UnityMonetization.PlacementContentState previousState, UnityMonetization.PlacementContentState newState) {
    }

    @Override
    public void onUnityServicesError (UnityServices.UnityServicesError error, String message) {
    }
}
}

有两个按钮 bannerButton 和 initializeButton。 但我不想向用户显示按钮来初始化和显示横幅广告。我想在 activity.

的启动方法中展示广告

在没有 bannerButton 和 initializeButton 的情况下显示横幅广告的正确方法是什么。

您可以只使用 activity:

的 OnResume 方法
@Override
protected void onResume() {
    ....
    ....
    UnityMonetization.initialize (myActivity, "1234567", unityMonetizationListener, true);
    if (bannerView != null) {
        UnityBanners.destroy ();
    }
    UnityBanners.loadBanner (myActivity, "banner");
}

但这还没有结束。您还应该注意横幅的可见性和刷新。

 @Override
    public void onPlacementContentReady (String placementId, PlacementContent placementContent) {

 UnityBanners.loadBanner (myActivity, "banner");
    }