Snaparazzi/Assets/Scripts/FirebaseInitializer.cs

65 lines
1.9 KiB
C#

using Firebase;
using System.Collections;
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
IEnumerable 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}");
}
});
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
}
// 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();
});
}
}