본문 바로가기
DePloy오류해결

해결 Admob Interstitial ads for Unity

by applejune 2023. 3. 26.

1박 2일을 찾은 끝에 애드몹 전면광고 해결하였다.  

해결에 도움을 준 사이트와 유튜버에 감사를.

 

참고 사이트 2022년과 2023년 사이 코드 예제가 바뀌었다.  최신 애드몹 적용하려면 사이트 참조

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using GoogleMobileAds;
using GoogleMobileAds.Api;



public class Replay : MonoBehaviour
{
   
    private InterstitialAd interstitialAd;
    public Canvas myCanvas;
    public void Start()
    {
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize((InitializationStatus initStatus) =>
        {
            // This callback is called once the MobileAds SDK is initialized.
        });
    }

    // These ad units are configured to always serve test ads.
#if UNITY_ANDROID
    private string _adUnitId = "ca-app-pub-3940256099942544/1033173712";
#elif UNITY_IPHONE
  private string _adUnitId = "ca-app-pub-3940256099942544/4411468910";
#else
  private string _adUnitId = "unused";
#endif

    
    

    public void LoadInterstitialAd()
    {
        if (interstitialAd != null)
        {
            interstitialAd.Destroy();
            interstitialAd = null;
        }

        Debug.Log("Loading the interstitial ad.");

        // create our request used to load the ad.
        var adRequest = new AdRequest.Builder()
                .AddKeyword("unity-admob-sample")
                .Build();

        // send the request to load the ad.
        InterstitialAd.Load(_adUnitId, adRequest,
            (InterstitialAd ad, LoadAdError error) =>
            {
              // if error is not null, the load request failed.
              if (error != null || ad == null)
                {
                    Debug.LogError("interstitial ad failed to load an ad " +
                                   "with error : " + error);
                    return;
                }

                Debug.Log("Interstitial ad loaded with response : "
                          + ad.GetResponseInfo());

                interstitialAd = ad;
            });
     }

    public void ShowAd()
    {
        if (interstitialAd != null && interstitialAd.CanShowAd())
        {
            Debug.Log("Showing interstitial ad.");
            interstitialAd.Show();
        }
        else
        {
            Debug.LogError("Interstitial ad is not ready yet.");
        }
    }
  
    private void RegisterReloadHandler(InterstitialAd ad) //광고 재로드
    {
        ad.OnAdFullScreenContentClosed += (null);
        {
            Debug.Log("Interstitial Ad full screen content closed.");

            LoadInterstitialAd();
        };
        ad.OnAdFullScreenContentFailed += (AdError error) =>
        {
            Debug.LogError("Interstitial ad failed to open full screen content " +
                           "with error : " + error);

            LoadInterstitialAd();
            
        };
    }

    public void ReplayGame()
    {
        LoadInterstitialAd();
        if (this.interstitialAd.CanShowAd())
        {
            this.interstitialAd.Show();
            myCanvas.sortingOrder = -1;
         

        } else {

  SceneManager.LoadScene("PlayScene1");  //예외처리 필요함

}
        

    }
    private void RegisterEventHandlers(InterstitialAd ad)
    {
        ad.OnAdFullScreenContentClosed += () =>
        {
         
            Debug.Log("aaa.");
        };
    }

}
    

 

 

 

https://parksh3641.tistory.com/entry/%EC%9C%A0%EB%8B%88%ED%8B%B0-C-%EA%B5%AC%EA%B8%80-%EC%95%A0%EB%93%9C%EB%AA%B9-Google-Admob-%EC%A0%84%EB%A9%B4-%EA%B4%91%EA%B3%A0-%EA%B0%84%EB%8B%A8-%EC%A0%81%EC%9A%A9%EB%B2%95

 

유니티 C# 구글 애드몹 전면 광고 간단 구현 Google Admob 7.4.1

구글 애드몹 SDK 설치 https://github.com/googleads/googleads-mobile-unity/releases Releases · googleads/googleads-mobile-unity Official Unity Plugin for the Google Mobile Ads SDK - googleads/googleads-mobile-unity github.com 구글 애드몹 홈페

parksh3641.tistory.com

 

https://youtu.be/Wis_Ub_nHuQ