157 lines
4.8 KiB
C#
157 lines
4.8 KiB
C#
using System;
|
||
using System.Threading.Tasks;
|
||
using Kill.Utils;
|
||
using UnityEngine;
|
||
|
||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||
using Firebase;
|
||
using Firebase.Auth;
|
||
#endif
|
||
|
||
namespace Kill.Managers
|
||
{
|
||
/// <summary>
|
||
/// Firebase 谷歌登录管理器
|
||
/// 使用 Firebase 内置的 Google 登录支持
|
||
/// </summary>
|
||
public class FirebaseAuthManager : MonoBehaviour
|
||
{
|
||
public static FirebaseAuthManager Instance { get; private set; }
|
||
|
||
// Google 登录配置 - 从 Firebase 控制台获取的 Web 客户端 ID
|
||
private const string WEB_CLIENT_ID = "785438724947-kpjbqi43hbj6eddianbjsgkgkkclkfmd.apps.googleusercontent.com";
|
||
|
||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||
private FirebaseAuth auth;
|
||
#endif
|
||
|
||
private void Awake()
|
||
{
|
||
Instance = this;
|
||
InitializeFirebase();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化 Firebase
|
||
/// </summary>
|
||
private void InitializeFirebase()
|
||
{
|
||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
|
||
{
|
||
var dependencyStatus = task.Result;
|
||
if (dependencyStatus == DependencyStatus.Available)
|
||
{
|
||
auth = FirebaseAuth.DefaultInstance;
|
||
Debug.Log("Firebase 初始化成功");
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError($"Firebase 初始化失败: {dependencyStatus}");
|
||
}
|
||
});
|
||
#else
|
||
Debug.Log("Firebase 谷歌登录仅在 Android 平台可用");
|
||
#endif
|
||
}
|
||
|
||
/// <summary>
|
||
/// 谷歌登录
|
||
/// 使用 Firebase 内置的 Google 登录弹窗
|
||
/// </summary>
|
||
public void SignInWithGoogle(Action<GoogleLoginResult> onSuccess, Action<string> onError)
|
||
{
|
||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||
if (auth == null)
|
||
{
|
||
onError?.Invoke("Firebase 未初始化");
|
||
return;
|
||
}
|
||
|
||
// 获取 Google 登录 Provider
|
||
var provider = new FederatedOAuthProviderData("google.com");
|
||
provider.Scopes = new string[] { "email", "profile" };
|
||
|
||
var federatedProvider = new FederatedOAuthProvider(provider);
|
||
|
||
auth.SignInWithProviderAsync(federatedProvider).ContinueWith(task =>
|
||
{
|
||
if (task.IsFaulted)
|
||
{
|
||
MainThread.Enqueue(() => onError?.Invoke("Google 登录失败"));
|
||
return;
|
||
}
|
||
|
||
if (task.IsCanceled)
|
||
{
|
||
MainThread.Enqueue(() => onError?.Invoke("Google 登录被取消"));
|
||
return;
|
||
}
|
||
|
||
var user = auth.CurrentUser;
|
||
if (user == null)
|
||
{
|
||
MainThread.Enqueue(() => onError?.Invoke("获取用户信息失败"));
|
||
return;
|
||
}
|
||
|
||
// 构造登录结果
|
||
MainThread.Enqueue(() =>
|
||
{
|
||
var result = new GoogleLoginResult
|
||
{
|
||
FirebaseUserId = user.UserId,
|
||
Email = user.Email,
|
||
DisplayName = user.DisplayName,
|
||
PhotoUrl = user.PhotoUrl?.ToString(),
|
||
IdToken = ""
|
||
};
|
||
onSuccess?.Invoke(result);
|
||
});
|
||
});
|
||
#else
|
||
// 编辑器环境下模拟
|
||
Task.Delay(1000).ContinueWith(_ =>
|
||
{
|
||
Debug.Log("编辑器模拟谷歌登录");
|
||
MainThread.Enqueue(() =>
|
||
{
|
||
var mockResult = new GoogleLoginResult
|
||
{
|
||
FirebaseUserId = "mock_firebase_user_id_12345",
|
||
Email = "mock@gmail.com",
|
||
DisplayName = "Mock User",
|
||
PhotoUrl = "",
|
||
IdToken = ""
|
||
};
|
||
onSuccess?.Invoke(mockResult);
|
||
});
|
||
});
|
||
#endif
|
||
}
|
||
|
||
/// <summary>
|
||
/// 谷歌登出
|
||
/// </summary>
|
||
public void SignOut()
|
||
{
|
||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||
auth?.SignOut();
|
||
Debug.Log("谷歌登出成功");
|
||
#endif
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 谷歌登录结果
|
||
/// </summary>
|
||
public class GoogleLoginResult
|
||
{
|
||
public string FirebaseUserId; // Firebase 用户ID(用于绑定)
|
||
public string Email; // 邮箱
|
||
public string DisplayName; // 显示名称
|
||
public string PhotoUrl; // 头像URL
|
||
public string IdToken; // ID Token
|
||
}
|
||
}
|