Files
back_cantanBuilding/Assets/Scripts/Services/AggAdsService.cs
2026-05-26 16:15:54 +08:00

243 lines
7.8 KiB
C#

using asap.core;
using GameCore;
using System;
using System.Threading.Tasks;
using UniRx;
namespace game
{
public class AdsResult
{
public AdvertPopupType Type;
/// <summary>
/// //0-成功 1-失败 2 -点击 3-关闭
/// </summary>
public int Result;
}
public enum AdvertPopupType
{
None = 0,
Shop = 1,
Turntable = 2,
Showdown = 3,
FishingFail = 4,
Chest = 5,
Aquarium = 6,
}
public class AggAdsService : IAdsService
{
#if UNITY_IOS
private static string AD_Unit_ID = "ec3c8fd688cdb5e2";
#else
private static string AD_Unit_ID = "c6c49a100de8496c";
#endif
private static AdvertPopupType currentADType = AdvertPopupType.None;
private static int retryTime = 0;
private ISettingService settings;
CompositeDisposable disposables;
public AggAdsService(ISettingService settings)
{
this.settings = settings;
}
public void Init()
{
#if UNITY_IOS
MaxSdkiOS.SetExtraParameter("disable_audio_session" , "true");
#endif
MaxSdkCallbacks.OnSdkInitializedEvent += (MaxSdkBase.SdkConfiguration sdkConfiguration) =>
{
UnityEngine.Debug.Log($"[MaxSdk::OnSdkInitializedEvent] Init:{sdkConfiguration.IsSuccessfullyInitialized}");
};
IConfig config = GContext.container.Resolve<IConfig>();
bool debug = !string.IsNullOrEmpty(config.Get("DEBUG", string.Empty));
MaxSdk.SetVerboseLogging(debug);
MaxSdk.InitializeSdk();
MaxSdkCallbacks.Rewarded.OnAdLoadedEvent += OnRewardedAdLoadedEvent;
MaxSdkCallbacks.Rewarded.OnAdLoadFailedEvent += OnRewardedAdLoadFailedEvent;
MaxSdkCallbacks.Rewarded.OnAdDisplayedEvent += OnRewardedAdDisplayedEvent;
MaxSdkCallbacks.Rewarded.OnAdClickedEvent += OnRewardedAdClickedEvent;
MaxSdkCallbacks.Rewarded.OnAdRevenuePaidEvent += OnRewardedAdRevenuePaidEvent;
MaxSdkCallbacks.Rewarded.OnAdHiddenEvent += OnRewardedAdHiddenEvent;
MaxSdkCallbacks.Rewarded.OnAdDisplayFailedEvent += OnRewardedAdFailedToDisplayEvent;
MaxSdkCallbacks.Rewarded.OnAdReceivedRewardEvent += OnRewardedAdReceivedRewardEvent;
// levl 40 , first build scene completed
disposables?.Dispose();
disposables = new CompositeDisposable();
RootCtx.OnEvent<OnLvChangeEvent>()
.Where(evt => evt.lvl >= 40)
.Subscribe(OnLevelChange)
.AddTo(disposables);
RootCtx.OnEvent<ExchangeFishCount>()
.Where(evt => evt.FishCount >= 17)
.Subscribe(OnExchangeFishCount)
.AddTo(disposables);
// Load AD after 40 Lvl
//if (!MaxSdk.IsRewardedAdReady(AD_Unit_ID))
//{
//MaxSdk.LoadRewardedAd(AD_Unit_ID);
//using (var e = GEvent.GameEvent("ad_request")) { }
//}
}
public void ShowRewarded(AdvertPopupType advertPopupType = AdvertPopupType.Shop)
{
if (advertPopupType == AdvertPopupType.None) return;
currentADType = advertPopupType;
using (var e = GEvent.GameEvent("ad_click"))
{
e.AddContent("ad_placement_id", (int)advertPopupType);
}
if (MaxSdk.IsRewardedAdReady(AD_Unit_ID))
{
MaxSdk.ShowRewardedAd(AD_Unit_ID, advertPopupType.ToString());
}
else
{
GContext.Publish(new AdsResult() { Result = 1, Type = advertPopupType });
using (var e = GEvent.GameEvent("ad_show"))
{
e.AddContent("ad_show_state", 3);
e.AddContent("ad_placement_id", (int)currentADType);
}
ToastPanel.Show(LocalizationMgr.GetText("UI_ToastPanel_27"));
}
}
private bool loadFired = false;
private void OnLevelChange(OnLvChangeEvent evt)
{
LoadFired();
}
public void OnExchangeFishCount(ExchangeFishCount evt)
{
LoadFired();
}
void LoadFired()
{
if (!loadFired && !MaxSdk.IsRewardedAdReady(AD_Unit_ID))
{
disposables?.Dispose();
disposables = null;
loadFired = true;
MaxSdk.LoadRewardedAd(AD_Unit_ID);
using (var e = GEvent.GameEvent("ad_request")) { }
}
}
private void ChangeAudio(bool adShowing)
{
ISoundService soundService = GContext.container.Resolve<ISoundService>();
ISettingService settingService = GContext.container.Resolve<ISettingService>();
int vol = adShowing ? 0 : 80;
if (soundService != null && settingService != null)
{
if (settingService.Music)
{
soundService.BGMVol = vol;
}
if (settingService.Sound)
{
soundService.SFXVol = vol;
soundService.VoiceVol = vol;
}
}
}
#region AppLovin Callbacks
private void OnRewardedAdLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
{
retryTime = 0;
using (var e = GEvent.GameEvent("ad_response"))
{
e.AddContent("ad_loading", 1);
}
}
private async void OnRewardedAdLoadFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo)
{
using (var e = GEvent.GameEvent("ad_response"))
{
e.AddContent("ad_loading", 0);
}
retryTime++;
int retryDelay = Math.Min(6, retryTime) * 2000;
UnityEngine.Debug.LogWarning($"[AggAdsService] OnRewardedAdLoadFailedEvent retry {retryDelay}, {adUnitId}, {errorInfo}");
await Task.Delay(retryDelay);
MaxSdk.LoadRewardedAd(adUnitId);
using (var e = GEvent.GameEvent("ad_request")) { }
}
private void OnRewardedAdDisplayedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
{
using (var e = GEvent.GameEvent("ad_show"))
{
e.AddContent("ad_show_state", 1);
e.AddContent("ad_placement_id", (int)currentADType);
}
ChangeAudio(true);
}
private void OnRewardedAdFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo, MaxSdkBase.AdInfo adInfo)
{
// Rewarded ad failed to display. AppLovin recommends that you load the next ad.
GContext.Publish(new AdsResult() { Result = 1, Type = currentADType });
MaxSdk.LoadRewardedAd(adUnitId);
using (var e = GEvent.GameEvent("ad_show"))
{
e.AddContent("ad_show_state", 2);
e.AddContent("ad_placement_id", (int)currentADType);
}
}
private void OnRewardedAdClickedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { }
private void OnRewardedAdHiddenEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
{
// Rewarded ad is hidden. Pre-load the next ad
MaxSdk.LoadRewardedAd(adUnitId);
ChangeAudio(false);
}
private void OnRewardedAdReceivedRewardEvent(string adUnitId, MaxSdk.Reward reward, MaxSdkBase.AdInfo adInfo)
{
GContext.Publish(new AdsResult() { Result = 0, Type = currentADType });
}
private void OnRewardedAdRevenuePaidEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
{
GEvent.ADRevenueEvent(adUnitId, adInfo);
}
#endregion
}
}