Question: using System.Collections; using System.Collections.Generic; using UnityEngine; using Firebase; using Firebase.Database; using UnityEngine.UI; using TMPro; public class DBManager : MonoBehaviour { / / Firebase references public
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Firebase;
using Firebase.Database;
using UnityEngine.UI;
using TMPro;
public class DBManager : MonoBehaviour
Firebase references
public DatabaseReference usersRef;
UI input fields
public TMPInputField usernameInput;
public TMPInputField passwordInput;
void Start
Initialize Firebase asynchronously
StartCoroutineInitializeFirebase;
private IEnumerator InitializeFirebase
var task FirebaseApp.CheckAndFixDependenciesAsync;
yield return new WaitUntil task.IsCompleted;
if taskIsCanceled task.IsFaulted
Debug.LogErrorFirebase initialization error: task.Exception;
yield break;
else
Firebase initialization successful
usersRef FirebaseDatabase.DefaultInstance.GetReferenceUsers;
Debug.LogFirebase initialization completed";
Save user data to Firebase
public void SaveUser
string username usernameInput.text;
string password passwordInput.text;
Dictionary user new Dictionary;
userusername username;
userpassword password;
string key usersRef.PushKey;
usersRef.ChildkeyUpdateChildrenAsyncuser;
Get user data from Firebase
public void GetData
if stringIsNullOrEmptyusernameInputtext
Debug.LogWarningUsername field is empty";
return;
StartCoroutineGetUserData;
private IEnumerator GetUserData
string username usernameInput.text;
var task usersRef.ChildusernameGetValueAsync;
yield return new WaitUntil task.IsCompleted;
if taskIsCanceled task.IsFaulted
Debug.LogErrorFirebase data retrieval error: task.Exception;
yield break;
DataSnapshot snapshot task.Result;
if snapshot.Exists
Debug.LogWarningUser data does not exist for the specified username.";
yield break;
foreach DataSnapshot user in snapshot.Children
if userKey "password"
Debug.LogPassword: user.Value.ToString;
else if userKey "username"
Debug.LogUsername: user.Value.ToString;
User data does not exist for the specified username. error
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
