I am working on a mobile game project and I am using Addressables to load the assets from a remote server instead of embedding them in the final build like it’s usually done (thus reducing the final build size drastically). How can I use Firebase Hosting to do so?
I used BunnyCDN and Google Cloud to host the addressables but since the project owner wants it on Firebase, I was searching for ways to use Firebase as a remote host for the addressable files. I looked into Firebase Storage, but it doesn’t seem to be the right direction since it doesn’t come with CDN capabilities, and it also doesn’t easily allow Unity to access the files (there is some third party plugin that helps with this though. See this Unity Discussion).
Then I looked into Firebase Hosting and it works perfectly. Although Firebase Hosting is primarily intended for website hosting, nothing really stops you from deploying any file you want, like addressables, and accessing it via the url.
So the procedure is easy enough. You need a Google Account since Firebase is by Google.
Then you need to create a Firebase project. Easy enough.
Then when at the Firebase dashboard choose to enable Firebase Hosting service.
This is where you need to follow their guide, which is
- Installing Node.js and npm (command based installation might be tricky, use the GUI installer from Node.js official site)
- Installing Firebase CLI via the npm
- Initializing the project via the Firebase CLI (you need to be at the root of your Unity project in your terminal)
- I only chose the Hosting service when it asked me which services to include in the Firebase project. There are like 10 options but you don’t need them.
- chose not to make it into a single page site
- chose the ServerData directory as my public folder since that is where Unity builds the addressables
- Deploying the project via the Firebase CLI
This is how my console looked like
Of course, you will also need to put the Firebase url into your Addressables Profiles settings like so
Now you might run into some problems with the URL if you don’t set it up correctly, since it needs to be exact. For example, if I’ve typed in my Addressables Profiles settings that the RemoteLoadPath is https://mywebsite.web.app/[BuildTarget]
, then Unity will be expecting the addressable files to be at https://mywebsite.web.app/Android/
(if I am making an android game). I can change that to whatever, like https://mywebsite.web.app/MyApp/Testing/Android/
, but I will need to deploy the addressables at that exact location too. What the directory structure of the uploaded files looks like depends on how you set up the public directory during the initialization procedure. You can always re-initialize the Firebase btw, so it’s okay to make a mistake.
Since I chose the ServerData directory as my public directory in the Firebase CLI, now whenever I deploy the Firebase “website” what Firebase Hosting will do is upload all the folders and files in ServerData as are.
Example: I have this structure in my Unity Project:
/MyProject/
Assets
Library
Packages
ServerData
Android
catalog_2024.01.01.08.20.29.hash
catalog_2024.01.01.08.20.29.json
...
...
If I’ve told the Firebase CLI that ServerData is my public directory, then I can access catalog_2024.01.01.08.20.29.json file like so:
https://mywebsite.web.app/Android/catalog_2024.01.01.08.20.29.json
Finally, my workflow is as follows: I change some assets in Unity, choose the Update a Previous Build
option in the Addressables Groups panel, and then just run firebase deploy
in the Firebase CLI. Within seconds the updated files are on Firebase servers and I can test the game with the new files immediately.
Hope this helps.
edit: clarification: the reason I dropped Google Cloud as my remote server solution is because I couldn’t purge the cache whenever I needed (it took like an hour to refresh), which is a must when testing and debugging Addressables. I can’t wait an hour or a week or whatever the default cache setting is which can’t be changed. BunnyCDN was good, it has a purge cache option, but I had to use Google services.