Unity + Addressables + Firebase + Google Cloud Storage

Mike Bauer
9 min readOct 20, 2020

I wanted to take a little time today and put together a small guide for other developers who like me, would like to publish data driven mobile games developed in Unity which used Firebase and the Cloud. I have grown fond of Google’s server tools for all of my back-end needs so we will be using Firebase and Google Cloud to supply our data. My goal is to keep this short and to the point, covering only the key elements required to connect Unity to Firebase, store Addressable content via Google Cloud, and pull that content back into your deployed game.

Thankfully, a recent release of the Firebase SDK: 6.16.0 and the latest version of Unity: 2020.1.7f1 have removed quite a bit of workarounds that were needed over the past few months.

Let me be clear that this guide is about piecing together the bigger puzzle of getting all of these tools working together. For example, for the authentication, I simply pass the email and password directly to Firebase auth, I don’t use a form as that is way outside of the scope of this guide.

I assume you are comfortable with setting up a Firebase account, Google Cloud, and how to setup Unity to generate the APK including the SHA1, so let’s get to the procedure that matters.

Here’s the github in case you want it: https://github.com/bauervision/FirebaseAuthApp

Unity: Scene setup

So in my Unity scene I have a simple UI with 3 distinct screens:

Initializer: will display if there are any core issues with running Firebase on the device.

Login: displays when the app is in the process of logging in and will also display any errors that may occur.

FirebaseLoaded: is the final screen which will display data retrieved from the Realtime Database and essentially equal our running game.

I also have an audio file set as the AudioClip in the AudioSource component of my main camera; this will be our addressable asset for this project.

Unity: Installing Firebase

While the latest versions of both Unity and Firebase cleaned up a lot of issues, I have found that its best to just install Firebase directly from Unity, via the Package Manager. However, you will not find Firebase as an option to install by default.

To enable it, add this to the bottom of your `Packages/manifest.json`

I also recommend this method, as the current Firebase SDK is over 5gbs, no need to download all that!

Open your Package Manager in Unity, and change the packages you view from Unity Registries, to My Registries, and then go ahead install Firebase App (Core), first! Once that is successful, feel free to install any of the others you require. For this lesson, we just need Auth and Database.

Note: External Dependency Manager will get installed with Firebase App (Core).

Unity: Addressables

Since you are in the Package Manager, might as well go ahead and get the Addressables installed. Switch back from My Registry, to Unity Registry in the Package Manager, and simply install the Addressables package.

The main thing I’m going for here is to use my 40mb audio file, as an Addressable asset so that I don’t have to include that in my APK. Of course, the point here is that this can be anything else you want to exclude from your install to save on app bloat.

All I did was select on my audio file, and check the addressable option, I changed nothing else about this file.

Now that we have the file we want set as an addressable, we need to make a few more changes, but we are almost done!

First open Addressable Profiles, and in the dialog that opens, top left corner, Create -> Profile. This will add a new profile to the list, then you can right click on the Profile Name and choose Rename Profile to name it what you like. I’m going with Firebase for the name.

Right click on the profile name once more and choose Set Active.

We will come back to Profiles in a second, let’s jump over to Addressable Groups:

Window -> Asset Management -> Addressables ->Groups

The first thing you will see is that Default Local Group now contains your addressable audio asset, and the Profile will automatically show the profile you just set as active.

For now, we can leave this right as it is, but feel free to make your own group if you’d like. I will recommend that you create a label, because labels are your friend once you get a ton of these assets in your project.

Open the Label drop down and scroll down to the bottom of the list which contains only a default label right now, click on Manage Labels and add your own label as you see fit: I’m going with Audio.

Google Cloud Storage

I went with Cloud storage over Firebase storage because Firebase requires a token for access whereas Cloud does not.

Create a Bucket

Inside your Google Cloud Dashboard, create a new Storage Bucket for your game under the Resources widget. Select Resources, which will load the Storage Browser, and then choose Create Bucket.

My preference is to have my Project be my game company, and then buckets for each of my games.

In the bucket creation steps, the key things I recommend setting are:

· Location = Multi-region

· Storage Class = Standard

· Control Access = Uniform

· Advanced Settings: Encryption = Google Managed Key

Unless you desire it, you can skip checking a retention policy for the bucket as well as a label.

With your new bucket in place, select it from the Storage browser, and switch to the Permissions tab. Find the Permissions option to Add a new permission, and in the add members dialog, type in and add allUsers.

For the Role, you want to choose Cloud Storage / Storage Object Viewer. No specific condition is needed.

