내일배움캠프 4일차 TIL . 4주차 5주차 완료!

최보훈·2023년 12월 27일
0

TIL

목록 보기
1/28
post-thumbnail

TMI

본격적으로 내일배움캠프를 시작했다.
원래 시작은 12.21이지만, 해외여행으로 오늘 시작을 하게 됬다. 오늘은 캠프 관련 정보들이나, 내가 참여하지 못했던동안 진행했던 캠프 내용들을 찾아보며 시간을 많이 보낸거같다.

TIL

4주차 강의

image
이번 4주차에서는 카드뒤집기 게임을 만들었다.

  • 게임의 진행방법
  1. 플레이어는 뒤집어진 카드 중에서 똑같은 카드짝들을 제한시간(30S)내에 찾으면 이기는 게임이다.

5주차 강의

5주차 강의에서는 게임의 완성도를 높혀주는 자잘한 방법들에 대해서 공부했다.
사운드 추가, Splash화면, 광고 추가와 같은 기능들에 대해서 공부하였다. 광고 추가의 기능은 처음 배우는 내용이라 정리를 해보았다. 캠프의 다른 학생이 공유해준 자료를 참고해서 진행을 하였다.
-https://www.youtube.com/watch?v=6jQIeZk72cA-

광고 추가하기

  1. 유니티 클라우드의 UnityAds탭에서 다음과 같은 정보를 얻을 수 있다.

    IOS Game ID와 Android Game ID 는 코드에서 참조 가능한 고유 식별자 번호이다.

    플랫폼, 광고 타입과 같은 정보를 확인 가능하며, 설정을 통해서 세부적인 내용이 조절이 가능하다.
  2. 실제 스크립트에서 사용할 코드는 Unity Documentation 에서 확인이 가능하다.
  • Initializing the SDK
    초기화 기능을 하는 코드이다.
using UnityEngine;
using UnityEngine.Advertisements;
 
public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener
{
    [SerializeField] string _androidGameId;              //위에서 말한 Game ID
    [SerializeField] string _iOSGameId;                 //위에서 말한 Game ID
    [SerializeField] bool _testMode = true;
    private string _gameId;
 
    void Awake()
    {
        InitializeAds();
    }
 
    public void InitializeAds()
    {
    #if UNITY_IOS
            _gameId = _iOSGameId;
    #elif UNITY_ANDROID
            _gameId = _androidGameId;
    #elif UNITY_EDITOR
            _gameId = _androidGameId; //Only for testing the functionality in the Editor
    #endif
        if (!Advertisement.isInitialized && Advertisement.isSupported)
        {
            Advertisement.Initialize(_gameId, _testMode, this);
        }
    }

 
    public void OnInitializationComplete()
    {
        Debug.Log("Unity Ads initialization complete.");
    }
 
    public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    {
        Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
    }
}
  • ShowAd() 함수를 통해 광고를 표시해준다.
using UnityEngine;
using UnityEngine.Advertisements;
 
public class InterstitialAdExample : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
{
    [SerializeField] string _androidAdUnitId = "Interstitial_Android";
    [SerializeField] string _iOsAdUnitId = "Interstitial_iOS";
    string _adUnitId;
 
    void Awake()
    {
        // Get the Ad Unit ID for the current platform:
        _adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
            ? _iOsAdUnitId
            : _androidAdUnitId;
    }
 
    // 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);
    }
 
    // Implement Load Listener and Show Listener interface methods: 
    public void OnUnityAdsAdLoaded(string adUnitId)
    {
        // Optionally execute code if the Ad Unit successfully loads content.
    }
 
    public void OnUnityAdsFailedToLoad(string _adUnitId, UnityAdsLoadError error, string message)
    {
        Debug.Log($"Error loading Ad Unit: {_adUnitId} - {error.ToString()} - {message}");
        // Optionally execute code if the Ad Unit fails to load, such as attempting to try again.
    }
 
    public void OnUnityAdsShowFailure(string _adUnitId, UnityAdsShowError error, string message)
    {
        Debug.Log($"Error showing Ad Unit {_adUnitId}: {error.ToString()} - {message}");
        // Optionally execute code if the Ad Unit fails to show, such as loading another ad.
    }
 
    public void OnUnityAdsShowStart(string _adUnitId) { }
    public void OnUnityAdsShowClick(string _adUnitId) { }
    public void OnUnityAdsShowComplete(string _adUnitId, UnityAdsShowCompletionState showCompletionState) { }
}
  1. Unity 설정

    1. 위 두 스크립트를 추가할 오브젝트를 생성해준다.

    Add Initializer 컴포넌트에서 Andrlid Gamd Id, IOS Game ID를 확일 할 수 있다.

    1. 광고를 실행할 버튼을 추가해준다.
    2. 버튼에 다음과 같이 On Click를 할당해주면 완료!


  • #내일배움캠프
  • #스파르타내일배움캠프
  • #스파르타내일배움캠프TIL

0개의 댓글