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
|
|
|
|
{
|
|
|
|
public GameObject PicturePlayer;
|
|
|
|
public GameObject Canvas;
|
|
|
|
private FirebaseStorage 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;
|
|
|
|
storage = FirebaseStorage.DefaultInstance;
|
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()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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 storageRef = storage.GetReferenceFromUrl("gs://ggj2024-5cb41.appspot.com");
|
|
|
|
StorageReference imageRef = storageRef.Child(game.myRoom.code).Child(game.currentPlayer.id).Child($"{imageUuid}.jpg");
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|