Load Assets From Firebase Storage — Unity

Farrukh Sajjad
3 min readMay 15, 2022

--

There are way too many options to load assets dynamically from cloud for your unity project. Some of them are:

  • Using CCD (Cloud Content Delivery) provided by Unity Game Services
  • Firebase Storage
  • Using Google Drive

But here we will discuss how to load the assets from the firebase storage inside your unity game and give the players new content without them updating the game.

load assets from firebase storage unity

Note: As firebase services are free, they do have a limit of bandwidth and storage capacity limits per project. So you might want to consider your usage and then opt for the solution that suits you the best. But if you have a small casual game, the free bandwidth and storage might be enough for you.

Step-1: Connect your project with Firebase

Connet your unity project with Firebase. There are tons of articles on how to do so you might want to follow any of those. Recommended is to follow the google’s official doc. Read this to understand how you can connect your project with firebase. Click Here.

Step-2: Create Asset Bundles for your assets/prefabs

  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);}}

Note: Make your that this script resides inside an Editor folder.

2. Select your prefabs/assets and set their asset bundle label to “levels”.

3. Create Asset Bundles. Select your assets/prefabs again and select “Create Asset Bundle”

load assets from firebase storage unity

This will create your assets bundle in the path you provided in your editor script.

Step-3: Upload the levels file to firebase storage

Upload the levels file to the firebase storage.

Step-4: Create a firebase script to download this file

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;}}}

This will download the levels file from firebase storage to your persistentDataPath.

Step-5: Load the assets from the downloaded file

Now you just have to get the file’s reference and load the assets from the downloaded file.

  1. Create a list of game objects which will contain the loaded assets.
public List<GameObject> dowloadedAssets = new List<GameObject>();

2. Get the file’s reference.

//Get the file from the directory firstvar file = Directory.GetFiles(Application.persistentDataPath);var assetBundle = AssetBundle.LoadFromFile(file[0]);

3. Load the assets from the asset bundle.

var loadedAssets = assetBundle.LoadAllAssets();for (int i = 0; i < loadedAssets.Length; i++){dowloadedAssets[i] = loadedAssets[i] as GameObject;}

Load the assets from the asset bundle and populate the list of game objects.

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;}
}

That’s all folks. Now you just have to create more levels, make their asset bundles and update the file on the firebase storage!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Farrukh Sajjad
Farrukh Sajjad

Written by Farrukh Sajjad

Noob Game Dev | Back to posting weekly

No responses yet

Write a response