ไปที่คอนโซล

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).

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. By default, read and write access to your database is restricted so only authenticated users can read or write data. To get started without setting up Authentication, you can configure your rules for public access. This does make your database open to anyone, even people not using your app, so be sure to restrict your database again when you set up authentication.

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.
    FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://YOUR-FIREBASE-APP.firebaseio.com/");
  }
}

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

https://console.cloud.google.com/iam-admin/serviceaccounts/project?project=YOUR-FIREBASE-APP

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.
    FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://YOUR-FIREBASE-APP.firebaseio.com/");
    FirebaseApp.DefaultInstance.SetEditorP12FileName("YOUR-FIREBASE-APP-P12.p12");
    FirebaseApp.DefaultInstance.SetEditorServiceAccountEmail("SERVICE-ACCOUNT-ID@YOUR-FIREBASE-APP.iam.gserviceaccount.com");
    FirebaseApp.DefaultInstance.SetEditorP12Password("notasecret");
  }
}

Next Steps

Known issues

  • When using the experimental .NET 4.6 framework, the Realtime Database does not work on macOS with Unity 2017.2 due to a regression introduced in 2017.2.0 which broke SslStream in that configuration. The regression was fixed in 2017.3.0 and the Realtime Database does work with that version.