https://docs.unity3d.com/kr/2019.3/Manual/UnityAds.html
https://github.com/smilejsu82/unity_ads
참고 사이트 ----
-------
유니티 GPGS가 연결되어있는 곳에
새 씬을 열어서
텍스트와 버튼을 달아준다.
크게 광고는 3개로 보면 된다.
보상형 광고 - 영상인데 스킵 불가능
전면광고 - 영상인데 스킵 가능
배너광고 - 상시 띄어져있는 광고
상단 고 어카운트가기
프로젝트 누르고
위 대시보드 클릭
모네타이즈 클ㄹ릭
내 프로젝트 클릭하기
enable ads 클릭
일단 애드몹 으로 하기
서드파트는 나중에 애드몹 달면 붙여도 된다.
그러면
이런 페이지가 나온다.
이 앱 아이디를사용한다.
여기있는 id를 문자열로 사용한다.
애드 를 하면 만들수도 있다! 참고하자
서비스에서 애즈 누르기
온으로 바꿔주기
인스톨해주기
아이디는 자동으로 연동이 된다.
닫고, 우리는 컴포넌트 식으로 연동을 해줄 것이다.
배너 스크립터 만들기
https://unityads.unity3d.com/help/unity/integration-guide-unity
이 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);
}
}
}
버튼에 스크립터를 각각달아주고.
빌드해서 보면
광고가 잘 나온다.
'Unity > 서버' 카테고리의 다른 글
210727_ 유니티 RestAPI (0) | 2021.07.27 |
---|---|
210722_ AdMob _구글 애드몹 유니티에 달기 (0) | 2021.07.22 |
210721_ IAP (인앱결제) (0) | 2021.07.21 |
210720_ 포톤 서버 (시험) (5) | 2021.07.20 |
210714 _ 데이터 프로그래밍 2 (복습 및 시험) (0) | 2021.07.14 |