Snaparazzi/Assets/Scripts/FirebaseInitializer.cs
2024-01-27 12:24:47 +01:00

64 lines
1.8 KiB
C#

using Firebase;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class FirebaseInitializer : MonoBehaviour
{
public Action onFirebaseReady;
public static FirebaseInitializer Instance;
void Awake()
{
Instance = this;
}
// Start is called before the first frame update
void Start()
{
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
{
var dependencyStatus = task.Result;
if (dependencyStatus == DependencyStatus.Available)
{
// Create and hold a reference to your FirebaseApp,
// where app is a Firebase.FirebaseApp property of your application class.
var app = FirebaseApp.DefaultInstance;
Authenticate();
// Set a flag here to indicate whether Firebase is ready to use by your app.
}
else
{
Debug.LogError($"Could not resolve all Firebase dependencies: {dependencyStatus}");
}
});
}
// Update is called once per frame
void Authenticate()
{
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
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();
});
}
}