Men and Female are not create equal.

Reality is women look down on men with less resources, while men are more attracted to women who needs their resources. Women deep inside still cling to rely on men on gathering resources and offer…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Firebase Database in Unity with REST API

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:

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.

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

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

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

So far so good…

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

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

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.

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:

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.

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.

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

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

Add a comment

Related posts:

An Abyssal Graveyard

A prose poem about the tragic death by drowning of a Syrian refugee family of 8. The sole surviver was the father who refused help and instead removed his life belt as he had lost all reason to live.

How freaking cool is it to be a writer for Medium?

I was amazed to take a chance on my first ever political piece and be later curated in three different categories: Election 2020, Politics, and LGBTQ. And I was initially sad, but ultimately happy…

10 Easy Vegan Rice Recipes That You Will Love To Eat!

Looking for some delicious and full meal prep recipes for lunch or dinner? A Rice meal is the perfect dish that makes you full for a long time. You can serve these vegan rice recipes with any other…