SDK_UnityMoney/Assets/Script/SDKManager/TransferManager/TransferManager.cs

395 lines
14 KiB
C#
Raw Normal View History

2026-01-05 09:09:25 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace WZ
{
public class TransferManager : D_MonoSingleton<TransferManager>
{
private AdInitState currentState = AdInitState.Idle;
private bool isFirebaseActivated = false;
private Coroutine waitingCoroutine = null;
2026-01-05 10:07:42 +00:00
private int admobRetryCount = 0;
private const int MAX_ADMOB_RETRY_COUNT = 5;
private const float ADMOB_RETRY_INTERVAL = 30f;
private Coroutine admobRetryCoroutine = null;
private bool isRetryingAdMob = false;
2026-01-07 09:48:24 +00:00
private bool _afterLoading = false;
private int _level = 0;
2026-01-05 09:09:25 +00:00
// 状态枚举
public enum AdInitState
{
Idle, // 空闲
WaitingForAdjustCallback, // 等待Adjust回调
WaitingForFirebase, // 等待Firebase激活
CheckingPromotion, // 检查推广状态
InitializingTransfer, // 初始化Transfer SDK
CheckingVpnPermission, // 检查VPN权限
RequestingVpnPermission, // 请求VPN权限
CheckingAdMobAvailability, // 检查AdMob可用性
InitializingAdMob, // 初始化AdMob
InitializingTopOn, // 初始化TopOn
Completed, // 完成
2026-01-05 10:07:42 +00:00
Failed, // 失败
RetryingAdMobCheck
}
2026-01-07 09:48:24 +00:00
public void StartAdInitialization(bool afterLoading,int level)
2026-01-05 09:09:25 +00:00
{
Debug.Log("收到Adjust回调开始处理广告初始化");
2026-01-07 09:48:24 +00:00
_afterLoading = afterLoading;
_level = level;
2026-01-05 09:09:25 +00:00
// 开始广告初始化流程
StartAdInitialization();
}
private void StartAdInitialization()
{
if (currentState != AdInitState.WaitingForAdjustCallback)
{
Debug.LogWarning($"广告初始化状态不正确,当前状态: {currentState}");
return;
}
// 检查Firebase状态
if (isFirebaseActivated)
{
Debug.Log("Firebase已激活直接执行广告初始化");
ExecuteAdInitialization();
}
else
{
Debug.Log("Firebase未激活等待激活后再执行广告初始化");
ChangeState(AdInitState.WaitingForFirebase, "等待Firebase激活");
// 开始等待Firebase激活
waitingCoroutine = StartCoroutine(WaitForFirebaseAndInitialize());
}
}
/// <summary>
/// 等待Firebase激活并初始化广告
/// </summary>
private IEnumerator WaitForFirebaseAndInitialize()
{
Debug.Log("开始等待Firebase激活...");
float startTime = Time.time;
const float timeout = 360f;
// 等待Firebase激活
while (!isFirebaseActivated && Time.time - startTime < timeout)
{
yield return new WaitForSeconds(0.5f);
// 检查Firebase状态可能是外部初始化的
CheckFirebaseStatus();
if (Time.time - startTime >= 5f && !isFirebaseActivated)
{
Debug.Log($"已等待{Time.time - startTime:F1}秒Firebase仍未激活");
}
}
if (isFirebaseActivated)
{
Debug.Log($"Firebase激活成功等待时间: {Time.time - startTime:F1}秒");
ExecuteAdInitialization();
}
else
{
Debug.LogWarning($"Firebase激活等待超时{timeout}秒),继续初始化广告");
ExecuteAdInitialization();
}
waitingCoroutine = null;
}
/// <summary>
/// 执行广告初始化流程
/// </summary>
private void ExecuteAdInitialization()
{
Debug.Log("开始执行广告初始化流程");
StartCoroutine(AdInitializationCoroutine());
}
/// <summary>
/// 广告初始化协程
/// </summary>
private IEnumerator AdInitializationCoroutine()
{
// 状态1: 检查是否为推广用户
ChangeState(AdInitState.CheckingPromotion, "检查推广用户状态");
if (!IsPromoteUser())
{
Debug.Log("非推广用户直接初始化TopOn广告");
ChangeState(AdInitState.InitializingTopOn, "初始化TopOn广告");
InitializeTopOnAds();
yield break;
}
Debug.Log("推广用户开始VPN和AdMob检查流程");
// 状态2: 初始化Transfer SDK
ChangeState(AdInitState.InitializingTransfer, "初始化Transfer SDK");
TransferAndroidClass.Init();
// 状态3: 检查VPN权限
ChangeState(AdInitState.CheckingVpnPermission, "检查VPN权限");
if (TransferData.HasVpnPermission)
{
Debug.Log("已有VPN权限检查AdMob可用性");
yield return StartCoroutine(CheckAndInitializeAdsWithVpnPermission());
}
else
{
Debug.Log("无VPN权限检查是否已请求过权限");
yield return StartCoroutine(HandleNoVpnPermission());
}
}
2026-01-06 08:31:14 +00:00
public static float checkAdmobStart;
2026-01-05 09:09:25 +00:00
/// <summary>
/// 已有VPN权限时的广告初始化流程
/// </summary>
private IEnumerator CheckAndInitializeAdsWithVpnPermission()
{
ChangeState(AdInitState.CheckingAdMobAvailability, "检查AdMob可用性");
bool checkCompleted = false;
bool isAdMobAllowed = false;
string message = "";
2026-01-06 08:31:14 +00:00
checkAdmobStart = Time.time;
RushSDKManager.Instance.LogEvent("isAllowAdMob_check_start");
2026-01-05 09:09:25 +00:00
TransferAndroidClass.IsAllowAdMob((res, msg) =>
{
isAdMobAllowed = res;
message = msg;
checkCompleted = true;
2026-01-06 08:31:14 +00:00
RushSDKManager.Instance.LogEvent(string.Format("isAllowAdMob_{0}",checkCompleted),new Dictionary<string, object>()
{
{"pass_time",(int)(Time.time-checkAdmobStart)},
{"reason",msg}
});
2026-01-05 09:09:25 +00:00
});
// 等待检查完成
yield return new WaitUntil(() => checkCompleted);
if (isAdMobAllowed)
{
Debug.Log($"AdMob可用: {message}初始化AdMob广告");
ChangeState(AdInitState.InitializingAdMob, "初始化AdMob广告");
InitializeAdMobAds();
}
else
{
Debug.Log($"AdMob不可用: {message}初始化TopOn广告");
ChangeState(AdInitState.InitializingTopOn, "初始化TopOn广告");
InitializeTopOnAds();
2026-01-05 10:07:42 +00:00
2026-01-07 09:48:24 +00:00
if (TransferData.HasVpnPermission)
2026-01-05 10:07:42 +00:00
{
Debug.Log("权限引导流程已完成且AdMob检查失败启动重试机制");
StartAdMobRetry();
}
2026-01-05 09:09:25 +00:00
}
}
/// <summary>
/// 处理无VPN权限的情况
/// </summary>
private IEnumerator HandleNoVpnPermission()
{
// 已请求过VPN权限但用户拒绝
2026-01-07 09:48:24 +00:00
if (TransferData.HasShowedReqVpnPermissionView)
2026-01-05 09:09:25 +00:00
{
Debug.Log("已请求过VPN权限但用户拒绝初始化TopOn广告");
ChangeState(AdInitState.InitializingTopOn, "初始化TopOn广告");
InitializeTopOnAds();
yield break;
}
// 首次请求VPN权限
Debug.Log("首次请求VPN权限弹出引导");
ChangeState(AdInitState.RequestingVpnPermission, "请求VPN权限");
bool permissionRequestCompleted = false;
bool permissionGranted = false;
2026-01-07 09:48:24 +00:00
var vpnGuide = new VPNGuideManager();
vpnGuide.ShowConnectionRequest(_afterLoading, _level, (res, msg) =>
2026-01-05 09:09:25 +00:00
{
permissionGranted = res;
permissionRequestCompleted = true;
Debug.Log($"VPN权限请求结果: {res}, 消息: {msg}");
});
// 等待权限请求完成
yield return new WaitUntil(() => permissionRequestCompleted);
if (permissionGranted)
{
Debug.Log("VPN权限已授予更新权限状态并检查AdMob");
TransferData.HasVpnPermission = true;
yield return StartCoroutine(CheckAndInitializeAdsWithVpnPermission());
}
else
{
Debug.Log("VPN权限被拒绝初始化TopOn广告");
ChangeState(AdInitState.InitializingTopOn, "初始化TopOn广告");
InitializeTopOnAds();
2026-01-05 10:07:42 +00:00
2026-01-05 09:09:25 +00:00
}
}
2026-01-05 10:07:42 +00:00
private void StartAdMobRetry()
{
if (isRetryingAdMob)
{
Debug.Log("AdMob重试已在运行中");
return;
}
if (admobRetryCount >= MAX_ADMOB_RETRY_COUNT)
{
Debug.Log($"已达到最大重试次数({MAX_ADMOB_RETRY_COUNT}),停止重试");
return;
}
Debug.Log("启动AdMob重试机制");
isRetryingAdMob = true;
admobRetryCoroutine = StartCoroutine(AdMobRetryCoroutine());
}
private IEnumerator AdMobRetryCoroutine()
{
// 第一次立即检查
admobRetryCount = 0;
while (isRetryingAdMob && admobRetryCount < MAX_ADMOB_RETRY_COUNT)
{
Debug.Log($"第{admobRetryCount + 1}次AdMob检查");
ChangeState(AdInitState.RetryingAdMobCheck, $"第{admobRetryCount + 1}次检查AdMob");
bool checkCompleted = false;
bool isAdMobAllowed = false;
string message = "";
2026-01-06 08:31:14 +00:00
RushSDKManager.Instance.LogEvent("isAllowAdMob_check_start");
checkAdmobStart = Time.time;
2026-01-05 10:07:42 +00:00
TransferAndroidClass.IsAllowAdMob((res, msg) =>
{
isAdMobAllowed = res;
message = msg;
checkCompleted = true;
2026-01-06 08:31:14 +00:00
RushSDKManager.Instance.LogEvent(string.Format("isAllowAdMob_{0}",checkCompleted),new Dictionary<string, object>()
2026-01-07 09:48:24 +00:00
{
{"pass_time",(int)(Time.time-checkAdmobStart)},
{"reason",msg}
});
});
2026-01-05 10:07:42 +00:00
// 等待检查完成
yield return new WaitUntil(() => checkCompleted);
if (isAdMobAllowed)
{
Debug.Log($"第{admobRetryCount + 1}次AdMob检查成功: {message}");
StopAdMobRetry();
InitializeAdMobAds();
break;
}
else
{
Debug.Log($"第{admobRetryCount + 1}次AdMob检查失败: {message}");
admobRetryCount++;
// 达到最大重试次数
if (admobRetryCount >= MAX_ADMOB_RETRY_COUNT)
{
Debug.Log($"达到最大重试次数({MAX_ADMOB_RETRY_COUNT}),停止重试");
StopAdMobRetry();
break;
}
// 等待30秒后再次尝试
Debug.Log($"等待{ADMOB_RETRY_INTERVAL}秒后重试AdMob检查");
yield return new WaitForSeconds(ADMOB_RETRY_INTERVAL);
}
}
}
private void StopAdMobRetry()
{
isRetryingAdMob = false;
if (admobRetryCoroutine != null)
{
StopCoroutine(admobRetryCoroutine);
admobRetryCoroutine = null;
}
}
2026-01-05 09:09:25 +00:00
private void InitializeAdMobAds()
{
2026-01-05 10:07:42 +00:00
StopAdMobRetry();
2026-01-05 09:09:25 +00:00
AdmobAdsManager.Instance.Initialize();
}
private void InitializeTopOnAds()
{
TpnAdsManager.Instance.Initialize();
}
private void ChangeState(AdInitState newState, string message)
{
var oldState = currentState;
currentState = newState;
Debug.Log($"广告初始化状态: {oldState} -> {newState} ({message})");
}
private void CheckFirebaseStatus()
{
if(FireBaseRemoteConfigManager.Instance.IsInitialized)
{
Debug.Log("Firebase激活完成事件收到");
isFirebaseActivated = true;
// 如果正在等待Firebase继续广告初始化
if (currentState == AdInitState.WaitingForFirebase && waitingCoroutine != null)
{
// 停止等待协程,直接继续
if (waitingCoroutine != null)
{
StopCoroutine(waitingCoroutine);
waitingCoroutine = null;
}
Debug.Log("从Firebase等待状态恢复继续广告初始化");
ExecuteAdInitialization();
}
}
}
public bool IsPromoteUser()
{
return !AdjustManager.Instance.IsOrganic(AdjustNetwork.GetNetwork());
}
}
}