nightward/Assets/Scripts/AttachToGameObject/ADInfo.cs

262 lines
7.8 KiB
C#

using System;
using UnityEngine;
using UnityEngine.Advertisements;
public class ADInfo : MonoBehaviourSingletonTemplate<ADInfo>,
IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
{
#if UNITY_ANDROID
string gameId = "5947579";
string placement_Interstitial = "Interstitial_Android";
string placement_Rewarded = "Rewarded_Android";
string placement_Banner = "Banner_Android";
#elif UNITY_IOS
string gameId = "5947578";
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);
}
// ===========================================================
// 광고 시청 요청
// ===========================================================
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("광고를 준비 중입니다.\n잠시 후 다시 시도해주세요.");
GetReward = 3;
}
}
else
{
if (rewardedReady)
{
Advertisement.Show(placement_Rewarded, this);
rewardedReady = false;
}
else
{
LobbyUI.Ins.m_ToastUI.Set("광고를 준비 중입니다.\n잠시 후 다시 시도해주세요.");
GetReward = 3;
}
}
}
// ===========================================================
// 배너 광고 제어
// ===========================================================
// 외부에서 배너를 켜거나 끌 때 호출
public void Set_Banner(bool active)
{
return; // 일단 비활성화
if (SaveMgr.Ins.Get_ShopNoAD())
{
// No-AD 유저면 항상 숨김
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;
// 자동으로 보이게 하려면 아래 호출
// Advertisement.Banner.Show(placement_Banner);
},
errorCallback = (err) =>
{
Debug.LogWarning("Banner Load Fail: " + err);
bannerLoaded = false;
// 필요시 재시도 로직 (예: 일정 시간 후 다시 Load)
// StartCoroutine(RetryLoadBannerCoroutine());
}
};
Advertisement.Banner.SetPosition(BannerPosition.BOTTOM_CENTER);
Advertisement.Banner.Load(placement_Banner, options);
}
void ShowBanner()
{
if (SaveMgr.Ins.Get_ShopNoAD()) return;
if (!bannerLoaded)
{
// 로드가 안 되어 있으면 우선 로드하고, 로드시 자동으로 보여주게 처리
LoadBanner();
// 간단 처리: 로드 완료 콜백에서 실제로 보여주도록 하거나 즉시 안내 메시지
LobbyUI.Ins.m_ToastUI.Set("배너 광고 준비 중입니다.");
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면 destroy
bannerVisible = false;
}
// ===========================================================
// 업데이트 - 광고 결과 처리
// ===========================================================
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");
// 초기화 완료 후 광고 미리 로드 (인터스티셜 + 리워드)
Advertisement.Load(placement_Interstitial, this);
Advertisement.Load(placement_Rewarded, this);
// 배너도 미리 로드하되, 자동 노출은 하지 않음
LoadBanner();
}
public void OnInitializationFailed(UnityAdsInitializationError error, string message)
{
Debug.LogError($"❌ Unity Ads Init Failed: {error} - {message}");
}
// ===========================================================
// 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}");
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;
// 광고 시청 후 즉시 다음 광고 미리 로드
Advertisement.Load(placementId, this);
}
public void OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message)
{
Debug.LogError($"❌ Show failed {placementId}: {error} - {message}");
GetReward = 2;
// 실패했어도 다음 광고 미리 로드 시도
Advertisement.Load(placementId, this);
}
public void OnUnityAdsShowStart(string placementId) { }
public void OnUnityAdsShowClick(string placementId) { }
}