Obviously, you want to choose Allow Public Access when prompted to make this repo publicly available.

Get the Public Link

The last thing we need from Cloud for now is the actual link to this bucket. To get this however, you will need a folder and a file.

Switch to the Objects Tab and Create Folder. Name it something relevant to your game. This is the directory you will put the files created from Unity.

Now, we need to put at least one file in here so we can get the actual public path that we need to use.

Note: this is NOT the link you want: https://console.cloud.google.com/storage/browser/...

You may find that this URL works, but I have seen it work and not work, so feel free to experiment if you’d like.

I recommend honestly picking any super small file to upload here, literally can be anything as it is going to be immediately deleted: I’m going with a jpg.

You do this so that you can get the public access link shown below.

Copy that link and you will see that it looks like this:

https://storage.googleapis.com/<yourBucketName>/<theFileYouUploaded>

If you want to mimic what Unity produces you should make this folder structure look like this:

https://storage.googleapis.com/<yourBucketName>/ServerData/Android

Or whatever build option you are using!

Unity Final Setup

With that public link copied, we just need to use it inside of our addressable settings. Back in Unity, load up your Addressable Profile, and paste the link inside of the RemoteLoadPath like so:

Note: you need to leave the /[BuildTarget] tacked on to the end.

So we have a Group and a Profile, as well as a link to our storage bucket where we will pull our addressable assets into our game.

All that is left now is to create a build of our current addressable assets.

Inside Addressable Groups, find Build -> New Build -> Default Build Script

This will create a new ServerData folder outside of your Assets folder which contains everything you need. All you need to do is push this folder to your Cloud bucket.

Push Data to Storage Bucket

Go to back to your Cloud console, and the bucket you created. Make a new Folder inside of the files directory, preferably the name of your game.

What matters the most at this point is that your bucket folder structure is identical to what you said it will be in your RemoteLoadPath.

As you saw, I added a ServerData folder above my Android so I will ensure that I do this same hierarchy in my bucket.

Once your folder structure is solid, go inside the Android folder, and copy the files Unity built inside the ServerData/Android/ in the previous section.

You need to have at least 1 hash file, one json, and one bundle; as your addressable assets grow, you get more bundles. If you are missing any of these 3 core files, you need to go back and verify your steps.

I recommend prior to each new build that you delete everything in ServerData/Android locally, and on the Cloud. Then run the build script, and push up the fresh new files.

Anytime you change the RemoteLoadPath, you will need to rebuild the assets!

Unity Code:

Finally, a couple of simple scripts to tie it all together.

First this is our Start method which will make sure that Firebase is installed properly in Unity and on the device. If there are any issues with Firebase, we will see them happen here.

Note: my LoginUser is passing “emailTest” and “passwordTest”, these are just public strings that are set in the Editor on the gameobject with this class attached. You could just as easily remove the variables and put your email and password here directly, of course this is just for testing purposes.

Nothing really special here, as this is pretty much right from the Firebase docs. All I do here is check that Firebase is setup correctly, and if it is, we fire off the coroutine that will authenticate our user. If we get a failure, we’ll display that on our UI.

Now for the methods that fire off sequentially IF Firebase is in order. At each stage, if our Firebase call was successful, we hide the current screen and move onto the next call. If there are any errors, we stop on the current screen and display the error message.

As you can see very straightforward, with lots of room for you to improve and enhance, but this will get you going.

Handling the Addressable Asset

At this point we have our Firebase side of things working all that is left to actually use our built Addressable package that we have stored in the cloud.

I created an empty game and placed this script on it.

The trick here to make sure that your apk build size stays small is that in this situation, my camera does not have an audio file attached; we let that be handle in the above script. If you actually assign the audio file in the scene instead of letting it get loaded through code, Unity is going to include it in the build which will defeat the purpose of what you’re trying to do.

Now before you go and build the apk you want to do one final test to make sure that everything is in working order. Using the existing build in play mode will verify if your cloud url works and if Unity is able to piece it all together.

Test in the editor, if your firebase connections work and you hear the audio file start playing, you’re gold!

All that is left now is to build your app and push it your device!

My APK size dropped about 6mbs when I made my audio file an Addressable.

Conclusion

Set up your scene to use Unity’s Addressable system to take advantage of smaller APK’s to push to your users. Everything is contained inside of that ServerData/[BuildTarget] folder that gets pushed to your server and Unity takes are of the rest.

I hope this guide was helpful, if it was, or if it wasn’t, drop me a comment, I’d love to hear from you!

--

--