Snaparazzi/Assets/Scripts/FirebaseInitializer.cs

92 lines
2.8 KiB
C#
Raw Normal View History

2024-01-26 22:51:40 +00:00
using Firebase;
2024-01-27 18:58:02 +00:00
using Firebase.Auth;
2024-01-27 11:24:47 +00:00
using System;
2024-01-27 18:58:02 +00:00
using UnityEngine;
2024-01-27 13:51:34 +00:00
using UnityEngine.Android;
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
2024-01-27 13:51:34 +00:00
void Start()
2024-01-26 22:51:40 +00:00
{
2024-01-27 13:51:34 +00:00
AskForCameraAuthorizationForAndroid();
2024-01-27 11:31:58 +00:00
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
2024-01-26 22:51:40 +00:00
{
var dependencyStatus = task.Result;
2024-01-27 11:31:58 +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:31:58 +00:00
var app = FirebaseApp.DefaultInstance;
2024-01-27 11:24:47 +00:00
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 18:58:02 +00:00
FirebaseAuth 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;
}
2024-01-27 18:58:02 +00:00
AuthResult result = task.Result;
2024-01-27 11:24:47 +00:00
Debug.Log($"User signed in successfully: {result.User.UserId}");
onFirebaseReady?.Invoke();
});
2024-01-26 22:51:40 +00:00
}
2024-01-27 13:51:34 +00:00
private void AskForCameraAuthorizationForAndroid()
{
if (Permission.HasUserAuthorizedPermission(Permission.Microphone))
{
// The user authorized use of the microphone.
}
else
{
bool useCallbacks = false;
if (!useCallbacks)
{
// We do not have permission to use the microphone.
// Ask for permission or proceed without the functionality enabled.
Permission.RequestUserPermission(Permission.Microphone);
}
else
{
var callbacks = new PermissionCallbacks();
//callbacks.PermissionDenied += PermissionCallbacks_PermissionDenied;
//callbacks.PermissionGranted += PermissionCallbacks_PermissionGranted;
//callbacks.PermissionDeniedAndDontAskAgain += PermissionCallbacks_PermissionDeniedAndDontAskAgain;
Permission.RequestUserPermission(Permission.Microphone, callbacks);
}
}
}
2024-01-26 22:51:40 +00:00
}