๊ธฐ์ˆ  ์ž๋ฃŒ

์ง€์›

Android์—์„œ ๋ฐฐ๋„ˆ ๊ด‘๊ณ  ๊ตฌํ˜„

Implement banner ads in your Android app. Create banner view objects, display them in your layout, and use a listener to manage ad events.
์ฝ๋Š” ์‹œ๊ฐ„ 2๋ถ„

๋ฐฐ๋„ˆ ๊ด‘๊ณ ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํŠน์ • ์œ ํ˜•์˜ ๋ฐฐ๋„ˆ ์ „์šฉ ๊ด‘๊ณ  ์œ ๋‹›์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ทฐ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฐฐ๋„ˆ ๋ทฐ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋ทฐ ๊ณ„์ธต ๊ตฌ์กฐ์— ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๊ฐ ๋ฐฐ๋„ˆ ์ธ์Šคํ„ด์Šค์˜ ์œ„์น˜๋ฅผ ์ปค์Šคํ„ฐ๋งˆ์ด์ฆˆํ•˜๊ฑฐ๋‚˜ ์—ฌ๋Ÿฌ ๋ฐฐ๋„ˆ๋ฅผ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์Šคํฌ๋ฆฝํŠธ ๊ตฌํ˜„

๋‹ค์Œ ์Šคํฌ๋ฆฝํŠธ ์ƒ˜ํ”Œ์€ ํ™”๋ฉด์— ๋‘ ๊ฐœ์˜ ๋ฐฐ๋„ˆ ๊ด‘๊ณ ์™€ ์ด๋ฅผ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•œ ๋ฒ„ํŠผ์„ ํ‘œ์‹œํ•˜๋Š” ๊ตฌํ˜„ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ๋Š” ์—ฌ๋Ÿฌ ๋ฐฐ๋„ˆ ๋ทฐ ์˜ค๋ธŒ์ ํŠธ์— ๋‹จ์ผ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ๋„ˆ ๋ทฐ ์˜ค๋ธŒ์ ํŠธ๋ณ„๋กœ ๋‹ค๋ฅธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

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) {
  }
}
๋‹ค์Œ ๋‹จ๊ณ„: ์ˆ˜์ตํ™” ์ „๋žต ๊ฐ€์ด๋“œ๋ฅผ ๊ฒ€ํ† ํ•˜๊ณ  ๊ตฌํ˜„ ๋‚ด์šฉ์„ ํ…Œ์ŠคํŠธํ•˜์‹ญ์‹œ์˜ค.

Android์—์„œ ๋ฐฐ๋„ˆ ๊ด‘๊ณ  ๊ตฌํ˜„ โ€ข Unity Grow โ€ข Unity Docs