From 99e461590d2a127596815b88e406df75c701664f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=E8=99=9E=E6=B8=A0=E6=88=90=E2=80=9D?= <“yuqucheng2006@qq.com”> Date: Thu, 24 Sep 2026 10:16:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=9B=BD=E5=86=85?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E5=90=AF=E5=8A=A8=E9=97=AA=E9=80=80=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=8C=E7=A6=81=E7=94=A8=20Firebase=20=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改 FirebaseAuthManager 改为懒加载,仅在用户触发登录时初始化 Firebase 移除 AndroidManifest 中 Firebase 相关自动初始化的 provider 关闭 MainScene 的服务器后门开关,提升安全性 --- Assets/Plugins/Android/AndroidManifest.xml | 11 ++- Assets/Scenes/MainScene.unity | 2 +- .../Scripts/Managers/FirebaseAuthManager.cs | 85 ++++++++++++++++--- 3 files changed, 81 insertions(+), 17 deletions(-) diff --git a/Assets/Plugins/Android/AndroidManifest.xml b/Assets/Plugins/Android/AndroidManifest.xml index e85a95c..b59e5f9 100644 --- a/Assets/Plugins/Android/AndroidManifest.xml +++ b/Assets/Plugins/Android/AndroidManifest.xml @@ -1,7 +1,5 @@  - + @@ -39,6 +37,13 @@ + + + + + + diff --git a/Assets/Scenes/MainScene.unity b/Assets/Scenes/MainScene.unity index 246d066..ef67db3 100644 --- a/Assets/Scenes/MainScene.unity +++ b/Assets/Scenes/MainScene.unity @@ -485,7 +485,7 @@ MonoBehaviour: - https://nextreal.cn/photon-matrix-api - https://api.photonmatrixlab.com serverBaseUrl: https://api.photonmatrixlab.com - enableServerBackdoor: 1 + enableServerBackdoor: 0 backdoorTapCount: 5 backdoorTapInterval: 1 defaultTimeout: 30 diff --git a/Assets/Scripts/Managers/FirebaseAuthManager.cs b/Assets/Scripts/Managers/FirebaseAuthManager.cs index c7e6c2e..01651d0 100644 --- a/Assets/Scripts/Managers/FirebaseAuthManager.cs +++ b/Assets/Scripts/Managers/FirebaseAuthManager.cs @@ -36,7 +36,9 @@ namespace Kill.Managers private void Awake() { Instance = this; - InitializeFirebase(); + // 不在此处初始化 Firebase。 + // 国内无 Google 服务的设备上,启动即初始化 Firebase 会闪退, + // 因此改为懒加载:在真正用到 Firebase(谷歌/苹果登录)时才初始化。 InitializeAppleAuth(); } @@ -45,30 +47,74 @@ namespace Kill.Managers appleAuthManager?.Update(); } + // Firebase 是否已初始化完成 + private bool firebaseReady; + // 初始化是否已开始(防止并发重复初始化) + private bool firebaseInitStarted; + // 等待初始化完成的回调队列 + private readonly List pendingReady = new List(); + /// - /// 初始化 Firebase + /// 懒加载:确保 Firebase 已初始化,未初始化则先初始化,完成后在主线程回调 onReady。 /// - private void InitializeFirebase() + private void EnsureFirebaseReady(Action onReady) + { + if (onReady == null) return; + + if (firebaseReady) + { + onReady(); + return; + } + if (firebaseInitStarted) + { + pendingReady.Add(onReady); + return; + } + + firebaseInitStarted = true; + pendingReady.Add(onReady); + StartFirebaseInit(); + } + + private void StartFirebaseInit() { #if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => { var dependencyStatus = task.Result; - if (dependencyStatus == DependencyStatus.Available) + MainThread.Enqueue(() => { - auth = FirebaseAuth.DefaultInstance; - Debug.Log("Firebase 初始化成功"); - } - else - { - Debug.LogError($"Firebase 初始化失败: {dependencyStatus}"); - } + firebaseReady = dependencyStatus == DependencyStatus.Available; + if (firebaseReady) + { + auth = FirebaseAuth.DefaultInstance; + Debug.Log("Firebase 初始化成功"); + } + else + { + Debug.LogError($"Firebase 初始化失败: {dependencyStatus},第三方登录不可用"); + } + FlushPending(); + }); }); #else - Debug.Log("Firebase 第三方登录仅在 Android / iOS 平台可用"); + FlushPending(); #endif } + /// + /// 回调所有等待初始化完成的调用 + /// + private void FlushPending() + { + var callbacks = pendingReady.ToArray(); + pendingReady.Clear(); + firebaseInitStarted = false; + foreach (var cb in callbacks) + cb?.Invoke(); + } + /// /// 初始化 Apple Sign In /// @@ -90,7 +136,11 @@ namespace Kill.Managers /// public void SignInWithGoogle(Action onSuccess, Action onError, int timeoutSeconds = 30) { - SignInWithProvider("google.com", new string[] { "email", "profile" }, onSuccess, onError, timeoutSeconds); + // 懒加载:仅在用户主动点谷歌登录时才初始化 Firebase,未初始化则先初始化 + EnsureFirebaseReady(() => + { + SignInWithProvider("google.com", new string[] { "email", "profile" }, onSuccess, onError, timeoutSeconds); + }); } /// @@ -98,6 +148,15 @@ namespace Kill.Managers /// public void SignInWithApple(Action onSuccess, Action onError, int timeoutSeconds = 30) { + // 懒加载:与谷歌登录同理,iOS 上 Firebase 也按需初始化 + EnsureFirebaseReady(() => SignInWithAppleInternal(onSuccess, onError)); + } + + /// + /// 苹果登录真正实现(须在 Firebase 初始化完成后调用) + /// + private void SignInWithAppleInternal(Action onSuccess, Action onError) + { #if UNITY_IOS && !UNITY_EDITOR if (appleAuthManager == null) {