Snaparazzi/Assets/Scripts/StorageManager.cs
2024-01-27 23:47:06 +01:00

66 lines
1.7 KiB
C#

using Firebase.Database;
using Firebase.Storage;
using System;
using System.Threading.Tasks;
using UnityEngine;
public class StorageManager : MonoBehaviour
{
public GameObject PicturePlayer;
public GameObject Canvas;
private StorageReference storage;
private DatabaseReference realtimeDB;
void Awake()
{
Debug.Log("non");
FirebaseInitializer.Instance.onFirebaseReady += Initialize;
}
void Initialize()
{
Debug.Log("oui");
FirebaseInitializer.Instance.onFirebaseReady -= Initialize;
storage = FirebaseStorage.DefaultInstance.RootReference;
realtimeDB = FirebaseDatabase.DefaultInstance.RootReference;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void UploadPhoto()
{
Texture2D photo = PicturePlayer.GetComponent<CameraManager>().GetPhoto();
byte[] photoBytes = ImageConversion.EncodeToJPG(photo);
GameManager game = Canvas.GetComponent<GameManager>();
string imageUuid = Guid.NewGuid().ToString();
StorageReference imageRef = storage.Child(game.myRoom.code).Child(game.currentPlayer.id).Child($"{imageUuid}.jpg");
imageRef.PutBytesAsync(photoBytes).ContinueWith((Task<StorageMetadata> task) =>
{
if (task.IsFaulted || task.IsCanceled)
{
Debug.LogException(task.Exception);
// Uh-oh, an error occurred!
}
else
{
// Metadata contains file metadata such as size, content-type, and md5hash.
StorageMetadata metadata = task.Result;
}
});
}
}