Integrate Firebase with Openfort in Unity
Learn how to integrate Firebase backend and Openfort with your game
Overview#
Firebase is Google's all-encompassing app development platform, providing game developers with an array of backend tools powered by Google Cloud.
Within Unity, we've integrated the Firebase SDK to facilitate backend functionalities. Coupled with the Google Play Games plugin for Unity, it ensures a secure and efficient login for Android users.
The integration is further enhanced with the Openfort Firebase extension, which incorporates the Openfort SDK. This enables Unity clients to directly leverage Openfort's blockchain capabilities, allowing for sophisticated blockchain interactions within the gaming environment.
Application Workflow#
Prerequisites#
-
Sign in to dashboard.openfort.xyz and create a new project.
-
You need a Google Play Developer account.
-
You need a Google Cloud project.
-
Clone or download the repository and open it with Unity 2021.3. When opening the project, select Ignore on this popup:
Once opened, you will see some reference errors. We will solve this in the next step by importing the Firebase SDK.
-
Follow the Firebase-Unity setup guide. On step 4, you just need to import FirebaseAuth and FirebaseFirestore packages:
Do it one by one and disable ExternalDependencyManager folder before importing:
Most reference errors should be solved by now. If
UnityEditor.iOS.Extensions.Xcode
error is still standing, select Firebase.Editor asset, disable Validate References and choose Apply: -
Create a keystore Follow this guide to create a new keystore for the Unity project.
-
Find SHA1 certificate fingerprint You need to extract the certificate fingerprint from the created keystore. Follow this video tutorial on how to do it, it also covers the creation of the keystore.
This is the command that you will need to run:
_10keytool -list -v -keystore "path/to/your/keystore" -alias "your_key_alias"
Set up Firebase#
Add Google sign-in provider#
Go to the Firebase console, select your project and select Authentication:
After selecting Get started, select Google as a sign-in provider:
Activate Enable toggle, choose a public-facing name and select Save:
A popup will appear. Copy the Web client ID and the Web client secret somewhere safe and choose Done. You will see your Google provider enabled:
Select the provider and choose Project Settings. Under Your apps section select Add fingerprint and add your SHA1 certificate fingerprint. Then choose Save:
Add Google Play sign-in provider#
Select Add new provider and choose Google Play:
Activate Enable toggle, enter the credentials you just saved and choose Save:
Both Google and Google Play sign-in providers are ready:
Install Openfort Extension#
Go to the Firebase Extensions Hub and choose Install in Firebase console:
Choose your project to continue:
Set up your billing profile and follow the instructions until you need to insert the Openfort API Secret key and choose Create secret. Also set Cloud Firestore to Sync:
Finally choose Install extension. After 3-5 minutes you will see the extension installed:
Now select Get started and under How this extension works section find Configure Openfort webhooks. Copy the URL:
Set up Openfort#
Add webhook#
Choose Add webhook:
Paste the webhook URL you got from the Firebase extension and leave the Type as it is. Choose Add webhook:
Add a Contract#
Choose Add contract:
This sample requires a contract to run. We use 0xbed6a05ce8719bc00db1cc81a814192c82be1bb1 (NFT contract deployed in 80002 Amoy). You can use the same for this tutorial:
Add a Policy#
Choose Add policy:
We aim to cover gas fees for users. Set a new gas policy:
Now, add a rule so our contract uses this policy:
Set up Google Play#
Reminder: Use the same Google account you used for setting up your Firebase app.
Create a new app#
Go to Play Console and create a new app. Enter app details (it's important you select Game), confirm policies and select Create app:
Under Grow --> Play Games Services --> Setup and management --> Configuration, select Create new Play Games Services project and choose your Firebase project as the cloud project. Then select Use:
Add credentials#
Add Android OAuth client credential
Under Credentials section choose Add credential:
Select Android:
Scroll down and select Create OAuth client:
Choose Create OAuth Client ID:
This will open the Google Cloud console. Now select Android as Application type, enter a Name and fill the Package name with the Unity app package name (found in the Android Platform Player Settings):
Enter your SHA1 certificate fingerprint and choose CREATE:
Now you can download the JSON and choose OK:
Go back to the Google Play console, select Done and choose your newly created Android OAuth client. Then select Save changes:
Add Game server/Web OAuth client credential
Go back to Configuration and select Add credential:
Choose Game server, refresh OAuth clients, select Web client (auto created by Google Service) (it was created automatically during this process) and select Save changes:
Finally copy the OAuth client ID:
Set up Unity project#
Reminder: Make sure Android is selected as a platform in Build settings.
Go to Window --> Google Play Games --> Setup --> Android setup:
Paste the Game server OAuth client ID you just copied under Client ID:
Go to the Google Play console and on your app's configuration select Get resources:
Copy the Android (XML):
In Unity, paste it in Resources Definition and then select Setup:
Finally, go to the Firebase console and under your app configuration, download the google-services.json:
Import it in your Unity project Assets folder to make sure every credential is up to date.
Test on Android#
Upon building and running the game on an Android device, the registration/login process is automated via Google Play Games, resulting in a streamlined user experience.
Conclusion#
Upon completing the above steps, your Unity game will be fully integrated with Openfort and Firebase. Always remember to test every feature before deploying to guarantee a flawless player experience.
Get support#
If you found a bug or want to suggest a new [feature/use case/sample], please file an issue.
If you have questions, comments, or need help with code, we're here to help:
- on Twitter at https://twitter.com/openfortxyz
- on Discord: https://discord.com/invite/t7x7hwkJF4
- by email: support+youtube@openfort.xyz