Unity/서버

210722_ 유니티Ads

minquu 2021. 7. 22. 12:28
반응형

 

 

https://docs.unity3d.com/kr/2019.3/Manual/UnityAds.html

 

Unity 애즈 - Unity 매뉴얼

최고의 모바일 게임 엔진으로 개발된 Unity Ads SDK는 Unity, xCode, Android Studio 등 모든 플랫폼으로 개발하는 게임을 위한 포괄적인 수익화 프레임워크를 제공합니다. 또한 최첨단 머신러닝을 활용하

docs.unity3d.com

 

 

https://github.com/smilejsu82/unity_ads

 

GitHub - smilejsu82/unity_ads: unity ads

unity ads. Contribute to smilejsu82/unity_ads development by creating an account on GitHub.

github.com

 

 

참고 사이트 ----

-------

 

유니티 GPGS가 연결되어있는 곳에

 

새 씬을 열어서

 

텍스트와 버튼을 달아준다.

 

 

크게 광고는 3개로 보면 된다.

보상형 광고 - 영상인데 스킵 불가능

전면광고 - 영상인데 스킵 가능

배너광고 - 상시 띄어져있는 광고

 

 

상단 고 어카운트가기

 

 

프로젝트 누르고

 

위 대시보드 클릭

 

모네타이즈 클ㄹ릭

 

내 프로젝트 클릭하기

 

enable ads 클릭

 

 

일단 애드몹 으로 하기

 

서드파트는 나중에 애드몹 달면 붙여도 된다.

 

그러면

 

이런 페이지가 나온다.

 

이 앱 아이디를사용한다.

 

여기있는 id를 문자열로 사용한다.

 

애드 를 하면 만들수도 있다! 참고하자

 

 

서비스에서 애즈 누르기

 

 

온으로 바꿔주기

 

인스톨해주기

 

아이디는 자동으로 연동이 된다.

 

닫고, 우리는 컴포넌트 식으로 연동을 해줄 것이다.

 

배너 스크립터 만들기

 

https://unityads.unity3d.com/help/unity/integration-guide-unity

 

Integration guide for Unity - Knowledge base

Integration guide for Unity Overview This guide covers integration for implementing Unity Ads in your made-with-Unity game. If you are an iOS developer using Objective-C, click here. If you are an Android developer using Java, click here. Click here for th

unityads.unity3d.com

 

이 API 보면서 하기

 

먼저 이니셜라이즈를 해줘야한다.

 

 

 

새로 스크립터 만들어주고

 

 

초기화 해주는데

 

true와 this

 

저 만들어준 인터페이스를 사용해주는 것

 

 

스크립터 오브젝트에 붙여서

 

 

플레이하면

 

초기화 까지 됌

 

배너 코드

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

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.UI;

public class interstitial : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
{
    public string adUnitId = "Interstitial_Android";
    public Button btnLoad;
    public Button btnShow;

    void Start()
    {

        this.btnShow.interactable = false;

        this.btnLoad.onClick.AddListener(() => {
            LoadAd();
            this.btnShow.interactable = true;
        });

        this.btnShow.onClick.AddListener(() => {
            this.ShowAd();
        });
    }

    public void LoadAd()
    {
        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);
    }

    public void ShowAd()
    {
        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.ToString());
    }
}

 

 

rewarded

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);
        }
    }
}

 

 

 

버튼에 스크립터를 각각달아주고.

 

빌드해서 보면

 

 

 

 

 

광고가 잘 나온다.

 

 

반응형