I want to show Unity banner ads (Unity ads 3.0.0) for my Java Android app.
I have read https://unityads.unity3d.com/help/android/integration-guide-android
According to the guide lines the following code has to be added to get banner ads.
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) {
}
}
}
There are two buttons bannerButton and initializeButton. But I don't want to show buttons to the user to initialize and show the banner ad. I want to show the ads in the on start method of the activity.
What is the proper way to show the banner ads without bannerButton and initializeButton.
You can just use OnResume method of your activity:
@Override
protected void onResume() {
....
....
UnityMonetization.initialize (myActivity, "1234567", unityMonetizationListener, true);
if (bannerView != null) {
UnityBanners.destroy ();
}
UnityBanners.loadBanner (myActivity, "banner");
}
But it is not the end. You should also take care about Banner visibility and refresh.