185 lines
		
	
	
		
			6.9 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			185 lines
		
	
	
		
			6.9 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System.Collections;
 | ||
| using System.Collections.Generic;
 | ||
| using Firebase.RemoteConfig;
 | ||
| using GoogleMobileAds.Api;
 | ||
| using GoogleMobileAds.Common;
 | ||
| using Unity.VisualScripting;
 | ||
| using UnityEngine;
 | ||
| 
 | ||
| namespace WZ
 | ||
| {
 | ||
|     public class AdsSplashManager : D_MonoSingleton<AdsSplashManager>
 | ||
|     {
 | ||
|         public float backgroundTime = 0;
 | ||
|         private int timeoutDuration = 5;
 | ||
|         private bool _coldLaunch = true;
 | ||
|         public void InitSplash()
 | ||
|         {
 | ||
|             timeoutDuration = GetSplashConfigItem().loadtime;
 | ||
|             StartCoroutine(LoadSplashAdAdWithTimeout());
 | ||
|             LoggerUtils.Debug(" [AppOpen]  加载开屏:"+timeoutDuration);
 | ||
|         }
 | ||
| 
 | ||
|         private void OnApplicationPause(bool pauseStatus)
 | ||
|         {
 | ||
|             if (!pauseStatus)    //从后台返回前台
 | ||
|             {
 | ||
|                 LoggerUtils.Debug(" [AppOpen]  进入前台");
 | ||
|                 if (AdmobAdsManager.Instance._splashLoaded)
 | ||
|                 { 
 | ||
|                     StartCoroutine(DelayedAction());
 | ||
|                 }
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
|                 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();
 | ||
|                     }
 | ||
|                 }
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         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 = 15 };
 | ||
|             }
 | ||
|             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;
 | ||
|         }
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| 
 |