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

204 lines
7.2 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;
using UnityEngine;
namespace WZ
{
public class AdsSplashManager : MonoBehaviour
{
public static AdsSplashManager Instance { get; private set; }
public float backgroundTime = 0;
private int timeoutDuration = 5;
private bool _coldLaunch = true;
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}
Instance = this;
DontDestroyOnLoad(gameObject);
}
public void InitSplash()
{
timeoutDuration = GetSplashConfigItem().loadtime;
StartCoroutine(LoadSplashAdAdWithTimeout());
}
private void OnAppStateChanged(AppState state)
{
if (_coldLaunch)
{
_coldLaunch = false;
return;
}
if (state == AppState.Foreground)
{
LoggerUtils.Debug("[AppOpen] 进入前台");
CheckSplashAdlash(false);
}
else if (state == AppState.Background)
{
backgroundTime = Time.realtimeSinceStartup;
LoggerUtils.Debug("[AppOpen] 进入后台 " + backgroundTime);
}
}
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;
}
if (!AdsSDKManager.Instance.IsSplashAvailable())
{
LoggerUtils.Debug(" [AppOpen] 热启动 广告是否准备好:");
AdsSDKManager.Instance.LoadSplashAd();
return;
}
// 展示热启动开屏
AdsSDKManager.Instance.ShowSplashAd();
}
else
{
// 冷启动
// 首次启动
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
{
AdsSDKManager.Instance.OnSplashAdCloseCallback?.Invoke();
AdsSDKManager.Instance.LoadSplashAd();
LoggerUtils.Debug(" [AppOpen] 冷启动广告未准备好");
}
}
}
else
{
// 非首次启动
if (coldSplashSwitch)
{
// 展示冷启动
if (AdsSDKManager.Instance.IsSplashAvailable())
{
AdsSDKManager.Instance.ShowSplashAd();
}
else
{
AdsSDKManager.Instance.OnSplashAdCloseCallback?.Invoke();
AdsSDKManager.Instance.LoadSplashAd();
}
}
else
{
AdsSDKManager.Instance.OnSplashAdCloseCallback?.Invoke();
}
}
AppStateEventNotifier.AppStateChanged += OnAppStateChanged;
}
}
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)
{
return new SplashConfigItem() { firstcold_splash_switch = 1, cold_splash_switch = 1, hot_splash_switch = 1, hot_timegap = 30, loadtime = 5 };
}
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;
}
}
}