반응형
Ads 설정
- PackageManager에서 Adverisement 를 선택하여 Import를 합니다.
- Windows/General/Servies 메뉴를 선택합니다.
- 'Create' 버튼을 선택합니다.
- Servies 에서 Ads를 'On' 을 합니다.
- 'Continue'를 선택합니다.
- 테스트를 위해 'Test Mode'를 체크해줍니다.
- 설정이 완료되면 'Dashboard'를 선택하여 대쉬보드로 이동합니다.
- Placement로 이동해서 'Banner', 'interstitial' 을 추가합니다.


Ads 구현하기
Banner 광고 구현하기 (배너)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Advertisements; | |
namespace Siyan.Ads | |
{ | |
public class BannerAds : MonoBehaviour | |
{ | |
[SerializeField] | |
private BannerPosition _bannerPosition = BannerPosition.BOTTOM_CENTER; | |
private const string surfacingId = "banner"; | |
private Coroutine _coroutine; | |
void Start() | |
{ | |
Advertisement.Initialize(Config.GameId, Config.IsTestAds); | |
} | |
private void OnDestroy() | |
{ | |
if (_coroutine != null) | |
{ | |
StopCoroutine(_coroutine); | |
} | |
if (Advertisement.Banner.isLoaded) | |
{ | |
Advertisement.Banner.Hide(); | |
} | |
} | |
public void Show() | |
{ | |
if (_coroutine != null) | |
{ | |
StopCoroutine(_coroutine); | |
} | |
_coroutine = StartCoroutine(ShowBannerWhenInitialized()); | |
} | |
IEnumerator ShowBannerWhenInitialized() | |
{ | |
WaitForSeconds wait = new WaitForSeconds(0.5f); | |
while (!Advertisement.isInitialized) | |
{ | |
yield return wait; | |
} | |
Advertisement.Banner.SetPosition(_bannerPosition); | |
Advertisement.Banner.Show(surfacingId); | |
} | |
} | |
} |
Interstitial 광고 구현하기 (전면)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Advertisements; | |
namespace Siyan.Ads | |
{ | |
public class InterstitialAds : MonoBehaviour | |
{ | |
const string mySurfacingId = "interstitial"; | |
private Coroutine _coroutine; | |
void Start() | |
{ | |
Advertisement.Initialize(Config.GameId, Config.IsTestAds); | |
} | |
private void OnDestroy() | |
{ | |
if (_coroutine != null) | |
{ | |
StopCoroutine(_coroutine); | |
} | |
} | |
public void Show() | |
{ | |
if (_coroutine != null) | |
{ | |
StopCoroutine(_coroutine); | |
} | |
_coroutine = StartCoroutine(ShowInterstitialWhenInitialized()); | |
} | |
IEnumerator ShowInterstitialWhenInitialized() | |
{ | |
WaitForSeconds wait = new WaitForSeconds(0.5f); | |
while (!Advertisement.isInitialized || !Advertisement.IsReady()) | |
{ | |
yield return wait; | |
} | |
Advertisement.Show(); | |
} | |
} | |
} |
Rewarded 광고 구현하기 (보상형)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
using UnityEngine.Advertisements; | |
using UnityEngine.Events; | |
namespace Siyan.Ads | |
{ | |
public class RewardedAds : MonoBehaviour, IUnityAdsListener | |
{ | |
const string mySurfacingId = "rewardedVideo"; | |
[SerializeField] private Button _rewardButton; | |
void Start() | |
{ | |
_rewardButton.enabled = false; | |
Advertisement.AddListener(this); | |
Advertisement.Initialize(Config.GameId, Config.IsTestAds); | |
} | |
public void Show() | |
{ | |
// Check if UnityAds ready before calling Show method: | |
if (Advertisement.IsReady(mySurfacingId)) | |
{ | |
Advertisement.Show(mySurfacingId); | |
} | |
else | |
{ | |
Debug.Log("Rewarded video is not ready at the moment! Please try again later!"); | |
} | |
} | |
// Implement IUnityAdsListener interface methods: | |
public void OnUnityAdsDidFinish(string surfacingId, ShowResult showResult) | |
{ | |
if (surfacingId == mySurfacingId) | |
{ | |
Debug.Log("# OnUnityAdsDidFinish : " + surfacingId + " : " + showResult.ToString()); | |
} | |
} | |
public void OnUnityAdsReady(string surfacingId) | |
{ | |
if (surfacingId == mySurfacingId) | |
{ | |
_rewardButton.enabled = true; | |
Debug.Log("# OnUnityAdsReady : " + surfacingId); | |
} | |
} | |
public void OnUnityAdsDidError(string message) | |
{ | |
Debug.Log("# OnUnityAdsDidError : " + message); | |
} | |
public void OnUnityAdsDidStart(string surfacingId) | |
{ | |
if (surfacingId == mySurfacingId) | |
{ | |
Debug.Log("# OnUnityAdsDidStart : " + surfacingId); | |
} | |
} | |
// When the object that subscribes to ad events is destroyed, remove the listener: | |
public void OnDestroy() | |
{ | |
Advertisement.RemoveListener(this); | |
} | |
} | |
} |
Github 코드
github.com/siyanbae/Unity_UnityAdsDemo.git
siyanbae/Unity_UnityAdsDemo
Contribute to siyanbae/Unity_UnityAdsDemo development by creating an account on GitHub.
github.com
반응형
'Unity' 카테고리의 다른 글
[Android] Local Notification (로컬 푸시) 구현하기 (1) | 2021.05.09 |
---|---|
Spine 연동 하기 (0) | 2021.05.09 |
AdMob 광고 연동하기 (0) | 2021.04.27 |
[FCM] Firebase 클라우드 메시징 테스트 하기 #3 (0) | 2021.04.18 |
[FCM] Firebase 클라우드 메시징 클라이언트 구현하기 #2 (0) | 2021.04.18 |