Load Assets From Firebase Storage — Unity

  • Using CCD (Cloud Content Delivery) provided by Unity Game Services
  • Firebase Storage
  • Using Google Drive
load assets from firebase storage unity
  1. Create an Editor Script to save your asset bundle to your desired location.
using System.IO;using UnityEditor;public class CreateAssetBundles{[MenuItem("Assets/Build Assets Bundles")]static void BuildAssetBundle(){string assetBundleDirectory = "Assets/AssetBundles";if (!Directory.Exists(assetBundleDirectory)){Directory.CreateDirectory(assetBundleDirectory);}BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, BuildTarget.Android);}}
load assets from firebase storage unity
using Firebase;using Firebase.Extensions;using Firebase.Storage;using UnityEngine;
public class FirebaseController : MonoBehaviour{private void Awake(){DownloadLevels();}private void DownloadLevels(){var app = FirebaseApp.DefaultInstance;var storage = FirebaseStorage.DefaultInstance;var storageReference = storage.GetReferenceFromUrl("gs://sports-car-parking-sim.appspot.com/Levels/levels");try{var applicationPersistentPath = $"{Application.persistentDataPath}/levels";//Start downloading the levels filevar task = storageReference.GetFileAsync(applicationPersistentPath);task.ContinueWithOnMainThread(resultTask =>{if (!resultTask.IsFaulted && !resultTask.IsCanceled){Debug.Log("Download Finished");}});}catch (System.Exception){throw;}}}
  1. Create a list of game objects which will contain the loaded assets.
public List<GameObject> dowloadedAssets = new List<GameObject>();
//Get the file from the directory firstvar file = Directory.GetFiles(Application.persistentDataPath);var assetBundle = AssetBundle.LoadFromFile(file[0]);
var loadedAssets = assetBundle.LoadAllAssets();for (int i = 0; i < loadedAssets.Length; i++){dowloadedAssets[i] = loadedAssets[i] as GameObject;}
private void GetTheAssetsFromDownloadedFile(){//Get the file from the directory firstvar file = Directory.GetFiles(Application.persistentDataPath);var assetBundle = AssetBundle.LoadFromFile(file[0]);var loadedAssets = assetBundle.LoadAllAssets();for (int i = 0; i < loadedAssets.Length; i++){dowloadedAssets[i] = loadedAssets[i] as GameObject;}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Farrukh Sajjad

Farrukh Sajjad

Noob Game Dev | Back to posting weekly