Shegotwet/Assets/Scripts/AttachToGameObject/ADInfo.cs

159 lines
4.5 KiB
C#

using System;
using UnityEngine;
using UnityEngine.Advertisements;
public class ADInfo : MonoBehaviourSingletonTemplate<ADInfo>,
IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
{
#if UNITY_ANDROID
string gameId = "5947579";
#elif UNITY_IOS
string gameId = "5947578";
#endif
bool testMode = false;
string placement_Interstitial = "Interstitial_Android";
string placement_Rewarded = "Rewarded_Android";
string placement_Banner = "Banner_Android";
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;
}
Advertisement.Load(placement_Interstitial, this);
}
else
{
Advertisement.Load(placement_Rewarded, this);
}
}
public void Set_Banner(bool active)
{
return;
if (active)
{
if (!SaveMgr.Ins.Get_ShopNoAD())
{
Advertisement.Banner.Load(placement_Banner, new BannerLoadOptions
{
loadCallback = () =>
{
Debug.Log("Banner Load Success");
Advertisement.Banner.SetPosition(BannerPosition.BOTTOM_CENTER);
Advertisement.Banner.Show(placement_Banner);
},
errorCallback = (err) =>
{
Debug.Log("Banner Load Fail: " + err);
}
});
}
}
else
{
Advertisement.Banner.Hide();
}
}
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();
LobbyUI.Ins.m_ToastUI.Set("준비된 광고가 없습니다.\n나중에 다시 시도해 주세요.");
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 Initialization Complete");
}
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);
Advertisement.Show(placementId, this);
}
public void OnUnityAdsFailedToLoad(string placementId, UnityAdsLoadError error, string message)
{
Debug.LogError($"Failed to load Ad {placementId}: {error} - {message}");
GetReward = 2;
}
// ===========================================================
// IUnityAdsShowListener
// ===========================================================
public void OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState)
{
if (placementId == placement_Rewarded && showCompletionState == UnityAdsShowCompletionState.COMPLETED)
GetReward = 1;
else
GetReward = 2;
}
public void OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message)
{
Debug.LogError($"Show failed {placementId}: {error} - {message}");
GetReward = 2;
}
public void OnUnityAdsShowStart(string placementId) { }
public void OnUnityAdsShowClick(string placementId) { }
}