Advertisement
AlezM

StorageController

Sep 25th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.18 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Text;
  8. using UnityEngine;
  9. using Firebase;
  10. using Firebase.Storage;
  11.  
  12. public class StorageController : MonoBehaviour {
  13.    
  14.     FirebaseStorage storage;
  15.     protected static string MyStorageBucket = "gs://atrlife-fa361.appspot.com/";
  16.     protected static string UriFileScheme = Uri.UriSchemeFile + "://";
  17.     protected string persistentDataPath;
  18.  
  19.     void Start () {
  20.         persistentDataPath = Application.persistentDataPath;
  21.         Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
  22.             var dependencyStatus = task.Result;
  23.             if (dependencyStatus == Firebase.DependencyStatus.Available) {
  24.                 storage = FirebaseStorage.DefaultInstance;
  25.             } else {
  26.                 UnityEngine.Debug.LogError(System.String.Format(
  27.                 "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
  28.             }
  29.         });
  30.     }
  31.  
  32.     protected StorageReference GetStorageReference(string storageFileName) {
  33.         var storageLocation = MyStorageBucket + storageFileName;
  34.  
  35.         // If this is an absolute path including a bucket create a storage instance.
  36.         if (storageLocation.StartsWith("gs://") ||
  37.             storageLocation.StartsWith("http://") ||
  38.             storageLocation.StartsWith("https://"))
  39.         {
  40.             var storageUri = new Uri(storageLocation);
  41.             var firebaseStorage = FirebaseStorage.GetInstance(
  42.             String.Format("{0}://{1}", storageUri.Scheme, storageUri.Host));
  43.             return firebaseStorage.GetReferenceFromUrl(storageLocation);
  44.         }
  45.         // When using relative paths use the default storage instance which uses the bucket supplied
  46.         // on creation of FirebaseApp.
  47.         return FirebaseStorage.DefaultInstance.GetReference(storageLocation);
  48.     }
  49.  
  50.     protected virtual string PathToPersistentDataPathUriString(string filename) {
  51.         if (filename.StartsWith(UriFileScheme)) {
  52.             return filename;
  53.         }
  54.         return String.Format("{0}{1}/{2}", UriFileScheme, persistentDataPath, filename);
  55.     }
  56.  
  57.     protected string DownloadToFile(string localFileName) {
  58.         var storageReference = GetStorageReference(localFileName);
  59.         var localFileNameUriString = PathToPersistentDataPathUriString(localFileName);
  60.        
  61.         if (File.Exists(localFileNameUriString))
  62.         {
  63.             return localFileNameUriString;
  64.         }
  65.  
  66.         var task = storageReference.GetFileAsync(
  67.             localFileNameUriString,
  68.             new Firebase.Storage.StorageProgress<DownloadState>((DownloadState state) => {
  69.                 Debug.Log(String.Format(
  70.                 "Progress: {0} of {1} bytes transferred.",
  71.                 state.BytesTransferred,
  72.                 state.TotalByteCount
  73.                 ));
  74.             }),
  75.             CancellationToken.None);
  76.  
  77.  
  78.         task.ContinueWith(resultTask => {
  79.             if (!resultTask.IsFaulted && !resultTask.IsCanceled) {
  80.                 Debug.Log("Download finished.");
  81.             }
  82.             else {
  83.                 localFileNameUriString = null;
  84.             }
  85.         });
  86.  
  87.         return localFileNameUriString;
  88.     }
  89.  
  90.     public Texture GetImage(string imageName)
  91.     {
  92.         string imagePath = DownloadToFile(imageName);
  93.  
  94.         return LoadTextureFromFile(imagePath);
  95.     }
  96.  
  97.     Texture2D LoadTextureFromFile(string path)
  98.     {
  99.         Texture2D image = new Texture2D(1, 1);
  100.         byte[] bytes;
  101.         bytes = File.ReadAllBytes(path);
  102.  
  103.         image.LoadImage(bytes);
  104.  
  105.         return image;
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement