Snaparazzi/Assets/Scripts/FirebaseInitializer.cs

64 lines
1.8 KiB
C#
Raw Normal View History

2024-01-26 22:51:40 +00:00
using Firebase;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2024-01-27 11:24:47 +00:00
using System;
2024-01-26 22:51:40 +00:00
public class FirebaseInitializer : MonoBehaviour
{
2024-01-27 11:24:47 +00:00
public Action onFirebaseReady;
2024-01-26 22:51:40 +00:00
public static FirebaseInitializer Instance;
void Awake()
{
Instance = this;
}
// Start is called before the first frame update
void Start()
{
2024-01-27 11:24:47 +00:00
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
2024-01-26 22:51:40 +00:00
{
var dependencyStatus = task.Result;
2024-01-27 11:24:47 +00:00
if (dependencyStatus == DependencyStatus.Available)
2024-01-26 22:51:40 +00:00
{
// Create and hold a reference to your FirebaseApp,
// where app is a Firebase.FirebaseApp property of your application class.
2024-01-27 11:24:47 +00:00
var app = FirebaseApp.DefaultInstance;
Authenticate();
2024-01-26 22:51:40 +00:00
// Set a flag here to indicate whether Firebase is ready to use by your app.
}
else
{
2024-01-27 11:24:47 +00:00
Debug.LogError($"Could not resolve all Firebase dependencies: {dependencyStatus}");
2024-01-26 22:51:40 +00:00
}
});
}
// Update is called once per frame
2024-01-27 11:24:47 +00:00
void Authenticate()
2024-01-26 22:51:40 +00:00
{
2024-01-27 11:24:47 +00:00
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
2024-01-26 22:51:40 +00:00
2024-01-27 11:24:47 +00:00
auth.SignInAnonymouslyAsync().ContinueWith(task =>
{
if (task.IsCanceled)
{
Debug.LogError("SignInAnonymouslyAsync was canceled.");
return;
}
if (task.IsFaulted)
{
Debug.LogError("SignInAnonymouslyAsync encountered an error: " + task.Exception);
return;
}
Firebase.Auth.AuthResult result = task.Result;
Debug.Log($"User signed in successfully: {result.User.UserId}");
onFirebaseReady?.Invoke();
});
2024-01-26 22:51:40 +00:00
}
}