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

时间:2018-11-20 08:26:01

标签: android unityads

我想为我的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来显示横幅广告的正确方法是什么。

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

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

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