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 Embedded Smart Accounts, which are created for every new Google Play Games user logged into Firebase. This enables Unity clients to directly leverage Openfort's blockchain capabilities, allowing for sophisticated blockchain interactions within the gaming environment.
-
Sign in to dashboard.openfort.xyz and create a new project.
-
You need a Google Play Developer account.
-
You need a Google Cloud project.
-
Run the Express backend locally.
-
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:
keytool -list -v -keystore "path/to/your/keystore" -alias "your_key_alias"
Go to the Firebase console, select your project and select Authentication:
Select 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:
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:
Enable Firebase, add your project ID and choose Save:
Choose Add contract:
This sample requires a contract to run. We use 0x38090d1636069c0ff1Af6bc1737Fb996B7f63AC0 (NFT contract deployed in 80001 Mumbai). You can use the same for this tutorial:
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:
Reminder: Use the same Google account you used for setting up your Firebase 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:
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:
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:
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.
Add your publishable key here.
Add your Express backend minting URL here.
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.
Upon completing the above steps, your Unity game will be fully integrated with Openfort Embedded Smart Accounts and Firebase and Google Play Games. Always remember to test every feature before deploying to guarantee a flawless player experience.
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: [email protected]