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) {