Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Text;
- using UnityEngine;
- using Firebase;
- using Firebase.Storage;
- public class StorageController : MonoBehaviour {
- FirebaseStorage storage;
- protected static string MyStorageBucket = "gs://atrlife-fa361.appspot.com/";
- protected static string UriFileScheme = Uri.UriSchemeFile + "://";
- protected string persistentDataPath;
- void Start () {
- persistentDataPath = Application.persistentDataPath;
- Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
- var dependencyStatus = task.Result;
- if (dependencyStatus == Firebase.DependencyStatus.Available) {
- storage = FirebaseStorage.DefaultInstance;
- } else {
- UnityEngine.Debug.LogError(System.String.Format(
- "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
- }
- });
- }
- protected StorageReference GetStorageReference(string storageFileName) {
- var storageLocation = MyStorageBucket + storageFileName;
- // If this is an absolute path including a bucket create a storage instance.
- if (storageLocation.StartsWith("gs://") ||
- storageLocation.StartsWith("http://") ||
- storageLocation.StartsWith("https://"))
- {
- var storageUri = new Uri(storageLocation);
- var firebaseStorage = FirebaseStorage.GetInstance(
- String.Format("{0}://{1}", storageUri.Scheme, storageUri.Host));
- return firebaseStorage.GetReferenceFromUrl(storageLocation);
- }
- // When using relative paths use the default storage instance which uses the bucket supplied
- // on creation of FirebaseApp.
- return FirebaseStorage.DefaultInstance.GetReference(storageLocation);
- }
- protected virtual string PathToPersistentDataPathUriString(string filename) {
- if (filename.StartsWith(UriFileScheme)) {
- return filename;
- }
- return String.Format("{0}{1}/{2}", UriFileScheme, persistentDataPath, filename);
- }
- protected string DownloadToFile(string localFileName) {
- var storageReference = GetStorageReference(localFileName);
- var localFileNameUriString = PathToPersistentDataPathUriString(localFileName);
- if (File.Exists(localFileNameUriString))
- {
- return localFileNameUriString;
- }
- var task = storageReference.GetFileAsync(
- localFileNameUriString,
- new Firebase.Storage.StorageProgress<DownloadState>((DownloadState state) => {
- Debug.Log(String.Format(
- "Progress: {0} of {1} bytes transferred.",
- state.BytesTransferred,
- state.TotalByteCount
- ));
- }),
- CancellationToken.None);
- task.ContinueWith(resultTask => {
- if (!resultTask.IsFaulted && !resultTask.IsCanceled) {
- Debug.Log("Download finished.");
- }
- else {
- localFileNameUriString = null;
- }
- });
- return localFileNameUriString;
- }
- public Texture GetImage(string imageName)
- {
- string imagePath = DownloadToFile(imageName);
- return LoadTextureFromFile(imagePath);
- }
- Texture2D LoadTextureFromFile(string path)
- {
- Texture2D image = new Texture2D(1, 1);
- byte[] bytes;
- bytes = File.ReadAllBytes(path);
- image.LoadImage(bytes);
- return image;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement