using Firebase.Database; using Firebase.Storage; using System; using System.Collections; using System.Threading.Tasks; using UnityEngine; using UnityEngine.Networking; public class StorageManager : MonoBehaviour { public CameraManager cameraManager; private StorageReference storage; private DatabaseReference realtimeDB; void Awake() { FirebaseInitializer.Instance.onFirebaseReady += Initialize; } void Initialize() { 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(string roomCode, string playerId, int question, int proposition) { Texture2D photo = cameraManager.GetPhoto(); byte[] photoBytes = ImageConversion.EncodeToJPG(photo); string imageUuid = Guid.NewGuid().ToString(); StorageReference imageRef = storage.Child($"gs://ggj2024-5cb41.appspot.com/{roomCode}/{playerId}/{imageUuid}.png"); imageRef.PutBytesAsync(photoBytes).ContinueWith((Task task) => { if (task.IsFaulted || task.IsCanceled) { Debug.LogException(task.Exception); // Uh-oh, an error occurred! } else { realtimeDB .Child("rooms") .Child(roomCode) .Child("questions") .Child(question.ToString()) .Child("propositions") .Child(proposition.ToString()) .Child("photoUrl") .SetValueAsync(imageRef.Path); } }); } public IEnumerator DownloadImage(string _url, Action callback_OnTextureDownloaded) { UnityWebRequest request = UnityWebRequestTexture.GetTexture(_url); yield return request.SendWebRequest(); if (request.result != UnityWebRequest.Result.Success) { Debug.LogError(request.error); } else { callback_OnTextureDownloaded?.Invoke(((DownloadHandlerTexture)request.downloadHandler).texture); } } }