2024-01-27 20:07:49 +00:00
|
|
|
using Firebase.Database;
|
2024-01-27 18:58:02 +00:00
|
|
|
using Firebase.Storage;
|
|
|
|
using System;
|
2024-01-28 16:07:54 +00:00
|
|
|
using System.Collections;
|
2024-01-27 18:58:02 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using UnityEngine;
|
2024-01-28 16:07:54 +00:00
|
|
|
using UnityEngine.Networking;
|
2024-01-27 18:58:02 +00:00
|
|
|
|
|
|
|
public class StorageManager : MonoBehaviour
|
|
|
|
{
|
2024-01-28 15:36:37 +00:00
|
|
|
public CameraManager cameraManager;
|
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 14:42:32 +00:00
|
|
|
public void UploadPhoto(string roomCode, string playerId, int question, int proposition)
|
2024-01-27 18:58:02 +00:00
|
|
|
{
|
2024-01-28 15:36:37 +00:00
|
|
|
Texture2D photo = cameraManager.GetPhoto();
|
2024-01-27 18:58:02 +00:00
|
|
|
byte[] photoBytes = ImageConversion.EncodeToJPG(photo);
|
|
|
|
string imageUuid = Guid.NewGuid().ToString();
|
|
|
|
|
2024-01-28 16:47:38 +00:00
|
|
|
StorageReference imageRef = storage.Child($"gs://ggj2024-5cb41.appspot.com/{roomCode}/{playerId}/{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
|
|
|
|
{
|
2024-01-28 12:25:17 +00:00
|
|
|
realtimeDB
|
|
|
|
.Child("rooms")
|
|
|
|
.Child(roomCode)
|
|
|
|
.Child("questions")
|
|
|
|
.Child(question.ToString())
|
|
|
|
.Child("propositions")
|
2024-01-28 14:42:32 +00:00
|
|
|
.Child(proposition.ToString())
|
2024-01-28 12:25:17 +00:00
|
|
|
.Child("photoUrl")
|
|
|
|
.SetValueAsync(imageRef.Path);
|
2024-01-27 18:58:02 +00:00
|
|
|
}
|
|
|
|
});
|
2024-01-28 16:07:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerator DownloadImage(string _url, Action<Texture> callback_OnTextureDownloaded)
|
|
|
|
{
|
|
|
|
UnityWebRequest request = UnityWebRequestTexture.GetTexture(_url);
|
|
|
|
yield return request.SendWebRequest();
|
2024-01-27 18:58:02 +00:00
|
|
|
|
2024-01-28 16:07:54 +00:00
|
|
|
if (request.result != UnityWebRequest.Result.Success)
|
|
|
|
{
|
|
|
|
Debug.LogError(request.error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
callback_OnTextureDownloaded?.Invoke(((DownloadHandlerTexture)request.downloadHandler).texture);
|
|
|
|
}
|
2024-01-27 18:58:02 +00:00
|
|
|
}
|
|
|
|
}
|