The Firebase Database Unity Sample demonstrates Firebase Realtime Database with the Firebase Unity SDK inside the Unity Editor.
- Unity 5.3 or higher.
- Download the Firebase Unity SDK and unzip it somewhere convenient.
- Open the sample project in the Unity editor.
- Select the
File > Open Projectmenu item. - Click
Open. - Navigate to the sample directory
testappin the file dialog and clickOpen.
- Select the
- Open the scene
MainScene.- Navigate to
Assets/TestApp/MainScenein theProjectwindow. - Double click on
MainScenefile to open.
- Navigate to
- Import the
Firebase Authplugin.- Select the
Assets > Import Package > Custom Packagemenu item. - Import
FirebaseAuth.unitypackagefrom the Firebase Unity SDK, downloaded previously. - Click the
Importwhen theImport Unity Packagewindow appears.
- Select the
- Import the
Firebase Databaseplugin.- Select the
Assets > Import Package > Custom Packagemenu item. - Import
FirebaseDatabase.unitypackagefrom the Firebase Unity SDK, downloaded previously. - Click the
Importwhen theImport Unity Packagewindow appears.
- Select the
- Turn off secure access. Configure your rules for public access.
- Change the line
SetEditorDatabaseUrl("https://replace-with-your-project.firebaseio.com/");to replacereplace-with-your-projectwith the project id found in the firebase console.
Once you have done this, you can run the Unity Editor and test the application.
You will be able to enter an email, a score, and press Add Score to see the
leaderboard update.
- The
Scorefield accepts a numeric score that will be added to the leaderboard. - The
Add Scorebutton only appears once an email is entered. It uses a Firebase Database transaction to record the score if it falls within the current top 5 all time scores.
Once you are ready to secure your database, you can configure your rules with private or user access and still access the database within the editor without logging in. To do this, you will need to create a service account and register it with Firebase by following the steps in the unity getting started documentation.
-
Register your iOS app with Firebase.
- Create a project in the
Firebase console,
and associate your iOS application.
- You should use
com.google.firebase.unity.database.testappas the package name while you're testing.- If you do not use the prescribed package name you will need to update
the bundle identifier as described in the
Optional: Update the Project Bundle Identifierbelow.
- If you do not use the prescribed package name you will need to update
the bundle identifier as described in the
- You should use
- Create a project in the
Firebase console,
and associate your iOS application.
-
Add the
GoogleService-Info.plistfile to the project.- Navigate to the
Assets\TestAppfolder in theProjectwindow. - Drag the
GoogleService-Info.plistdownloaded from the Firebase console into the folder.- NOTE:
GoogleService-Info.plistcan be placed anywhere under theAssetsfolder.
- NOTE:
- Navigate to the
-
Optional: Update the Project Bundle Identifier
- If you did not use
com.google.firebase.unity.database.testappas the project package name you will need to update the sample's Bundle Identifier.- Select the
File > Build Settingsmenu option. - Select
iOSin thePlatformlist. - Click
Player Settings - In the
Player Settingspanel scroll down toBundle Identifierand update the value to the package name you provided when you registered your app with Firebase.
- Select the
- If you did not use
-
Build for iOS
- Select the
File > Build Settingsmenu option. - Select
iOSin thePlatformlist. - Click
Switch Platformto selectiOSas the target platform. - Wait for the spinner (compiling) icon to stop in the bottom right corner of the Unity status bar.
- Click
Build and Run.
- Select the
- Register your Android app with Firebase.
- Create a project in the
Firebase console,
and attach your Android app to it.
-
You should use
com.google.firebase.unity.database.testappas the package name while you're testing.- If you do not use the prescribed package name you will need to update
the bundle identifier as described in the
Optional: Update the Project Bundle Identifierbelow.
- If you do not use the prescribed package name you will need to update
the bundle identifier as described in the
-
To generate a SHA1, first you will need to set the keystore in the Unity project.
- Locate the
Publishing SettingsunderPlayer Settings. - Select an existing keystore, or create a new keystore using the toggle.
- Select an existing key, or create a new key using "Create a new key".
- Locate the
-
After setting the keystore and key, you can generate a SHA1 by running this command:
keytool -exportcert -list -v -alias <key_name> -keystore <path_to_keystore>
-
- Download the
google-services.jsonfile associated with your Firebase project from the console. This file identifies your Android app to the Firebase backend, and will need to be included in the sample later.- For further details please refer to the general instructions which describes how to configure a Firebase application for Android.
- Create a project in the
Firebase console,
and attach your Android app to it.
- Add the
google-services.jsonfile to the project.- Navigate to the
Assets\TestAppfolder in theProjectwindow. - Drag the
google-services.jsondownloaded from the Firebase console into the folder.- NOTE:
google-services.jsoncan be placed anywhere under theAssetsfolder.
- NOTE:
- Navigate to the
- Optional: Update the Project Bundle Identifier
- If you did not use
com.google.firebase.unity.database.testappas the project package name you will need to update the sample's Bundle Identifier.- Select the
File > Build Settingsmenu option. - Select
Androidin thePlatformlist. - Click
Player Settings - In the
Player Settingspanel scroll down toBundle Identifierand update the value to the package name you provided when you registered your app with Firebase.
- Select the
- If you did not use
- Build for Android
- Select the
File > Build Settingsmenu option. - Select
Androidin thePlatformlist. - Click
Switch Platformto selectAndroidas the target platform. - Wait for the spinner (compiling) icon to stop in the bottom right corner of the Unity status bar.
- Click
Build and Run.
- Select the
https://firebase.google.com/support/
Copyright 2016 Google, Inc.
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.