Snaparazzi/Assets/Scripts/StorageManager.cs

63 lines
1.7 KiB
C#
Raw Normal View History

2024-01-27 20:07:49 +00:00
using Firebase.Database;
2024-01-27 18:58:02 +00:00
using Firebase.Storage;
using System;
using System.Threading.Tasks;
using UnityEngine;
public class StorageManager : MonoBehaviour
{
2024-01-27 22:47:06 +00:00
private StorageReference storage;
2024-01-27 20:07:49 +00:00
private DatabaseReference realtimeDB;
2024-01-27 18:58:02 +00:00
void Awake()
{
FirebaseInitializer.Instance.onFirebaseReady += Initialize;
}
void Initialize()
{
FirebaseInitializer.Instance.onFirebaseReady -= Initialize;
2024-01-27 22:47:06 +00:00
storage = FirebaseStorage.DefaultInstance.RootReference;
2024-01-27 20:07:49 +00:00
realtimeDB = FirebaseDatabase.DefaultInstance.RootReference;
2024-01-27 18:58:02 +00:00
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
2024-01-28 01:40:12 +00:00
public void UploadPhoto(string roomCode, string playerId, string questionRef, string propRef)
2024-01-27 18:58:02 +00:00
{
Texture2D photo = gameObject.GetComponent<CameraManager>().GetPhoto();
2024-01-27 18:58:02 +00:00
byte[] photoBytes = ImageConversion.EncodeToJPG(photo);
2024-01-28 00:16:53 +00:00
GameManager game = gameObject.GetComponent<GameManager>();
2024-01-27 18:58:02 +00:00
string imageUuid = Guid.NewGuid().ToString();
2024-01-27 23:32:01 +00:00
StorageReference imageRef = storage.Child($"{game.myRoom.code}/{game.currentPlayer.id}/{imageUuid}.png");
2024-01-27 18:58:02 +00:00
imageRef.PutBytesAsync(photoBytes).ContinueWith((Task<StorageMetadata> task) =>
{
if (task.IsFaulted || task.IsCanceled)
{
2024-01-27 20:07:49 +00:00
Debug.LogException(task.Exception);
2024-01-27 18:58:02 +00:00
// Uh-oh, an error occurred!
}
else
{
// Metadata contains file metadata such as size, content-type, and md5hash.
StorageMetadata metadata = task.Result;
2024-01-28 00:16:53 +00:00
2024-01-27 18:58:02 +00:00
}
});
}
}