Adding Firebase to your Unity Projectplat_iosplat_androidplat_unity

Overview

Power up your Unity games with our Firebase Unity SDKs.

  • With Analytics learn how players are experiencing your game, how much time they spend playing, how long they take to complete levels, how much in-app content they're purchasing and much more.
  • Dynamic Links lets you share content like custom levels, in-game items, and game invitations between players.

  • Using Cloud Messaging you can release new content and send push notifications to players who have completed your available content.

  • Remote Config allows on the fly adjustment of gameplay settings like enemy density, power-up frequency, or anything else you want to control.

  • Realtime Database stores user-generated content like custom maps safely in the cloud.

  • Cloud Storage delivers downloadable content to your players using our secure service.

Download the SDK


Find out more about how you can power up your games, at our Firebase games page.

Features

The following features are supported by the Firebase Unity SDK on iOS and Android:

Feature Unity Package
AdMob Distributed separately in the AdMob Unity Plugin
Analytics FirebaseAnalytics.unitypackage
Authentication FirebaseAuth.unitypackage
Realtime Database FirebaseDatabase.unitypackage
Dynamic Links FirebaseDynamicLinks.unitypackage
Invites FirebaseInvites.unitypackage
Cloud Messaging FirebaseMessaging.unitypackage
Remote Config FirebaseRemoteConfig.unitypackage
Cloud Storage FirebaseStorage.unitypackage

The Firebase Unity SDK includes desktop workflow support for a subset of features, enabling certain parts of Firebase to be used in the Unity editor and in standalone desktop builds on Windows, OS X, and Linux. (Beta)

Sample Game: MechaHamster

To show how easy it is to plug Firebase into your Unity project we made a sample game 'MechaHamster' that you can download from GitHub, the App Store, and the Google Play Store (see links below)

MechaHamster GitHub MechaHamster App Store MechaHamster Play Store

Setup for iOS

Prerequisites

  • Unity 5.3 or later.
  • Xcode 8.0 or later.
  • For Cloud Messaging:
    • A physical iOS device
    • APNs certificate with Push Notifications enabled
  • CocoaPods 1.2.0 or later.
  • A Unity project bundle identifier.

If you don't have a Unity project already, you can download one of our quickstart samples if you just want to try a Firebase feature. If you're using a quickstart, remember to get the bundle identifier from the project settings, you'll need it for the next step.

Add Firebase to your app

It's time to add Firebase to your app. To do this you'll need a Firebase project and a Firebase configuration file for your app.

  1. Create a Firebase project in the Firebase console, if you don't already have one. If you already have an existing Google project associated with your mobile app, click Import Google Project. Otherwise, click Create New Project.
  2. Click Add Firebase to your iOS app and follow the setup steps. If you're importing an existing Google project, this may happen automatically and you can just download the config file.
  3. When prompted, enter your app's bundle ID. It's important to enter the bundle ID your app is using; this can only be set when you add an app to your Firebase project.
  4. At the end, you'll download a GoogleService-Info.plist file. You can download this file again at any time.

Add the SDK to your app

  1. If you don't have a Unity project yet, create one now. You can also use one of the quickstart samples to try out a Firebase feature.
  2. Download the Firebase Unity SDK and unzip it somewhere convenient.
  3. Open your Unity project in the Unity editor.

    • Select the File > Open Project menu item.
    • Click Open.
    • Navigate to the app directory in the file dialog and click Open.
  4. Import the Firebase feature plugin, for example Firebase Analytics.

    • Select the Assets > Import Package > Custom Package menu item.
    • Import FirebaseAnalytics.unitypackage from the Firebase Unity SDK, downloaded previously.
    • Click the Import when the Import Unity Package window appears.
  5. Add the GoogleService-Info.plist file to the project.

    • Navigate to the Assets folder in the Project window.
    • Drag the GoogleService-Info.plist downloaded from the Firebase console into the folder.
      • NOTE: GoogleService-Info.plist can be placed anywhere under the Assets folder.
  6. Build for iOS

    • Select the File > Build Settings menu option.
    • Select iOS in the Platform list.
    • Click Switch Platform to select iOS as 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.

Setup for Android

  • Unity 5.3 or higher.
  • A device running Android 4.0 (Ice Cream Sandwich) or newer.
  • A Unity project bundle identifier.

If you don't have a Unity project already, you can download one of our quickstart samples if you just want to try a Firebase feature. If you're using a quickstart, remember to get the bundle identifier from the project settings, you'll need it for the next step.

Add Firebase to Your app

To add Firebase to your app you'll need a Firebase project and a Firebase configuration file for your app.

  1. Create a Firebase project in the Firebase console, if you don't already have one. If you already have an existing Google project associated with your mobile app, click Import Google Project. Otherwise, click Create New Project.
  2. Click Add Firebase to your Android app and follow the setup steps. If you're importing an existing Google project, this may happen automatically and you can just download the config file.
  3. When prompted, enter your app's package name. It's important to enter the package name your app is using; this can only be set when you add an app to your Firebase project.
  4. At the end, you'll download a google-services.json file. You can download this file again at any time.

