How To Create A Database In Unity
This tutorial shows you how to use Unity and Firebase on Google Cloud to create an integrated, robust, cross-platform, and globally available leaderboard for the MechaHamster game. This tutorial assumes that you are experienced in working with Unity and have working knowledge of C# programming in the Unity game engine. The tutorial assumes that you're using Windows or macOS on your computer.
Costs
If your usage is less than the limits specified in the Firebase Spark plan, there is no charge for using Firebase. This tutorial fits within Firebase Spark plan limits.
When you finish this tutorial, you can avoid continued billing by deleting the resources you created. See Cleaning up for more detail.
Before you begin
-
Make sure you have a Firebase account. If you don't, create one in the Firebase console:
Go to the Firebase Console
-
Make sure you have the Unity game engine installed and licensed on your computer. This tutorial is tested with the personal edition of Unity 2019.1.4f1.
-
Make sure you have the Android Build Support module installed for your version of Unity.
-
Make sure you have the iOS Build Support module installed for your version of Unity.
-
Make sure you have Git installed on your computer.
Introduction
MechaHamster is an open-source game where you guide a futuristic hamster ball through dangerous levels, create your own mazes with the built-in map editor, and share them with friends. The game is intended to serve as a demonstration, sample, and reference implementation for using the Firebase Unity SDK to add Firebase technology to a game project. MechaHamster is available as source code on GitHub, a playable game on Google Play for Android, and on the App Store for iOS.
Firebase is Google's web and mobile platform that helps accelerate the development of a wide range of backend services by leveraging APIs that run on multiple platforms and across a range of devices.
In this tutorial, you explore a subset of these services, set up the connection between Unity and Firebase, and explore an integrated, cross-platform and globally available leaderboard.
Creating a Firebase project
The first task is to create a Firebase project for the tutorial.
-
Log into your Firebase account.
Go to the Firebase console
-
Click Add project.
-
Enter a project name. You can use any name (for example,
leaderboardtest).Make a note of the name you use, because you use this name throughout the tutorial.
-
Follow the remaining steps on the page, and then click Create project.
-
After the wizard provisions your project, click Continue.
-
In the Project Overview page of your project, click Settings, and then select Project settings.
Leave this page open for the next procedure.
Registering your iOS or Android app
In this section, you register your Unity app. When you're done, the project is represented in your Firebase project as an Android or iOS app.
- In the Your Apps section of the Firebase console, click
Unity. - Click Register as iOS app.
-
In the iOS bundle ID box, enter a name like
com.google.leaderboardtest.dev.Make a note of the name, because you need to enter it into Unity in a later step. You can use the same app name for Android and iOS.
-
Optionally, enter a name in App nickname.
-
Click Register as Android app.
-
In the Android package name box, enter a name like
com.google.leaderboardtest.dev.Make a note of the name, because you need it later. You can use the same app name for Android and iOS.
-
Optionally, enter a name for App nickname.
-
Click Register app.
-
Click Download GoogleService-Info.plist and save the file to your computer.
-
Click Download google-services.json and save the file to your computer.
-
In the Download config file section, click Next.
-
Select Download Firebase Unity SDK and save the file to your computer.
This downloads a ZIP file with a name similar to
firebase_unity_sdk_version-number.zip`. At the time of writing, the name isfirebase_unity_sdk_6.0.0.zip. -
Unzip the downloaded file and make a note of where the
firebase_unity_sdkfolder is.You use this in the next step.
-
Click Next.
-
Click Continue to console.
Creating the Firebase Realtime Database
Firebase Realtime Database is a NoSQL cloud database that synchronizes across all clients in real time and that remains available even when your app goes offline. In this section, you create a Firebase Realtime Database and examine the interface for viewing the database contents in your game.
- In the Firebase console menu, select Develop, and select then Database.
- In the Database page, go to the Realtime Database section, and then click Create database.
-
In the Security rules for Realtime Database dialog, select Start in test mode, and then click Enable.
-
Examine the database console.
You see one null record. As you play MechaHamster later, this listing is updated with leaderboard scores.
Enabling sign-in methods
In this section, you enable users to sign in to MechaHamster when they're playing the game. For production use, you would likely enable multiple options. For this test case, you enable anonymous sign-in.
- In the Firebase console menu, select Develop, and then select Authentication.
- In the Authentication window, go to the Sign-in method tab.
- Scroll down in the window, and then click Anonymous.
- In the window, enable the option, and then click Save.
Downloading the MechaHamster code
You can now get the code for the game.
-
In Windows PowerShell, or macOS Terminal, go to the root folder:
cd ~ -
Clone the MechaHamster GitHub repository:
git clone https://github.com/google/mechahamster --recurse-submodules
Adding the Firebase SDK to your app
The next task is to open MechaHamster in Unity and import the Firebase Unity SDK plug-ins.
- In the Unity Hub window, to open the MechaHamster project, click Add and select the mechahamster folder you downloaded in the previous section.
- In the Unity Version list, select the Unity editor version.
- Click the MechaHamster project to open the Unity editor.
-
Import the Firebase Realtime Database plugins, which adds the Firebase Realtime Database to Unity.
- From the menu, select Assets > Import Package > Custom Package.
-
Import
FirebaseDatabase.unitypackagefrom thefirebase_unity_sdk/dotnet3folder that you downloaded in the previous procedure.For now, MechaHamster requires .NET Framework version 3 packages. Ensure you import the packages from the
dotnet3folder, not thedotnet4folder. -
When the Import Unity Package window appears, use the default selections and click Import.
-
Repeat these steps to import all of the Firebase Unity SDK plug-ins from the
firebase_unity_sdk/dotnet3folder.MechaHamster includes examples for many features and displays errors if some of the plug-ins are missing.
Set the bundle identifier for iOS games
In this step, you connect Unity to Firebase by adding the PLIST settings file to Unity's Asset folder and changing the bundle identifier to match what you previously registered with Firebase. This enables communication between Unity and Firebase, which you test in a later procedure.
-
In Unity, add the
GoogleService-Info.plistfile to the Unity project:- In the Unity Project window, go to the
Assetsfolder. - Drag the
GoogleService-Info.plistfile you downloaded earlier into theAssetsfolder.
- In the Unity Project window, go to the
-
Select File > Build Settings.
-
Select Platform > iOS to select your target build.
-
Click Player Settings.
-
In the Project Settings window, under Player > Other Settings > Identification > Bundle Identifier, enter the name that you used earlier in the Firebase app (for example,
com.google.leaderboardtest.dev). -
In the Project Settings window, under Player > Other Settings > Configuration, set scripting runtime version to .NET 3.5 Equivalent.
-
In the Project Settings window, under Player > Other Settings > Configuration, set the API compatibility level parameter to .NET 2.0.
Set the package name for Android games
In this step, you connect Unity to Firebase by adding the JSON settings file to Unity's Asset folder and changing the package name to match what you previously registered with Firebase. This enables communication between Unity and Firebase, which you test in a later procedure.
-
In Unity, add the
google-services.jsonfile to the Unity project:- In the Unity Project window, go to the
Assetsfolder. - Drag the
google-services.jsonfile you downloaded earlier into theAssetsfolder.
- In the Unity Project window, go to the
-
Select File > Build Settings.
-
Select Platform > Android to select your target build.
-
Click Player Settings.
-
In the Project Settings window, under Player > Other Settings > Identification > Package Name, enter the name that you used earlier in the Firebase app (for example,
com.google.leaderboardtest.dev). -
In the Project Settings window, under Player > Other Settings > Configuration, set the scripting runtime version to .NET 3.5 Equivalent.
-
In the Project Settings window, under Player > Other Settings > Configuration, set the API compatibility level parameter to .NET Standard 2.0.
Playing MechaHamster in Unity
You can now play the game in the Unity editor, post a few high scores, and see the real-time database update in the Firebase console.
- In the Unity Project window, select Assets > Hamster > MainGameScene to load the main game scene in the Game window.
-
In the Unity toolbar, press Play.
This previews MechaHamster in the Game window.
-
Play through a level and register a score:
- In the Game window, click Anonymous Sign In.
- Click Play!
- Click Easy Peezie. The selected maze turns yellow.
- Click Let's Roll! to start the selected game.
- Use the arrow keys on your keyboard to move the ball on the screen, avoiding obstacles, and making your way to the goal tile.
- After you've completed the maze, click Submit! to log your score. You are taken to the High Rollers page, where the score of the default player (Hammy) is displayed.
Validating the connection between Firebase and Unity
To validate the connection, you can change the user_id and username values in the Firebase console, then switch back to Unity to view the updated settings in the High Rollers page.
-
In the Realtime Database window in Firebase, view the score:
-
Go to the Firebase console.
Go to the Firebase console
-
Select your project.
-
Select Develop > Database.
-
In the Realtime Database window, navigate to an individual entry.
-
Make a note of the settings for
score,timestamp,user_id, andusername.
-
-
Update the
user_idandusernamevalues in the Realtime Database:- In the
user_idline, double-click Hammy, and then change the value to Hamster. - In the
usernameline, double-click Hammy, and then change the value to Hamster.
- In the
-
Switch back to Unity.
-
In the High Rollers window in Unity, toggle between 30 days and All Time to force Unity to refresh.
This reads information from the database and updates your username to Hamster.
Populating scores in the database
You've created and validated the connection between Firebase and Unity. Next, you explore the leaderboard in MechaHamster, where it's useful to have multiple scores in your database.
Rather than playing through each of these mazes, you can populate the leaderboard by loading an existing JSON file that contains scores into the Firestore Realtime Database.
-
In the Realtime Database window in Firebase, import the
mechahamster-scores.jsonfile:- In the Firebase console, select your project.
- Select Develop > Database.
- In the Realtime Database window, click More.
- Select Import JSON.
-
Select Browse, and then navigate to your local mechamster repository and select the following file:
mechahamster/Resources/mechahamster-scores.json -
Click Import.
Editing the leaderboard controller interface in Unity
The leaderboard controller interface is created at runtime. It is editable only while play is active in the game window.
Go to the leaderboard controller interface
- Go to the Inspector window.
- In the Unity Project window, make sure that both the Inspector and Hierarchy windows are open.
- If the game is running, stop it by pressing the Play button in the Unity toolbar, and then click Play again to force a refresh of the database and start the game.
- In the Hierarchy window, select CameraHolder, and then open the arrow toggle to see its child objects.
- In the Game window, click Play!
- In the Game window, select Easy Peezie, and then click Top Times.
- In the Hierarchy window, select LeaderboardController (Clone) to load the leaderboard controller interface in the Inspector window.
Take a tour of the leaderboard controller interface
- In the Inspector window, open the arrow toggle in the Top Scores field of the Leaderboard Controller (Script).
-
In the Game window, toggle between 30 days and All Time.
As you toggle between 30 days and All Time, note the dates changing in the Get scores from field of the Inspector window.
As you toggle between 30 days and All Time, note the list of users changing in the Inspector window.
-
Change the value of Scores To Retrieve to 2.
-
Open the arrow toggle on one of the user names under Top Scores.
You see the user ID, username, score, and timestamp values that were retrieved from the database.
-
To change the sort order of the scores in the Game window, toggle the Low Scores Better setting.
Next steps for adapting the code to your project
Now that you've connected Firebase and Unity and explored the Leaderboard interface, you can learn more about incorporating this code into your project by reading the README file for the unity-projects/Firebase_Leaderboard GitHub project.
You can also explore the developer documentation and MechaHamster source code on GitHub. The game comes with well-documented code and examples for a variety of tools and functions, which can be modified to work for your game.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, remove them.
Delete the project
The simplest way to stop billing charges is to delete the project you created for this tutorial. Although you created the project in the Firebase console, you can also delete it in the Cloud Console, because the Firebase and GCP projects are the same.
- In the Cloud Console, go to the Manage resources page.
Go to Manage resources
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
How To Create A Database In Unity
Source: https://cloud.google.com/architecture/connecting-firebase-and-unity-to-create-a-gaming-leaderboard
Posted by: kearnsdureal.blogspot.com

0 Response to "How To Create A Database In Unity"
Post a Comment