본문 바로가기
개발/C#

유니티 C# 구글 애드몹 보상형 전면 광고 간단 구현 Admob 8.7.0

by SPNK 2024. 2. 14.
반응형

구글 애드몹 SDK 설치

 

Releases · googleads/googleads-mobile-unity

Official Unity Plugin for the Google Mobile Ads SDK - googleads/googleads-mobile-unity

github.com

 

  • 구글 애드몹 홈페이지
 

보상형 전면 광고  |  Unity  |  Google for Developers

이 페이지는 Cloud Translation API를 통해 번역되었습니다. 보상형 전면 광고 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. Rewarded interstitial is a type of incentivized

developers.google.com

 

 

  • v8.7.0 기준
using GoogleMobileAds;
using GoogleMobileAds.Api;

public class GoogleMobileAdsDemoScript: MonoBehaviour {

        private RewardedInterstitialAd _rewardedInterstitialAd;

        public void Start() {
            // Initialize the Google Mobile Ads SDK.
            MobileAds.Initialize((InitializationStatus initStatus) => {
                // This callback is called once the MobileAds SDK is initialized.
            });


            #if UNITY_ANDROID
            private string _adUnitId = "ca-app-pub-3940256099942544/5354046379";
            #elif UNITY_IPHONE
            private string _adUnitId = "ca-app-pub-3940256099942544/6978759866";
            #else
            private string _adUnitId = "unused";
            #endif
        }

        public void LoadRewardedInterstitialAd() {
            // Clean up the old ad before loading a new one.
            if (_rewardedInterstitialAd != null) {
                _rewardedInterstitialAd.Destroy();
                _rewardedInterstitialAd = null;
            }

            Debug.Log("Loading the rewarded interstitial ad.");

            // create our request used to load the ad.
            var adRequest = new AdRequest();
            adRequest.Keywords.Add("unity-admob-sample");

            // send the request to load the ad.
            RewardedInterstitialAd.Load(_adUnitId, adRequest,
                (RewardedInterstitialAd ad, LoadAdError error) => {
                    // if error is not null, the load request failed.
                    if (error != null || ad == null) {
                        Debug.LogError("rewarded interstitial ad failed to load an ad " +
                            "with error : " + error);
                        return;
                    }

                    Debug.Log("Rewarded interstitial ad loaded with response : " +
                        ad.GetResponseInfo());

                    _rewardedInterstitialAd = ad;
                });
        }

        public void ShowRewardedInterstitialAd() {
            const string rewardMsg =
                "Rewarded interstitial ad rewarded the user. Type: {0}, amount: {1}.";

            if (rewardedInterstitialAd != null && rewardedInterstitialAd.CanShowAd()) {
                rewardedInterstitialAd.Show((Reward reward) => {
                    // TODO: Reward the user.
                    Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount));
                });
            }
        }
}

 

 


 
반응형

댓글