Google is committed to advancing racial equity for Black communities. See how.

Get Started with Firebase Realtime Database for Unity

The Firebase Realtime Database stores and synchronizes data with our NoSQL cloud database. Data is synced across all clients in realtime, and remains available when your app goes offline.

Before you begin

Before you can use Realtime Database, you need to:

  • Register your Unity project and configure it to use Firebase.

    • If your Unity project already uses Firebase, then it's already registered and configured for Firebase.

    • If you don't have a Unity project, you can download a sample app.

  • Add the Firebase Unity SDK (specifically, FirebaseDatabase.unitypackage) to your Unity project.

Note that adding Firebase to your Unity project involves tasks both in the Firebase console and in your open Unity project (for example, you download Firebase config files from the console, then move them into your Unity project).

Create a Database

  1. If you haven't already, create a Firebase project: In the Firebase console, click Add project, then follow the on-screen instructions to create a Firebase project or to add Firebase services to an existing GCP project.

  2. Navigate to the Realtime Database section of the Firebase console. You'll be prompted to select an existing Firebase project. Follow the database creation workflow.

  3. Select a starting mode for your Firebase Security Rules:

    Test mode

    Good for getting started with the mobile and web client libraries, but allows anyone to read and overwrite your data. After testing, make sure to review the Understand Firebase Realtime Database Rules section.

    To get started with the web, iOS, or Android SDK, select test mode.

    Locked mode

    Denies all reads and writes from mobile and web clients. Your authenticated application servers can still access your database.

  4. Click Done.

When you enable Realtime Database, it also enables the API in the Cloud API Manager.

Setting up public access

The Realtime Database provides a declarative rules language that allows you to define how your data should be structured, how it should be indexed, and when your data can be read from and written to.

Configuring the SDK for the Unity Editor.

When testing your scene in the Unity Editor, you can use the Realtime Database. You must configure the SDK with the proper database URL. Call SetEditorDatabaseUrl with the url of your database.

using Firebase;
using Firebase.Unity.Editor;

public class MyScript: MonoBehaviour {
  void Start() {
    // Set this before calling into the realtime database.

If you have chosen to use public access for your rules and have set the database url, you can proceed to the sections on saving and retrieving data.

Optional. Editor Setup for restricted access.

If you choose to use rules that disallow public access, you will need to configure the SDK to use a service account to run in the Unity Editor. This will also allow you to impersonate end users while testing. To do this first create a new p12 file via

Record the generated email and password of the service account.

Place the p12 file under "Editor Default Resources" within your Unity project. Next, add the following code to initialize usage of the service account.

using Firebase;
using Firebase.Unity.Editor;

public class MyScript: MonoBehaviour {
  void Start() {
    // Set these values before calling into the realtime database.

Next Steps