SDK_UnityMoney/Assets/Script/SDKManager/AdsSDKManager/AdsSplashManager.cs

194 lines
7.4 KiB
C#
Raw Normal View History

2025-09-01 10:32:50 +00:00
using System.Collections;
using System.Collections.Generic;
using Firebase.RemoteConfig;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;
2025-09-02 13:42:07 +00:00
using Unity.VisualScripting;
2025-09-01 10:32:50 +00:00
using UnityEngine;
namespace WZ
{
2025-09-01 12:53:08 +00:00
public class AdsSplashManager : D_MonoSingleton<AdsSplashManager>
2025-09-01 10:32:50 +00:00
{
public float backgroundTime = 0;
private int timeoutDuration = 5;
private bool _coldLaunch = true;
public void InitSplash()
{
timeoutDuration = GetSplashConfigItem().loadtime;
StartCoroutine(LoadSplashAdAdWithTimeout());
2025-09-02 13:42:07 +00:00
LoggerUtils.Debug(" [AppOpen] 加载开屏:"+timeoutDuration);
2025-09-01 10:32:50 +00:00
}
2025-09-02 13:42:07 +00:00
private void OnApplicationPause(bool pauseStatus)
2025-09-01 10:32:50 +00:00
{
2025-09-02 13:42:07 +00:00
if (!pauseStatus) //从后台返回前台
2025-09-01 10:32:50 +00:00
{
2025-09-02 13:42:07 +00:00
LoggerUtils.Debug(" [AppOpen] 进入前台");
if (AdmobAdsManager.Instance._splashLoaded)
{
StartCoroutine(DelayedAction());
}
2025-09-01 10:32:50 +00:00
}
2025-09-02 13:42:07 +00:00
else
2025-09-01 10:32:50 +00:00
{
backgroundTime = Time.realtimeSinceStartup;
2025-09-02 13:42:07 +00:00
LoggerUtils.Debug(" [AppOpen] 进入后台 " + backgroundTime);
2025-09-01 10:32:50 +00:00
}
}
private IEnumerator LoadSplashAdAdWithTimeout()
{
// 开始加载广告
AdsSDKManager.Instance.LoadSplashAd();
// 等待5秒或直到广告加载完成
float elapsedTime = 0f;
while (elapsedTime < timeoutDuration && !AdsSDKManager.Instance.IsSplashAvailable())
{
elapsedTime += Time.deltaTime;
yield return null;
}
CheckSplashAdlash(true);
}
IEnumerator DelayedAction()
{
yield return new WaitForSeconds(1f);
CheckSplashAdlash(false);
}
private void CheckSplashAdlash(bool isCold)
{
if (!isCold)
{
// 热启动
// 当前有其他类型广告在展示
LoggerUtils.Debug(" [AppOpen] 热启动 是否有广告在展示:" + AdsSDKManager.Instance.otherAdsOnShow);
if (AdsSDKManager.Instance.otherAdsOnShow) return;
// 热启动开关
var hotSplashSwitch = GetSplashConfigItem().hot_splash_switch == 1;
LoggerUtils.Debug(" [AppOpen] 热启动 开关:" + hotSplashSwitch);
if (!hotSplashSwitch)
{
return;
}
// 后台运行时间
float resultTime = Time.realtimeSinceStartup - backgroundTime;
float onlineTime = GetSplashConfigItem().hot_timegap;
LoggerUtils.Debug(" [AppOpen] 后台运行时间差值:" + resultTime + " 在线参数值:" + onlineTime);
if (resultTime < onlineTime)
{
return;
}
2025-09-04 09:19:05 +00:00
Invoke(nameof(TrackAdImpression), 10);
2025-09-01 10:32:50 +00:00
if (!AdsSDKManager.Instance.IsSplashAvailable())
{
LoggerUtils.Debug(" [AppOpen] 热启动 广告是否准备好:");
2025-09-05 02:15:32 +00:00
AdsActionEvents.TrackAdFailToShow(AdsType.Splash, "", AdsShowFailType.NoFill);
2025-09-01 10:32:50 +00:00
AdsSDKManager.Instance.LoadSplashAd();
return;
}
// 展示热启动开屏
AdsSDKManager.Instance.ShowSplashAd();
}
else
{
// 冷启动
// 首次启动
2025-09-04 09:19:05 +00:00
Invoke(nameof(TrackAdImpression), 10);
2025-09-01 10:32:50 +00:00
var isNew = PlayerPrefsUtils.GetPlayerPrefsInt("Firstcold_Splash_Switch", 0) == 0;
// 首次冷启动开关
var isFirstShow = GetSplashConfigItem().firstcold_splash_switch == 1;
// 冷启动开关
var coldSplashSwitch = GetSplashConfigItem().cold_splash_switch == 1;
LoggerUtils.Debug(" [AppOpen] 冷启动 开关:" + coldSplashSwitch + " 首次启动是否展示开屏:" + isFirstShow + " 新用户:" + isNew);
// 新用户首次启动
if (isNew)
{
PlayerPrefsUtils.SavePlayerPrefsInt("Firstcold_Splash_Switch", 1);
if (!isFirstShow)
{
// 新用户首次不展示
AdsSDKManager.Instance.OnSplashAdCloseCallback?.Invoke();
}
else
{
// 新用户首次展示
if (AdsSDKManager.Instance.IsSplashAvailable())
{
AdsSDKManager.Instance.ShowSplashAd();
}
else
{
2025-09-05 02:15:32 +00:00
AdsActionEvents.TrackAdFailToShow(AdsType.Splash, "", AdsShowFailType.NoFill);
2025-09-01 10:32:50 +00:00
AdsSDKManager.Instance.OnSplashAdCloseCallback?.Invoke();
AdsSDKManager.Instance.LoadSplashAd();
LoggerUtils.Debug(" [AppOpen] 冷启动广告未准备好");
}
}
}
else
{
// 非首次启动
if (coldSplashSwitch)
{
// 展示冷启动
if (AdsSDKManager.Instance.IsSplashAvailable())
{
AdsSDKManager.Instance.ShowSplashAd();
}
else
{
2025-09-05 02:15:32 +00:00
AdsActionEvents.TrackAdFailToShow(AdsType.Splash, "", AdsShowFailType.NoFill);
2025-09-01 10:32:50 +00:00
AdsSDKManager.Instance.OnSplashAdCloseCallback?.Invoke();
AdsSDKManager.Instance.LoadSplashAd();
}
}
else
{
AdsSDKManager.Instance.OnSplashAdCloseCallback?.Invoke();
}
}
}
}
2025-09-04 09:19:05 +00:00
private void TrackAdImpression()
{
AdsActionEvents.TrackAdPosition(AdsType.Splash,"");
}
2025-09-01 10:32:50 +00:00
private static string Splash_AD_RULES = "Splash_AD_RULES";
private static string Splash_AD_RULES_DEFAULT_VALUE = "[{\"firstcold_splash_switch\":1,\"cold_splash_switch\":1,\"hot_splash_switch\":1,\"hot_timegap\":30,\"loadtime\":5}]";
public SplashConfigItem GetSplashConfigItem()
{
string jsonData = FireBaseRemoteConfigManager.Instance.GetRemoteConfigString(Splash_AD_RULES, Splash_AD_RULES_DEFAULT_VALUE);
SplashConfigItem[] configItems = DataUtils.FromJsonArray<SplashConfigItem>(jsonData);
if (configItems.Length == 0 || configItems == null)
{
2025-09-02 13:42:07 +00:00
return new SplashConfigItem() { firstcold_splash_switch = 1, cold_splash_switch = 1, hot_splash_switch = 1, hot_timegap = 30, loadtime = 15 };
2025-09-01 10:32:50 +00:00
}
else
{
return configItems[0];
}
}
[System.Serializable]
public class SplashConfigItem
{
public int firstcold_splash_switch;
public int cold_splash_switch;
public int hot_splash_switch;
public int hot_timegap;
public int loadtime;
}
}
}