Start()
, a LevelStart event will be logged
and OnDestroy()
a LevelEnd will be logged
To change scenes when clicking a button. A OnChangeLevelClick.cs is created. I learned that we can use the interface UnityEngine.EventSystems.IPointerClickHandler
to click buttons. There is a OnPointerClick
.
In FirebaseMainMenu scene. ChangeLevelCllick.cs is attached to the button "MainMenu". Nothing needs to be the on the OnClick in the inspector. Simply attach the script, fill in the scene name and when you press the button, scene will change (assuming you added the scenes in build settings)
In FirebaseLevel1 Scene, the LevelLoginBehaviour is attached so that everytime we enter and exit FirebaseLevel1 scene, the event will be logged.
NOTE THIS PART IS FOR ANDROID ONLY: Then I make a build, sideload. I want to see my log events in Firebase DebugView. To do that, I needed to do somethings in the command line. First we need to set the events from the device to firebase. Events are cached and sent to firebase in batches. With this property set, we send it automatically adb shell setprop debug.firebase.analytics.app insert-your-package-name
. Secondly, to actually print analytics information to the terminal adb shell setprop log.tag.FA VERBOSE
. And then thirdly adb shell setprop log.tag.FA-SVC VERBOSE
. Fourth, now launch the app. Then you need to contract FA and FA-SVC tag. adb logcat -s "FA","FA-SVC"
. Lastly, when you are done debugging adb shell setprop debug.firebase.analytics.app .none
In Firebase DebugView, I will see the events! So at least I know events are working. Now to wait 24 hours. Stay tuned...
I checked after 12 hours. The LevelStart and LevelEnd event exists!!
I can get analytics but remember our goal is to store user data like weapons. So next let's figure out how to store user login using Firebase Authentication. I'll try to follow this link. First I went into my project on firebase > project settings > Service Accounts > Generate new private key. A warning pops up saying "Your private key gives access to your project's Firebase services. Keep it confidential and never store it in a public repository.Store this file securely, because your new key can't be recovered if lost."
However, after I did that and reread the title of the documentation, it says it is for Custom server. The instructions says to copy this file to your authentication server. I don't have a custom server. I want to use firebase own server! Instead, I should be following this Get started link. I'll need to import the FirebaseAuth.Unitypackage from this Firebase Unity SDK. This link will automatically download sdk In firebase, I went to my project > Authentication > Sign in method > Email/Password Enable Email/Password Back into Unity. I created a script called FirebaseLogin. Instead of creating a login UI. I'm hardcoding the email which I retrieved from this temp email website and the password. Pressing L will run the code In firebase Authentication, I can see the email login information! If I press L again, it will recognize that this user already exist. Now the user registers, can the user login? Continuing following the get started link. I added this piece of code, if user presses "S", the user should be logged in If I passed in the wrong email or password, I would get an error If I pass in the correct information then there will be a debug log that the user signed in successfully On firebase I can see the Sign in date has changed So it's great I can sign users in. There are also other ways to manage users like sign out, delete users, update password, send verification email etc. However, how do I store information in a database? There is firestore database - Cloud firestore. I want to see if I can use this to my advantage to store things like what weapons users has bought. Storing data in Cloud firestore can do things like allowing users to access their information across devices or share progress with other players. I will follow this Get Started for Cloud Firestore I clicked create database button. First the prompt asked me to select a location. There is a warning "After you set this location, you cannot change it later. Also, this location setting will be the location for your default Cloud Storage bucket. " The secure rules I select is "start in test mode", because I am not familiar with cloud firestore yet. The warning says "The default security rules for test mode allow anyone with your database reference to view, edit and delete all data in your database for the next 30 days ". I am then provided with this page There is instructions regarding "You must minify the build to avoid the message Error while merging dex archives". I'm curious to see when this error will appear so I'm not going to look into how to configure this yet until this error appears I then imported the Firebase.Firestore package so that I can use the Firebase.Firestore namespace later I added theusing Firebase.Firestore;
and using Firebase.Extensions;
. I added the code if user press D, Create a new collection and a document. Note that when adding data, as the Get started says "Cloud Firestore stores data in Documents, which are stored in Collections. Cloud Firestore creates collections and documents implicitly the first time you add data to the document. You do not need to explicitly create collections or documents"
In Cloud firestore, you can see the data is added. The alovelace document is added to the users collection
Now I want to add data to the aturing document in the users collection.
In Cloud Firestore we see the users collection contains the aturing document. Notice that this document includes a key-value pair (middle name) that does not appear in the first document. Documents in a collection can contain different sets of information
Note that if the same document name is sent, it will replace the data. For example. If I pass in aturing document again and change Born value to a different year. On Cloud Firestore, I will see the Born value is different
If I select Query builder. The users collection will display these information. Notice all the key value are displayed. However, the "middle" name for alovelace DocumentID doesn't exist
If I manually add a Middle name field to the alovelace document like this in Cloud firestore
If I go to query builder, I can see the "Middle" field is added!
This document is getting long! I will create another blog and continue this conversation on Firebase! So far it seems like firebase will be the best choice to store data! See next blog here