For many developers, using a cloud-based real-time database may be way out there and not even worth consideration. Of course, it also very much depends on the type of game you are developing. You likely wouldn't want to use a real-time database for a FPS, but fortunately, Unity has excellent options for that. For a location-based game, though, a real-time database makes ideal sense for all the reasons we mentioned earlier.
Even if you are not making a location-based game and one that doesn't require ultra-high performance updates, you should consider Firebase. The free version of Firebase real-time allows 100 concurrent users and the first level subscription provides for unlimited concurrent users. Other multiplayer platforms generally start charging after 20 concurrent users.
We won't add Firebase as a multiplayer platform to the sample game we developed over the course of the book. That would likely need several chapters or even a book on its own to cover the numerous details. Instead we will look at setting up and using one of the Firebase samples. First, follow the directions given to download the sample Firebase SDK database project:
database
folder and select the testapp
folder as the Unity project folder. Click on the Open button to load the project.Assets/TestApp
folder in the Project window. The scene will load blank.With the database sample project set up and ready to go, now we need to create a Firebase account and perform some configuration:
TestApp
and then, choose a region from the list that matches your location, as shown:
The Firebase create project window
Database read/write rules
true
, as shown in the following screenshot:
Removing read/write security from database
testapp
). Copy the entire URL to your clipboard by selecting the text and then pressing Ctrl + C (command + C on Mac). Be sure to leave the Firebase console open in your browser window.Now, with the Firebase real-time database configured, we will jump back to Unity and get the project configured:
UIHandler
script located in the Assets/TestApp
folder in the Project window to open the script in your editor of choice.InitializeFirebase
method, as shown in the following code, for review:void InitializeFirebase() { FirebaseApp app = FirebaseApp.DefaultInstance; app.SetEditorDatabaseUrl("https://YOUR-FIREBASE- APP.firebaseio.com/");
TestApp interface to test adding scores to a real-time database
So, it looks like the simple app is saving scores to a database, but now we want to confirm that the data is saved to the cloud database and it updates clients in real time. Follow the given instructions to test the real-time functionality of the database:
Leaders
, added to the database. Expand the following nodes and confirm that the entries match the sample data you entered in Unity, as shown in the following screenshot:
Database Leaders added by Unity
Leaders
list in the Console. Note how they are also automatically updated on the Unity client. This is the power of the real-time database and the automatic client updates.At this stage, if you have used other network solutions, you may be only somewhat impressed. After all, Photon PUN and UNET support very similar things out of the box and they also provide mechanisms to update transform objects and other Unity components automatically. However, as we previously mentioned and discussed, those automatic transformations are of little value in a location-based game, where the player is always at the center of the game world.
Some readers may think that an alternative solution to a player-centered world would actually be positioning a player to their actual-world coordinates in meters. While that could work when the distances are small (less than 100,000 meters for 0,0), the amount of errors that would occur in most other parts of the world would be tremendous.
Of course, location-based games are not the only place well-suited for a real-time cloud database. Some other suitable genres could be board, empire building, strategy, puzzle, card, and more. Speaking of other types of games, how about we look at suggestions for different types of location-based games in the following section?
3.21.100.34