Why use REST?

When it comes to Unity, Firebase offers a complete SDK in order to easily integrate its different services (database, authentication, functions…).

So why should we use REST APIs instead?

The main reason is that the Firebase SDK is not available for Standalone Unity Builds (Windows, MacOS, Linux). There is a desktop workflow, but as it is stated on the Firebase documentation:

Caution: Firebase Unity SDK desktop support is a beta feature. This feature is intended only for workflows during the development of your game, not for publicly shipping code.

Moreover, I have encountered a bug that simply makes Firebase SDK not work on Unity 2019.3. This is not a surprise because, at the time I am writing this, that version of Unity is still in Alpha stages, but it is something to consider.

Enough chitchat, let’s begin!

These are the functionalities I plan on implementing for this tutorial:

Ability to post a user to the database

a user to the Ability to retrieve a user from the database given its id

a user from the given its id Ability to retrieve all users from the database (and all their ids)

What we’ll be using

First things first, we’ll be using two external libraries that will help us in our tasks:

Rest Client which is available on the asset store

which is available on the asset store Full Serializer which you can find here

The User object

Let’s create a User object responsible for holding data that we’ll later upload to the Firebase Database.

So far so good…

Let’s connect to Firebase

If you haven’t already, create a new Firebase project!

Once you are in your project, click on Database in the toolbar and create a Realtime Database with security rules in testing mode.

Once that is done, go to your Project Settings and copy your Project Id, we’ll need that in just a moment. For this example, my Project Id will be nico_the_weather, for no logical reason.

The DatabaseHandler Class

Let’s take this slowly. First of all, we’ll need a reference to the database REST Endpoint we’ll send the requests to:

The PostUser Function

Now it’s time to write our PostUser function that will upload a new user onto our database. The function will take as a parameter the user that will be uploaded and also a userId that will identify the user so that we can retrieve it afterward from the database. We’ll be doing a Put request using the RestClient Asset linked above.

That means that, for instance, if the userId is 10, the user will be uploaded on Firebase Database and it will be available at this endpoint: https://nico_the_weather.firebaseio.com/users/10.json

The user will look something like this on the Firebase Database:

Depending on your connection speed, the request may take some time. It is good practice to know exactly when the request is complete. Luckily for us, RestClient has a simple way to solve this very issue (with .Then()):

Line 4 of that snippet of code will only execute once the request has been successfully fulfilled. We’ll include a delegate as a parameter of our function so that whoever calls the PostUser function can decide what to do after the request is complete, like this:

The GetUser Function

Let’s now do the exact opposite: a Get request to retrieve a user with a specific userId:

The .Then() will now have our already deserialized object User as a parameter; we can use that as a parameter in our function delegate.

The GetUsers Function

We’ll now create a function that will get all of the users in our database! Boom!

Before doing that, let’s think about how data in Firebase Realtime Database is structured. Let’s look at this list of users in JSON:

As you can see, for each user, we have a Key (the userId) and a Value (the user age, name, and surname).

At example, for the second user in the list, the Key is [11] and the Value is [“age”: 36, “name”: “Matt”, “surname”: “Smith”].

In C#, that JSON is mapped into a Dictionary<string, User>, which is a list that pairs up a userId (Key) with a User (Value).

Unfortunately, Unity’s internal serializer is not good enough to serialize such a complex object; that is why we are forced to use an external library, FullSerializer, which is linked above. With that in mind, let’s write the function that will retrieve all of our users:

First and for most, the URL of the request no longer needs a userId, since we are downloading the entire users branch. Secondly, we are no longer internally deserializing the result (note that the result is not a <User> anymore but a response object), unlike the other functions. This means, the response.Text parameter we will retrieve from the request will be some JSON.

Then, we deserialize that JSON into the type we want (we said above it had to be a Dictionary<string, User>) and we put that as a parameter of our delegate function.

If you have questions on the FullSerializer syntax, check out their documentation on GitHub.

So, in conclusion, this is how our DatabaseHandler class is looking:

The Main Class

We’ve written all the logic involved to make our requests do their job! Now it’s time to call those functions we created and see if our work pays off:

Thanks to Unity magic, that function right there will be executed right when we start our application; so let’s now write some requests and run the app to test them!

This code should now create a new User and then print the information about the user with an id of 11. In fact, if we run the app and check our logs, this is what we see:

Unity Logs

For our last test, let’s replace the code above with this one:

This should retrieve all users in our database…

Unity Logs

And that’s it!

You can check out all of the code shown in this tutorial on GitHub.

Finally, if you are looking for more content regarding Unity Firebase tutorials, you can take a look at my playlist Firebase Unity Tutorials on my Youtube channel.