728x90
using Firebase;
using Firebase.Extensions;
using UnityEngine;
using Firebase.Auth;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class FirebaseManager : MonoBehaviour
{
FirebaseAuth auth;
public Toggle autoLoginToggle;
void Start()
{
FirebaseInit();
}
void FirebaseInit()
{
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
{
FirebaseApp app = FirebaseApp.DefaultInstance;
auth = FirebaseAuth.DefaultInstance;
Debug.Log($"초기화함");
CheckAutoLogin();
});
}
// 자동 로그인 확인
void CheckAutoLogin()
{
string autoLoginYn = PlayerPrefs.GetString("autoLoginYn");
// 저장된 UID가 있고, 자동로그인 체크했을때
if (!string.IsNullOrEmpty(autoLoginYn) && autoLoginYn == "Y")
{
Debug.Log($"자동로그인 시도");
PerformAnonymousLogin();
}
}
public void PerformAnonymousLogin()
{
auth.SignInAnonymouslyAsync().ContinueWithOnMainThread(task =>
{
if (task.IsCompleted && !task.IsFaulted && !task.IsCanceled)
{
AuthResult authResult = task.Result;
FirebaseUser user = authResult.User;
bool toggleValue = autoLoginToggle.isOn;
Debug.Log("익명 로그인 성공!");
Debug.Log("사용자 UID: " + user.UserId);
Debug.Log("Toggle의 현재 값: " + toggleValue);
if(toggleValue){
PlayerPrefs.SetString("autoLoginYn","Y");
PlayerPrefs.SetString("userID", user.UserId);
}else{
PlayerPrefs.SetString("autoLoginYn","N");
}
SceneManager.LoadScene("HomeScene");
}
else
{
Debug.LogError("익명 로그인 실패: " + task.Exception);
}
});
}
}