Add the SDK to your app

  1. If you don't have a Unity project yet, create one now. You can also use one of the quickstart samples to try out a Firebase feature.
  2. Download the Firebase Unity SDK and unzip it somewhere convenient.
  3. Open your Unity project in the Unity editor.

    • Select the File > Open Project menu item.
    • Click Open.
    • Navigate to the app directory in the file dialog and click Open.
  4. Import the Firebase feature plugin, for example Firebase Analytics

    • Select the Assets > Import Package > Custom Package menu item.
    • Import FirebaseAnalytics.unitypackage from the Firebase Unity SDK, downloaded previously.
    • Click the Import when the Import Unity Package window appears.
  5. Add the google-services.json file to the project.

    • Navigate to the Assets folder in the Project window.
    • Drag the google-services.json downloaded from the Firebase console into the folder.
      • NOTE: google-services.json can be placed anywhere under the Assets folder.
  6. Build for Android

    • Select the File > Build Settings menu option.
    • Select Android in the Platform list.
    • Click Switch Platform to select Android as 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.

Desktop workflow implementations (Beta feature)

When you are creating an app, it's often much easier to test your app in the Unity editor and on desktop platforms first, then deploy and test on mobile devices later on in development. To support this workflow, we provide a subset of the Firebase Unity SDK which can run on Windows, OS X, Linux, and from within the Unity editor:

Desktop Feature Library
Authentication FirebaseAuth.unitypackage
Realtime Database FirebaseDatabase.unitypackage
Remote Config FirebaseRemoteConfig.unitypackage
Cloud Storage FirebaseStorage.unitypackage

The remaining desktop libraries are stub (non-functional) implementations, provided for convenience when building for Windows, OS X, and Linux so that you don't need to conditionally compile code when also targeting the desktop.

Desktop project setup

To use desktop workflow support, you must have an Android or iOS project set up in Firebase console.

Follow the Setup for Android or Setup for iOS instructions to set up the Firebase Unity SDK in your app.

Firebase will automatically use the GoogleService-Info.plist or google-services.json file in your project to set up your app when you run the app in the Unity editor or build for desktop.

Running in Edit Mode

The Firebase Unity SDK can also be run in Unity's edit mode, allowing its use in editor plugins. When creating a FirebaseApp used by the editor, don't use the default instance, but instead provide a unique name to the FirebaseApp.Create() call. This is important to avoid a conflict in options between the instance used by the Unity editor and the instance used by your application.

Note on Firebase Unity SDK desktop support

Firebase Unity SDK desktop support is a Beta feature, and is intended for workflow use only during the development of your application, not for publicly shipping code.

Known Issues

Installing Plugins in Unity 2017.x .NET 4.x Projects

.NET 4.x support is available as an experimental build option in Unity 2017 and beyond. Firebase plugins use components of the Parse SDK to provide some .NET 4.x classes in earlier versions of .NET. Therefore, when importing Firebase into a .NET 4.x enabled project you'll see compile errors from some types in the .NET 4.x framework that are implemented by the Parse SDK.

To resolve the compilation error:

  1. Remove or disable the following DLLs for all platforms:
    • Parse/Plugins/Unity.Compat.dll
    • Parse/Plugins/Unity.Tasks.dll
  2. Enable the following DLLs for all platforms:
    • Parse/Plugins/dotNet45/Unity.Compat.dll
    • Parse/Plugins/dotNet45/Unity.Tasks.dll

If you import another Firebase plugin:

  • Select the menu item Assets > Play Services Resolver > Version Handler > Update to re-enable .NET 4.x DLLs and disable .NET 3.x DLLs.

Unity 2017.1 IL2CPP Compilation in .NET 4.x Projects

.NET 4.x support is available as an experimental build options in Unity 2017 and beyond. Firebase plugins use components of the Parse SDK to provide some .NET 4.x classes in earlier versions of .NET. Type forwarding DLLs are provided which forward Parse types (e.g Parse implementation of System.Threading.Tasks.Task) to the .NET framework. Unfortunately, IL2CPP (transpiler that converts C# to C++) shipped in Unity 2017.1.x does not correctly process type forwarding DLLs resulting in build errors that resemble the following:

Fatal error in Unity CIL Linker Mono.Cecil.ResolutionException: Failed to resolve System.Threading.Tasks.TaskCompletionSource1`

There is currently no workaround available for .NET 4.x IL2CPP build errors in Unity 2017.1. Users must upgrade to 2017.2 or later to use .NET 4.x in projects compiled with IL2CPP.

Unity 2017.2 Networking

Realtime Database and Storage create TLS network connections using the .NET networking stack. TLS functionality is broken in Unity 2017.2 when using .NET 4.6 causing the Realtime Database and Storage plugins to fail. There is no workaround for this issue, you have to use a different version of Unity, for example 2017.1 or 2017.3.

Support

Firebase Support

傳送您對下列選項的寶貴意見...

這個網頁
需要協助嗎?請前往我們的支援網頁