Shegotwet/Assets/Scripts/AttachToGameObject/ADInfo.cs

249 lines
7.2 KiB
C#
Raw Normal View History

using GoogleMobileAds.Api;
using System;
using UnityEngine;
public class ADInfo : MonoBehaviourSingletonTemplate<ADInfo>
{
//[RuntimeInitializeOnLoadMethod]
//static void OnRuntimeMethodLoad() { new GameObject("ADInfo").AddComponent<ADInfo>(); }
//public static ADInfo Ins;
private RewardedInterstitialAd short_rewardedInterstitialAd;
string rewardedInterstitialAd_ID = "ca-app-pub-6390804219581974/3468554834";
private InterstitialAd m_interstitialAd;
string InterstitialAd_ID = "ca-app-pub-6390804219581974/2124679733";
private RewardedAd long_rewardedAd;
string longAD = "ca-app-pub-6390804219581974/9811598061";
//string longAD = "ca-app-pub-3940256099942544/5224354917"; // 테스트
BannerView _bannerView;
string bannerAD = "ca-app-pub-6390804219581974/2451982794";
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);
_bannerView = new BannerView(bannerAD, AdSize.Banner, 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<RewardedInterstitialAd> 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<InterstitialAd> 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<RewardedAd> 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 (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);
_bannerView.Show();
}
else
{
_bannerView.Hide();
}
}
}