ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 07/22 Unity ads 붙이기
    게임 플랫폼 응용프로그래밍 2021. 7. 22. 12:32

    씬 만들기

     

    광고 종류

    보상형

    전면광고

    배너

     

     

     

     

     

     

     

     

     

     

     

    OFF -> ON

     

    스크립트 생성

    Banner.cs

    UnityAdsMain.cs

    게임오브젝트 생성

    UnityAdsMain

     

    UnityAdsMain.cs

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Advertisements;
    
    public class UnityAdsMain : MonoBehaviour, IUnityAdsInitializationListener
    {
        public string gameId = "4227389";
        public bool testMode = true;
    
        private void Awake()
        {
            Advertisement.Initialize(gameId, testMode, true, this);
        }
    
        public void OnInitializationComplete()
        {
            Debug.Log("Unity Ads init completed");
        }
    
        public void OnInitializationFailed(UnityAdsInitializationError error, string message)
        {
            Debug.LogFormat("[Unity Ads init failed]\n error: {0}\n message: {1}", error, message);
        }
    
    }

     

     

    Banner.cs

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Advertisements;
    using UnityEngine.UI;
    
    public class Banner : MonoBehaviour
    {
        public string adUnitId = "Banner_Android";
        public Button btnLoad;
        public Button btnShow;
        public Button btnHide;
    
        void Start()
        {
            Advertisement.Banner.SetPosition(BannerPosition.BOTTOM_CENTER);
    
            btnShow.interactable = false;
            btnHide.interactable = false;
    
            this.btnLoad.onClick.AddListener(() => {
                this.Load();
            });
    
            this.btnShow.onClick.AddListener(() => {
                this.Show();
            });
    
            this.btnHide.onClick.AddListener(() => {
                this.Hide();
            });
        }
    
        private void Load()
        {
    
            BannerLoadOptions options = new BannerLoadOptions
            {
                loadCallback = OnBannerLoaded,
                errorCallback = OnBannerError
            };
            Advertisement.Banner.Load(adUnitId, options);
        }
    
        void OnBannerLoaded()
        {
            Debug.Log("Banner loaded");
            btnShow.interactable = true;
            btnHide.interactable = true;
        }
    
        void OnBannerError(string message)
        {
            Debug.Log($"Banner Error: {message}");
        }
    
    
        void Show()
        {
            // Set up options to notify the SDK of show events:
            BannerOptions options = new BannerOptions
            {
                clickCallback = OnBannerClicked,
                hideCallback = OnBannerHidden,
                showCallback = OnBannerShown
            };
    
            Advertisement.Banner.Show(adUnitId, options);
        }
    
        void OnBannerClicked() { }
        void OnBannerShown() { }
        void OnBannerHidden() { }
    
    
        void Hide()
        {
            Advertisement.Banner.Hide();
        }
    
    }

     

     

     

    Interstitial 생성

    Interstitial.cs

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Advertisements;
    using UnityEngine.UI;
    
    public class Interstitial : MonoBehaviour, IUnityAdsShowListener, IUnityAdsLoadListener
    {
        public string adUnitId = "Interstitial_Android";
        public Button btnLoad;
        public Button btnShow;
    
        // Start is called before the first frame update
        void Start()
        {
            this.btnLoad.onClick.AddListener(() => { LoadAd(); });
            this.btnShow.onClick.AddListener(() => { ShowAd(); });
        }
    
        // Load content to the Ad Unit:
        public void LoadAd()
        {
            // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
            Debug.Log("Loading Ad: " + adUnitId);
            Advertisement.Load(adUnitId, this);
        }
    
        // Show the loaded content in the Ad Unit: 
        public void ShowAd()
        {
            // Note that if the ad content wasn't previously loaded, this method will fail
            Debug.Log("Showing Ad: " + adUnitId);
            Advertisement.Show(adUnitId, this);
        }
    
        public void OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message)
        {
            Debug.LogFormat("OnUnityAdsShowFailure: {0} {1} {2}", placementId, error, message);
        }
    
        public void OnUnityAdsShowStart(string placementId)
        {
            Debug.LogFormat("OnUnityAdsShowStart: {0}", placementId);
        }
    
        public void OnUnityAdsShowClick(string placementId)
        {
            Debug.LogFormat("OnUnityAdsShowClick: {0}", placementId);
        }
    
        public void OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState)
        {
            Debug.LogFormat("OnUnityAdsShowComplete: {0} {1}", placementId, showCompletionState);
        }
    
        public void OnUnityAdsAdLoaded(string placementId)
        {
            Debug.LogFormat("OnUnityAdsAdLoaded: {0}", placementId);
        }
    
        public void OnUnityAdsFailedToLoad(string placementId, UnityAdsLoadError error, string message)
        {
            Debug.LogFormat("OnUnityAdsFailedToLoad: {0} {1} {2}", placementId, error, message);
        }
    }

     

     

     

    Rewarded.cs

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Advertisements;
    using UnityEngine.UI;
    
    public class Rewarded : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
    {
        public string adUnitId = "Rewarded_Android";
        public Button btnLoad;
        public Button btnShow;
    
        // Start is called before the first frame update
        void Start()
        {
            this.btnShow.interactable = false;
    
            this.btnLoad.onClick.AddListener(() => {
                this.LoadAd();
                this.btnShow.interactable = true;
            });
            this.btnShow.onClick.AddListener(() => {
                this.ShowAd();
                this.btnShow.interactable = false;
            });
        }
    
        public void LoadAd()
        {
            // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
            Debug.Log("Loading Ad: " + adUnitId);
            Advertisement.Load(adUnitId, this);
        }
        public void OnUnityAdsAdLoaded(string placementId)
        {
            Debug.LogFormat("OnUnityAdsAdLoaded: {0}", placementId);
        }
    
        public void OnUnityAdsFailedToLoad(string placementId, UnityAdsLoadError error, string message)
        {
            Debug.LogFormat("OnUnityAdsFailedToLoad: {0}, {1}, {2}", placementId, error, message);
        }
    
    
        // Implement a method to execute when the user clicks the button.
        public void ShowAd()
        {
            // Then show the ad:
            Advertisement.Show(adUnitId, this);
        }
    
        public void OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message)
        {
            Debug.LogFormat("OnUnityAdsShowFailure: {0}, {1}, {2}", placementId, error, message);
        }
    
        public void OnUnityAdsShowStart(string placementId)
        {
            Debug.LogFormat("OnUnityAdsShowStart: {0}", placementId);
        }
    
        public void OnUnityAdsShowClick(string placementId)
        {
            Debug.LogFormat("OnUnityAdsShowClick: {0}", placementId);
        }
    
        public void OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState)
        {
            Debug.LogFormat("OnUnityAdsShowComplete: {0}, {1}", placementId, showCompletionState.ToString());
    
            if (adUnitId.Equals(placementId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
            {
                Debug.Log("Unity Ads Rewarded Ad Completed");
                // Grant a reward.
                Debug.Log("보상을 받았습니다.");
    
                // Load another ad:
                Advertisement.Load(adUnitId, this);
            }
        }
    }

     

     

     

     

     

    빌드 후 테스트

     

     

     

    adb 로그

    '게임 플랫폼 응용프로그래밍' 카테고리의 다른 글

    07/21 Unity IAP  (1) 2021.07.21
    06/30 파이어베이스(Firebase) 사용 연습  (0) 2021.06.30
    06/25 웹뷰  (0) 2021.06.25
    06/24 네이버로그인  (0) 2021.06.24
    06/24 GPGS 4 리더보드  (0) 2021.06.24
Designed by Tistory.