using GoogleMobileAds.Api; using System; using UnityEngine; public class ADInfo : MonoBehaviourSingletonTemplate { //[RuntimeInitializeOnLoadMethod] //static void OnRuntimeMethodLoad() { new GameObject("ADInfo").AddComponent(); } //public static ADInfo Ins; private RewardedInterstitialAd short_rewardedInterstitialAd; //string rewardedInterstitialAd_ID = "ca-app-pub-6390804219581974/3468554834"; string rewardedInterstitialAd_ID = "ca-app-pub-3940256099942544/1033173712"; // 테스트 private InterstitialAd m_interstitialAd; //string InterstitialAd_ID = "ca-app-pub-2550554896492831/9309815439"; string InterstitialAd_ID = "ca-app-pub-3940256099942544/1033173712"; // 테스트 private RewardedAd long_rewardedAd; //string longAD = "ca-app-pub-2550554896492831/2536577422"; string longAD = "ca-app-pub-3940256099942544/5224354917"; // 테스트 BannerView _bannerView; //string bannerAD = "ca-app-pub-2550554896492831/2225586237"; string bannerAD = "ca-app-pub-3940256099942544/6300978111"; // 테스트 byte GetReward = 0; Action Action_success, Action_fail; bool isLoading_InterstitialAd, isLoading_RewardedAd; //private void Awake() //{ // Ins = this; // DontDestroyOnLoad(gameObject); //} public void Start() { MobileAds.Initialize(initStatus => { //Load_RewardedInterstitialAd(rewardedInterstitialAd_ID, Set_RewardedInterstitialAd); Load_InterstitialAd(InterstitialAd_ID, Set_InterstitialAd); Load_LongAD(longAD, Set_LongAD); AdSize adaptiveSize = AdSize.GetPortraitAnchoredAdaptiveBannerAdSizeWithWidth(AdSize.FullWidth); _bannerView = new BannerView(bannerAD, adaptiveSize, AdPosition.Bottom); }); } void Set_RewardedInterstitialAd(RewardedInterstitialAd ad) { if (short_rewardedInterstitialAd != null) { short_rewardedInterstitialAd.Destroy(); short_rewardedInterstitialAd = null; } short_rewardedInterstitialAd = ad; } void Set_InterstitialAd(InterstitialAd ad) { if (m_interstitialAd != null) { m_interstitialAd.Destroy(); m_interstitialAd = null; } m_interstitialAd = ad; m_interstitialAd.OnAdFullScreenContentClosed += () => { GetReward = 1; Load_InterstitialAd(InterstitialAd_ID, Set_InterstitialAd); }; m_interstitialAd.OnAdFullScreenContentFailed += (AdError adError) => { Load_InterstitialAd(InterstitialAd_ID, Set_InterstitialAd); }; } void Set_LongAD(RewardedAd ad) { if (long_rewardedAd != null) { long_rewardedAd.Destroy(); long_rewardedAd = null; } long_rewardedAd = ad; } void Load_RewardedInterstitialAd(string s_ad, Action adaction) { // create our request used to load the ad. var adRequest = new AdRequest(); // send the request to load the ad. RewardedInterstitialAd.Load(s_ad, adRequest, (RewardedInterstitialAd ad, LoadAdError error) => { // if error is not null, the load request failed. if (error != null || ad == null) { Debug.LogError("Rewarded ad failed to load an ad " + "with error : " + error); return; } adaction(ad); }); } void Load_InterstitialAd(string s_ad, Action adaction) { if (isLoading_InterstitialAd) return; isLoading_InterstitialAd = true; // create our request used to load the ad. var adRequest = new AdRequest(); // send the request to load the ad. InterstitialAd.Load(s_ad, adRequest, (InterstitialAd ad, LoadAdError error) => { isLoading_InterstitialAd = false; // if error is not null, the load request failed. if (error != null || ad == null) { Debug.LogError("Rewarded ad failed to load an ad " + "with error : " + error); return; } adaction(ad); }); } void Load_LongAD(string s_ad, Action adaction) { if (isLoading_RewardedAd) return; isLoading_RewardedAd = true; // create our request used to load the ad. var adRequest = new AdRequest(); // send the request to load the ad. RewardedAd.Load(s_ad, adRequest, (RewardedAd ad, LoadAdError error) => { isLoading_RewardedAd = false; // if error is not null, the load request failed. if (error != null || ad == null) { Debug.LogError("Rewarded ad failed to load an ad " + "with error : " + error); return; } adaction(ad); }); } public void Show_AD(bool bshort, Action _success, Action _fail = null) { Stop_Game(); Action_success = _success; Action_fail = _fail; GetReward = 0; if (bshort) { if (SaveMgr.Ins.Get_ShopAD()) // 리워드형 광고가 아니면 제거 { GetReward = 1; return; } if (m_interstitialAd != null && m_interstitialAd.CanShowAd()) { m_interstitialAd.Show(); } else { GetReward = 2; Load_InterstitialAd(InterstitialAd_ID, Set_InterstitialAd); } } else { if (long_rewardedAd != null && long_rewardedAd.CanShowAd()) { long_rewardedAd.Show((Reward reward) => { GetReward = 1; Load_LongAD(longAD, Set_LongAD); }); } else { GetReward = 2; Load_LongAD(longAD, Set_LongAD); } } } protected override void Update() { base.Update(); switch (GetReward) { case 1: GetReward = 0; Play_Game(); Action_success?.Invoke(); // 후 처리 및 저장 break; case 2: GetReward = 0; Play_Game(); LobbyUI.Ins.m_ToastUI.Set("준비된 광고가 없습니다.\n나중에 다시 시도해 주세요."); Action_fail?.Invoke(); break; case 3: GetReward = 0; Play_Game(); LobbyUI.Ins.m_ToastUI.Set("준비된 광고가 없습니다.\n나중에 다시 시도해 주세요."); Action_fail?.Invoke(); break; } } float curVolume; void Stop_Game() { //Time.timeScale = 0f; curVolume = AudioListener.volume; AudioListener.volume = 0f; } void Play_Game() { //Time.timeScale = 1f; AudioListener.volume = curVolume; } public void Set_Banner(bool active) { if (_bannerView == null) return; if (active) { //// 배너 광고 요청 및 표시 //var adRequest = new AdRequest(); //_bannerView.LoadAd(adRequest); if (!SaveMgr.Ins.Get_ShopAD()) _bannerView.Show(); } else { _bannerView.Hide(); } } }