How to use Firebase with WebGL?
Use Firebase services with Unity and WebGL
Pley Firebase is a Unity package that enables the use of Firebase when building games for WebGL.
Firebase is an excellent game back-end service. Sadly, Firebase does not provide support for WebGL games natively. Luckily, Pley has you covered with a Unity package for your mobile game that allows for partial Firebase-WebGL-Unity integration.
Pley Firebase wraps around the Firebase API and checks if the game is running in-browser. If the game is running on WebGL, the normal import Firebase calls are simply replaced by Pley Firebase; making integrating Firebase with WebGL easy!
Implemented Services
Analytics | Realtime Database | Remote Config | Cloud Firestore |
---|---|---|---|
✅ | ✅* | ✅ | ✅* |
Unimplemented Services
Note that even if these do not work out-of-the-box, Pley can still find solutions during porting.
AdMob | Authentication | Cloud Functions | Cloud Messaging | Cloud Storage | Crashlytics | Dynamic Links |
---|---|---|---|---|---|---|
⛔ | ⛔ | ⛔ | ⛔ | ⛔ | ⛔ | ⛔ |
How to use Pley Firebase
Step 2: Add the Firebase configuration file.
Obtain the app project configuration (Firebase Instructions), and save it to StreamingAssets/google-services-web.json
.
{
"apiKey": "api-key",
"authDomain": "project-id.firebaseapp.com",
"databaseURL": "https://project-id.firebaseio.com",
"projectId": "project-id",
"storageBucket": "project-id.appspot.com",
"messagingSenderId": "sender-id",
"appId": "app-id",
"measurementId": "G-measurement-id"
}
Step 3: Initialize the Firebase API
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task =>
{
var status = task.Result;
if (status == DependencyStatus.Available)
{
Debug.Log("Success initialize firebase app");
}
else
{
Debug.LogError($"Failed to initialize firebase app: {status}");
}
});
Step 4: Access Firebase in your game
The API is the same as the official Firebase Unity SDK, which means that you only have to add import the correct
namespaces based on which platform you're building for.
#if UNITY_WEBGL && !UNITY_EDITOR
using Pley.Firebase;
using Pley.Firebase.Analytics;
using Pley.Firebase.Extensions;
#else
using Firebase;
using Firebase.Analytics;
using Firebase.Extensions;
#endif
....
void OnLevelUp(int newLevel)
{
FirebaseAnalytics.LogEvent(FirebaseAnalytics.EventLevelUp, FirebaseAnalytics.ParameterLevel, newLevel);
}
Just like Firebase!
Once Pley Firebase is implemented you can use firebase just like you would without Pley (according to the Google Firebase documentation), as long as you use the features that Pley has manage to enable for the web.
Updated 3 months ago