0

我想为我的 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。但我不想向用户显示按钮来初始化和显示横幅广告。我想在活动的 on start 方法中显示广告。

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

4

2 回答 2

1

您可以只使用活动的 OnResume 方法:

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

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

于 2018-12-06T15:56:46.130 回答
0
 @Override
    public void onPlacementContentReady (String placementId, PlacementContent placementContent) {

 UnityBanners.loadBanner (myActivity, "banner");
    }
于 2019-03-31T19:29:45.733 回答