Getting started with Firebase in Unity (2019)

PATRICK MARTIN: Hi, everybody. My name’s Patrick
Martin, and I’m here to show you how to get started
with Firebase in Unity. One of Unity’s strengths, and
I’m sure one of the reasons why you’re here
today, is its ability to target both iOS and Android
with a single project and code base. I’ll start today by configuring
a Unity Project with Firebase that I can deploy to
both of these platforms. Let’s start up by making sure
that our tools are all ready. I like to use the Unity Hub
to keep my copy of Unity up to date and
manage my components. From here, I can add a
new version of Unity, if I don’t have one already. I can also use AddComponent
on my current editor to make sure I have iOS
and Android support. Note that you can only make
local iOS builds on a Mac. If you’re installing
Unity 2019 or later, you can opt to install the
Android Build tools now, as well. If you opt out of
this, or if you’re using an older
version of Unity, you will need to download
Android Studio and install the SDK separately. If you can, I’d
recommend letting Unity manage all of this for you. If you’re using an
older version of Unity or opted to use your own
version of the Android SDK, you can point your
specific version of the Android SDK and NDK under
Preferences, External Tools. If you have an Apple Computer
and want to make an iOS build, you’ll need to have
Xcode installed. Apple makes this really easy
via the Apple App Store. On Windows or
Linux, you can still generate an iOS executable
using Unity Cloud Build. It’s a little more involved than
what I’d like to cover here, but you can find instructions
from Unity’s developer documentation. Now, to initially setup our
project to use Firebase, we’ll bounce a little bit
between both the Unity Editor and the Firebase console. You can easily use an existing
Unity or Firebase project, but I’ll create new
ones from scratch. I’ll create a new Unity Project. This is a default empty
project, and I’ll just add some text for fun. Now, we have a simple
project we can just run. I’ll save the scene, then
open up Build Settings. I will start by building
for Android first. If you’re not on Mac OS, this
is actually the only platform you can target right now. And then I’ll add
some app identifiers that I’ll need to
configure Firebase. If you look under
Other Settings, Package Name, this is
needed to associate your app with Firebase. So let’s change this
to something unique. Now, if you’re setting
up iOS, as well, you can click this little button
up here, then select a Bundle Identifier here. This does not have to match
your Android package name, but I recommend making
them as similar as possible for your own sanity. Here, someone already used
my Android package name as their iOS bundle
identifier, so I have to find my own
unique identifier. We’re now ready to start setting
up our project in the Firebase console. Point your web browser
to, and click on the Go
to Console button. Before we create a project,
I should talk a little bit about what a Firebase project is
versus a Firebase application. A project is a pool of resources
shared between a number of client applications. This may include user accounts,
analytics data, databases, or anything else that
lives in Google’s cloud. An app is a single application
backed by this data. You can have up to 30
applications under one project before you have to ask to
have your limit increased. For this video, I will have
one application for iOS and another application
for Android, sharing one project as their back end. So now, click Add Project,
and give it a nice name. It’ll take a moment to
put everything into place. Then you’ll be dropped into
your freshly-minted project. Now, you can create a standalone
iOS and Android application, but this little
Unity icon will let you create a project and
Unity on multiple platforms that Firebase supports. Let’s create iOS and
Android applications, since we can build both of them
from our Unity installation. That package name and bundle
identifier you set up before go right here. Click this little Register
App button to continue. Now, you have a
Google-Services.json file to download for Android, and
a GoogleService-Info.plist for iOS. These just tell your app
how to connect to Firebase. They’re are actually safe to
commit to version control. So let’s download these files. I like putting these in
a subfolder named Data, but they can go anywhere
in your Assets Directory. Let’s create a Data folder,
and drop these files in there. Now, we can jump back into
the Firebase console and click Next. Here, we’ll see a download
link for Firebase Unity’s SDK. I’ll start that
download, and click Next and continue to the console. We have to do one more thing
to get started on Android. For security
reasons, you’ll need to register the SHA1 of your
app sign-in key with Firebase. So click on the gear
next to Project Overview, and select Project Settings. From here, you can add
certificates. You’ll want your
release certificate here so your users can
access Firebase, but you’ll also want to
have each of your developers register their debug
key, if they’re going to work on Firebase features. So open a terminal and
type keytool list v alias androiddebug keystore
android debug keystore. Note that your password will
be Android for your debug key. The debug keystore
is automatically generated whenever you
first make an Android build. If you get an error
message telling you that it can’t be found,
just run an Android build from Unity real fast. Now, copy the value under
SHA1, switch back over to your Firebase console,
click Add Fingerprint, and paste the SHA1 key. IOS does not need a similar
step to get started, but if you use some
features, like Dynamic Links, you will need to
provide an App Store ID, which you can enter here. For now, I’ll leave this blank. Thanks for sticking
around this long. We’re almost done. Now, we need to integrate
the SDK into Unity. Do you remember that ZIP
we downloaded earlier? Let’s go ahead and unzip it. We have folders for
.NET 4 and .NET 3. Inside of each folder
is a Unity package for each of the Firebase
products supported in Unity. Now, if you’re unfamiliar
with Unity packages, these are effectively little
pieces of a Unity game that you can share
between projects. If you’re looking
for a clean way to bundle up scripts, art,
audio, prefab, scenes, and really anything
else you need and Unity between projects,
this is a system Unity provides you to do so. If you have one and only
one Unity Project open, you can reliably just
double-click on the package you want to install. Since that doesn’t work
in every situation, I’ll walk you through importing
it from the Assets menu. First, we need to figure
out which version of .NET we’re using. I highly recommend using .NET 4,
but if you’re stuck in an older version of Unity, you
might not have a choice. To see what your project
is using, go over to Unity, bring up your Build Settings,
go to Player Settings, and look under Configuration
for Scripting Runtime Version. This will be universal for
all of your target platforms, and actually requires
relaunching your editor to switch. Here, you see that I’m
using .NET 4.x Equivalent, which means that I should use
the .NET 4 Unity packages. Now, go to Assets, Import
Package, Custom Package, and import one of the
Unity packages in .NET 4. Analytics is the easiest to pull
in, so let’s just grab that. Just leave everything
checked and click Import. If you really know
what you’re doing, you can selectively
add different bits, but there are really
very few situations where you’d want to do this
for the Firebase plugins. You may see this dialog box
from the Play Services Resolver. You should let this auto-update. What does it do exactly? Firebase requires
native dependencies outside of what is
typically required by Unity. Rather than requiring you to
learn how each platform’s build system works, and making
sure native components match with their Unity wrappers,
the Play Services Resolver will make sure that
your Unity Project has any non-Unity dependencies it
needs, when generating a device build. Now that you have
the SDK downloaded, you have a new menu
item under Window, Firebase, Documentation. This lets you jump
directly to the console for your application,
as well as quickly set up new integrations. You can even configure a new
project right from this window, if you choose to install
the SDK before configuring the project in the console. One more thing
that we need to do is make sure that
Firebase is initialized. I’ll create a quick
script to make sure Firebase is
available on this device by calling
CheckAndFixedDependenciesAsync. When that completes, I’ll call
SetAnalyticsCollectionEnabled to make sure everything starts. It’s important to note that
analytics collection is typically enabled
by default, but you need to make a call into
Firebase to initialize the SDK. Since this project is really
simple without this call, we would never start
receiving analytics events. Finally, let’s make
sure this works. Remember to add the scene
to the Build Settings, then make sure you
have a phone plugged in or an emulator running with
Google Play Services installed. Then hit Build and Run. I always put everything
under a Build folder under my home directory so I can
clean up large binaries, when I’m running low on disk space. If you jump over to the
Analytics Dashboard, you should now see
one user in this Users in the Last 30 Minutes panel. Now, I’ll target iOS
with the same executable. Simply click iOS, and
then Switch Platform. We’ll run through and
reimport everything in a way that better suits iOS. This usually includes things
like re-encoding textures as PVR, versus something
like ECT for Android. Then just click
Build and Run again. I’m saving the Xcode
project in the same builds directory is my Android APK. You may see this
dialog box appear, when you try to make a build. The Firebase SDK uses a popular
and near-universal package manager called CocoaPods
to manage dependencies on iOS, much like Gradle on
the Android, which the Firebase SDK will download
and install for you. This is a one-time cost, and
after this finishes running, subsequent builds
will be just as snappy as non-Firebase-enabled builds– even if you do a clean build. If you need to kill some
time whilst this runs, I recommend visiting
Cats as a Service. Unlike Android, on
iOS, Unity actually generates a full Xcode project
that you open in Xcode. Then you build your Xcode
executable from here. You can export a Google
project from Build Settings, if you want to, but I
usually only do this if I’m trying to
debug native Android plugins I’ve written on my own. Then we jump over to Xcode
and run it on our device. If everything goes
well, we should see an iOS device pop up
in our Analytics Dashboard, and we’re done. So whether you’re gearing
up for a 48-hour game jam or settling in for the long
haul for your next major battle royale tower defense game,
you have the foundation of a great mobile-first
cloud presence. You can build on Analytics with
custom events or properties, or drop in Crashlytics
to easily start gathering crash data from the field. You may also want to look
into Firebase Authentication, your gateway to
the powerful tools Firebase provides to easily
create and manage world– spanning back-end
infrastructure. Let me know what you plan
to make with Firebase and what you want me to cover
next, either in the comments below or by tweeting
at me on Twitter with the handle @Pux0r3. Link in the description below. See you next time. [MUSIC PLAYING]

5 thoughts on “Getting started with Firebase in Unity (2019)

  1. We don't need another setup guide for unity rather actual videos on how to use the services like storage. There is a video on this, like every other video, it isn't for unity. There is nearly ZERO tutorials for Unity.

Leave a Reply

Your email address will not be published. Required fields are marked *