TIL

[멋쟁이사자처럼 부트캠프 TIL 회고] 유니티 게임개발 3기 62일차 퀴즈게임 제작8

HYttt 2025. 3. 6. 01:23

퀴즈게임 제작

전면광고, 리워드 광고 추가

스크립트를 추가하고 광고를 표시할 곳에 적용

  • 전면광고 스크립트
public void LoadInterstitialAd()
    {
        if(_interstitialAd != null)
        {
            _interstitialAd.Destroy();
            _interstitialAd = null;
        }
        
        Debug.Log("Load Interstitial Ad");

        var adRequest = new AdRequest();
        
        InterstitialAd.Load(_interstitialAdUnitId, adRequest, (InterstitialAd ad, LoadAdError error) =>
        {
            if(error != null || ad == null)
            {
                Debug.LogError("Interstitial ad failed to load with error : " + error);
                return;
                
            }
            Debug.Log("Interstitial ad loaded with response : " + ad.GetResponseInfo());
            _interstitialAd = ad;
            
            RegisterInterstitialAdsEventHandlers(_interstitialAd);
        });
    }
    
    public void ShowInterstitialAd()
    {
        if (_interstitialAd != null && _interstitialAd.CanShowAd())
        {
            Debug.Log("Show Interstitial Ad");
            _interstitialAd.Show();
        }
        else
        {
            Debug.Log("Interstitial ad is not ready yet");
        }
        
    }
    
    private void RegisterInterstitialAdsEventHandlers(InterstitialAd interstitialAd)
    {
        // Raised when the ad is estimated to have earned money.
        interstitialAd.OnAdPaid += (AdValue adValue) =>
        {
            Debug.Log(String.Format("Interstitial ad paid {0} {1}.",
                adValue.Value,
                adValue.CurrencyCode));
        };
        // Raised when an impression is recorded for an ad.
        interstitialAd.OnAdImpressionRecorded += () =>
        {
            Debug.Log("Interstitial ad recorded an impression.");
        };
        // Raised when a click is recorded for an ad.
        interstitialAd.OnAdClicked += () =>
        {
            Debug.Log("Interstitial ad was clicked.");
        };
        // Raised when an ad opened full screen content.
        interstitialAd.OnAdFullScreenContentOpened += () =>
        {
            Debug.Log("Interstitial ad full screen content opened.");
        };
        // Raised when the ad closed full screen content.
        interstitialAd.OnAdFullScreenContentClosed += () =>
        {
            Debug.Log("Interstitial ad full screen content closed.");
            
            // 전면광고 닫히면 다시 로드
            LoadInterstitialAd();
        };
        // Raised when the ad failed to open full screen content.
        interstitialAd.OnAdFullScreenContentFailed += (AdError error) =>
        {
            Debug.LogError("Interstitial ad failed to open full screen content " +
                           "with error : " + error);
            
            // 전면광고 로드 실패시 다시 로드
            LoadInterstitialAd();
        };
    }
  • 리워드 광고 스크립트
public void LoadRewardedAd()
    {
        if (_rewardedAd != null)
        {
            _rewardedAd.Destroy();
            _rewardedAd = null;
        }
        
        Debug.Log("Load Rewarded Ad");
        
        var adRequest = new AdRequest();
        
        RewardedAd.Load(_rewardedAdUnitId, adRequest, (RewardedAd ad, LoadAdError error) =>
        {
            if (error != null || ad == null)
            {
                Debug.LogError("Rewarded ad failed to load with error : " + error);
                return;
            }
            Debug.Log("Rewarded ad loaded with response : " + ad.GetResponseInfo());
            _rewardedAd = ad;
        });
        RegisterRewardedAdEventHandlers(_rewardedAd);
    }
    
    public void ShowRewardedAd()
    {
        const string rewardMessage = "Rewarded ad rewarded user. Type : {0}, Amount : {1}";

        if (_rewardedAd != null && _rewardedAd.CanShowAd())
        {
            _rewardedAd.Show((Reward reward) =>
            {
                Debug.Log(String.Format(rewardMessage, reward.Type, reward.Amount));
                // 광고 다 봄 
            });
        }
    }
    
    private void RegisterRewardedAdEventHandlers(RewardedAd ad)
    {
        // Raised when the ad is estimated to have earned money.
        ad.OnAdPaid += (AdValue adValue) =>
        {
            Debug.Log(String.Format("Rewarded ad paid {0} {1}.",
                adValue.Value,
                adValue.CurrencyCode));
        };
        // Raised when an impression is recorded for an ad.
        ad.OnAdImpressionRecorded += () =>
        {
            Debug.Log("Rewarded ad recorded an impression.");
        };
        // Raised when a click is recorded for an ad.
        ad.OnAdClicked += () =>
        {
            Debug.Log("Rewarded ad was clicked.");
        };
        // Raised when an ad opened full screen content.
        ad.OnAdFullScreenContentOpened += () =>
        {
            Debug.Log("Rewarded ad full screen content opened.");
        };
        // Raised when the ad closed full screen content.
        ad.OnAdFullScreenContentClosed += () =>
        {
            Debug.Log("Rewarded ad full screen content closed.");
            
            // Reload the ad so that we can show another as soon as possible.
            LoadRewardedAd();
            
            // 광고 다 봤는지 확인해서 보상 지급
        };
        // Raised when the ad failed to open full screen content.
        ad.OnAdFullScreenContentFailed += (AdError error) =>
        {
            Debug.LogError("Rewarded ad failed to open full screen content " +
                           "with error : " + error);
            
            // Reload the ad so that we can show another as soon as possible.
            LoadRewardedAd();
        };
    }

세부 기능 추가하여 완성

  • 시작시 하트 1개 감소
  • 스테이지 시작, 클리어 애니메이션
  • 레벨업 게이지
  • 스테이지 클리어 후 전면 광고 표시
  • 게임 종료 애니메이션
  • 광고보고 하트 3개 받기