Firebase as a multiplayer platform

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.

Note

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:

  1. Open a web browser and navigate to the following URL: https://github.com/firebase/quickstart-unity.
  2. Follow the directions on the GitHub page to either download the SDK samples as a ZIP file or using Git to clone the repository to your computer. If you downloaded the samples as a ZIP file, unzip them into a folder you can locate later.
  3. Start a new instance of Unity and click on the Open button at the top of the project selection window. Navigate to the folder you unzipped or cloned the SDK samples to. Then, open the database folder and select the testapp folder as the Unity project folder. Click on the Open button to load the project.
  4. After the project is open, you will see some compile errors. This is normal and nothing to worry about. We will resolve these shortly.
  5. Open MainScene by double-clicking on the MainScene scene in the Assets/TestApp folder in the Project window. The scene will load blank.

    Tip

    If Unity is still set to use the mobile layout, you may want to change the back to Default layout for the rest of this exercise. From the menu, select Window | Layouts | Default.

With the database sample project set up and ready to go, now we need to create a Firebase account and perform some configuration:

  1. Use your web browser to go to the Firebase site at https://firebase.google.com/.
  2. Near the top of this page will be a large button with the text, Get Started for Free. Click on this button and, when prompted, log in with your Google account, preferably the same one you used to create the Google Places API key earlier.
  3. After you log in, you will be taken to the Console and prompted to import or create a new project. For now, we will just create a new project.
  4. You will be prompted to name you project and select a region. Name your project TestApp and then, choose a region from the list that matches your location, as shown:

    Firebase as a multiplayer platform

    The Firebase create project window

  5. Then, click on the Create Project button to create the new project. After the project is created, you will be taken to the Firebase Console for the project.
  6. On the left-hand side panel, select the Database item to open the Real-time Database panel. Then, in the panel, select the Rules tab at the top. The following screenshot is shown for reference:

    Firebase as a multiplayer platform

    Database read/write rules

  7. For the purpose of this example, we don't really want to concern ourselves with setting up security. Of course, in a real-world app or game, security should be the first priority. You can disable the read/write database security by setting each of the values to true, as shown in the following screenshot:

    Firebase as a multiplayer platform

    Removing read/write security from database

  8. After you make the changes to the values, you will then need to click on the Publish button at the top of the panel.

    Tip

    Did you note that everything in the database looks like JSON? That's because the real-time database is entirely in JSON.

  9. Navigate back to the Data tab. Note the comment about default security rules. Again, we want to disable security for this sample test, so click on the Dismiss link beside the notice. This will disable the first level authentication.
  10. At the top of the Data tab will be the project URL, which should contain the name you set for the project (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.

Tip

Firebase supports standard OAuth, custom OAuth, Google, Facebook, Twitter, and federated authentication support. It also supports multiple levels of database rules you can apply to the node level.

Now, with the Firebase real-time database configured, we will jump back to Unity and get the project configured:

  1. Double-click on the UIHandler script located in the Assets/TestApp folder in the Project window to open the script in your editor of choice.
  2. Scroll down in the file and locate the InitializeFirebase method, as shown in the following code, for review:
            void InitializeFirebase() { 
                FirebaseApp app = FirebaseApp.DefaultInstance; 
                app.SetEditorDatabaseUrl("https://YOUR-FIREBASE-
                APP.firebaseio.com/"); 
    
  3. Select the URL text, shown highlighted, and paste the URL you copied earlier to your clipboard by pressing Ctrl + V (command + V on Mac). Review the previous set of steps if you need to copy that URL to your clipboard again.
  4. Save the file and return to Unity. Wait for the script to recompile and click on Play to run the test app in the editor.
  5. An UI will be displayed in the Game window. Enter an e-mail address and score then click on the Enter Score button. Try this a few times to enter a number of scores. Note how the scores populate in the list, as shown in the following screenshot:

    Firebase as a multiplayer platform

    TestApp interface to test adding scores to a real-time database

  6. Stop and then start the game again. Note how the scores are added back in automatically. After you are done testing, leave the game running in the editor.

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:

  1. Go back to the browser window with your Firebase console. The first thing you will note is that there is a new child, called 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:

    Firebase as a multiplayer platform

    Database Leaders added by Unity

  2. Edit the values directly in the Console and then switch back to the running Unity session. Note how the values on the score list are changing almost instantly.
  3. Try adding or deleting values to the 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.

Note

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?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.100.34