nightward/Assets/Scripts/AttachToGameObject/ADInfo.cs

262 lines
7.7 KiB
C#
Raw Normal View History

2025-11-25 19:58:58 +00:00
using System;
using UnityEngine;
using UnityEngine.Advertisements;
public class ADInfo : MonoBehaviourSingletonTemplate<ADInfo>,
IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
{
#if UNITY_ANDROID
2025-12-17 00:02:40 +00:00
string gameId = "6006223";
2025-11-25 19:58:58 +00:00
string placement_Interstitial = "Interstitial_Android";
string placement_Rewarded = "Rewarded_Android";
string placement_Banner = "Banner_Android";
#elif UNITY_IOS
2025-12-17 00:02:40 +00:00
string gameId = "6006222";
2025-11-25 19:58:58 +00:00
string placement_Interstitial = "Interstitial_iOS";
string placement_Rewarded = "Rewarded_iOS";
string placement_Banner = "Banner_iOS";
#endif
bool testMode = false;
bool interstitialReady = false;
bool rewardedReady = false;
bool bannerLoaded = false;
bool bannerVisible = false;
byte GetReward = 0;
Action Action_success, Action_fail;
float curVolume;
void Start()
{
Advertisement.Initialize(gameId, testMode, this);
}
// ===========================================================
// 광고 ?<3F>청 ?<3F>
2025-11-25 19:58:58 +00:00
// ===========================================================
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_ShopNoAD())
{
GetReward = 1;
return;
}
if (interstitialReady)
{
Advertisement.Show(placement_Interstitial, this);
interstitialReady = false;
}
else
{
LobbyUI.Ins.m_ToastUI.Set("광고<EAB491><EAB3A0>?준<><ECA480>?중입?<3F>다.\n?<3F>시 ???<3F>시 ?<3F>도?<3F>주?<3F>요.");
2025-11-25 19:58:58 +00:00
GetReward = 3;
}
}
else
{
if (rewardedReady)
{
Advertisement.Show(placement_Rewarded, this);
rewardedReady = false;
}
else
{
LobbyUI.Ins.m_ToastUI.Set("광고<EAB491><EAB3A0>?준<><ECA480>?중입?<3F>다.\n?<3F>시 ???<3F>시 ?<3F>도?<3F>주?<3F>요.");
2025-11-25 19:58:58 +00:00
GetReward = 3;
}
}
}
// ===========================================================
// 배너 광고 ?<3F>
2025-11-25 19:58:58 +00:00
// ===========================================================
// ?<3F><>??<3F>서 배너<EBB0B0><EB8488>?켜거???????<3F>
2025-11-25 19:58:58 +00:00
public void Set_Banner(bool active)
{
return; // ?<3F>단 비활?<3F>
2025-11-25 19:58:58 +00:00
if (SaveMgr.Ins.Get_ShopNoAD())
{
// No-AD ?<3F><>?<3F><>???<3F><> ?<3F><>?
2025-11-25 19:58:58 +00:00
if (bannerVisible)
{
HideBanner();
}
return;
}
if (active)
{
ShowBanner();
}
else
{
HideBanner();
}
}
void LoadBanner()
{
if (bannerLoaded) return;
var options = new BannerLoadOptions
{
loadCallback = () =>
{
Debug.Log("Banner Load Success");
bannerLoaded = true;
// ?<3F>동?<3F>로 보이<EBB3B4><EC9DB4>??<3F><EFBFBD><EBA0A4>??<3F>래 ?<3F>
2025-11-25 19:58:58 +00:00
// Advertisement.Banner.Show(placement_Banner);
},
errorCallback = (err) =>
{
Debug.LogWarning("Banner Load Fail: " + err);
bannerLoaded = false;
// ?<3F>요???<3F>시??로직 (?? ?<3F>정 ?<3F>간 ???<3F>시 Load)
2025-11-25 19:58:58 +00:00
// StartCoroutine(RetryLoadBannerCoroutine());
}
};
Advertisement.Banner.SetPosition(BannerPosition.BOTTOM_CENTER);
Advertisement.Banner.Load(placement_Banner, options);
}
void ShowBanner()
{
if (SaveMgr.Ins.Get_ShopNoAD()) return;
if (!bannerLoaded)
{
// 로드가 ???<3F>어 ?<3F><EFBFBD><EC9CBC>??<3F>선 로드?<3F>고, 로드???<3F>동?<3F>로 보여주게 처리
2025-11-25 19:58:58 +00:00
LoadBanner();
// 간단 처리: 로드 ?<3F>료 콜백?<3F>서 ?<3F><EFBFBD><ECA09C>?보여주도<ECA3BC><EB8F84>??<3F>거??즉시 ?<3F>내 메시지
LobbyUI.Ins.m_ToastUI.Set("배너 광고 준<><ECA480>?중입?<3F>다.");
2025-11-25 19:58:58 +00:00
return;
}
var showOptions = new BannerOptions
{
clickCallback = () => Debug.Log("Banner clicked"),
hideCallback = () => Debug.Log("Banner hidden"),
showCallback = () => Debug.Log("Banner shown")
};
Advertisement.Banner.Show(placement_Banner, showOptions);
bannerVisible = true;
}
void HideBanner()
{
Advertisement.Banner.Hide(false); // true<75><65>?destroy
2025-11-25 19:58:58 +00:00
bannerVisible = false;
}
// ===========================================================
// ?<3F>데?<3F>트 - 광고 결과 처리
2025-11-25 19:58:58 +00:00
// ===========================================================
protected override void Update()
{
base.Update();
switch (GetReward)
{
case 1:
GetReward = 0;
Play_Game();
Action_success?.Invoke();
break;
case 2:
case 3:
GetReward = 0;
Play_Game();
Action_fail?.Invoke();
break;
}
}
void Stop_Game()
{
curVolume = AudioListener.volume;
AudioListener.volume = 0f;
}
void Play_Game()
{
AudioListener.volume = curVolume;
}
// ===========================================================
// IUnityAdsInitializationListener
// ===========================================================
public void OnInitializationComplete()
{
Debug.Log("??Unity Ads Initialized");
2025-11-25 19:58:58 +00:00
// 초기???<3F>료 ??광고 미리 로드 (?<3F>터?<3F>티??+ 리워??
2025-11-25 19:58:58 +00:00
Advertisement.Load(placement_Interstitial, this);
Advertisement.Load(placement_Rewarded, this);
// 배너??미리 로드?<3F>되, ?<3F>동 ?<3F>출?<3F> ?<3F><>? ?<3F>
2025-11-25 19:58:58 +00:00
LoadBanner();
}
public void OnInitializationFailed(UnityAdsInitializationError error, string message)
{
Debug.LogError($"??Unity Ads Init Failed: {error} - {message}");
2025-11-25 19:58:58 +00:00
}
// ===========================================================
// IUnityAdsLoadListener
// ===========================================================
public void OnUnityAdsAdLoaded(string placementId)
{
Debug.Log("Ad Loaded: " + placementId);
if (placementId == placement_Interstitial)
interstitialReady = true;
else if (placementId == placement_Rewarded)
rewardedReady = true;
}
public void OnUnityAdsFailedToLoad(string placementId, UnityAdsLoadError error, string message)
{
Debug.LogError($"??Failed to load Ad {placementId}: {error} - {message}");
2025-11-25 19:58:58 +00:00
if (placementId == placement_Interstitial) interstitialReady = false;
else if (placementId == placement_Rewarded) rewardedReady = false;
}
// ===========================================================
// IUnityAdsShowListener
// ===========================================================
public void OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState)
{
if (showCompletionState == UnityAdsShowCompletionState.COMPLETED)
GetReward = 1;
else
GetReward = 2;
// 광고 ?<3F>청 ??즉시 ?<3F>음 광고 미리 로드
2025-11-25 19:58:58 +00:00
Advertisement.Load(placementId, this);
}
public void OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message)
{
Debug.LogError($"??Show failed {placementId}: {error} - {message}");
2025-11-25 19:58:58 +00:00
GetReward = 2;
// ?<3F>패?<3F>어???<3F>음 광고 미리 로드 ?<3F>
2025-11-25 19:58:58 +00:00
Advertisement.Load(placementId, this);
}
public void OnUnityAdsShowStart(string placementId) { }
public void OnUnityAdsShowClick(string placementId) { }
}