我正在开发一个将 VR 应用程序集成到 Firebase 数据库中的项目。我想将应用程序中的用户名和登录信息存储到数据库中。
我有多个代码,例如用于用户注册的 C# 代码和 Firebase 初始化。我的数据库设置可以接受读取和写入。
using UnityEngine;
using Firebase;
using Firebase.Database;
using Firebase.Extensions;
public class FirebaseSetup : MonoBehaviour
{
private DatabaseReference dbReference;
void Start()
{
// Initialize Firebase Database
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
{
if (task.Result == DependencyStatus.Available)
{
// Get the default instance of the Firebase app
FirebaseApp app = FirebaseApp.DefaultInstance;
// Set the custom Firebase Realtime Database URL
string firebaseDatabaseURL = "https://silid-ai-939a6-default-rtdb.firebaseio.com/";
FirebaseDatabase.DefaultInstance.App.Options.DatabaseUrl = new System.Uri(firebaseDatabaseURL);
// Get the root reference location of the database
dbReference = FirebaseDatabase.DefaultInstance.RootReference;
Debug.Log("Firebase Initialized Successfully!");
}
else
{
Debug.LogError("Failed to initialize Firebase.");
// Handle initialization failure
}
});
}
public void WriteDataToDatabase(string key, string value)
{
// Write data to the database
dbReference.Child(key).SetValueAsync(value).ContinueWith(task =>
{
if (task.IsCompleted && !task.IsFaulted && !task.IsCanceled)
{
Debug.Log("Data written to the database successfully!");
}
else
{
Debug.LogError("Failed to write data to the database: " + task.Exception);
}
});
}
}
using UnityEngine;
using UnityEngine.SceneManagement; // Add this line for SceneManager
using UnityEngine.UI;
using Firebase;
using Firebase.Database;
using TMPro;
public class DatabaseManager : MonoBehaviour
{
public TMP_InputField firstnameField;
public TMP_InputField lastnameField;
public TMP_InputField usernameInput;
public TMP_InputField passwordInput;
private string userID;
private DatabaseReference dbReference;
void Start()
{
userID = SystemInfo.deviceUniqueIdentifier;
dbReference = FirebaseDatabase.DefaultInstance.RootReference;
}
public void CreateUser()
{
User newUser = new User(firstnameField.text, lastnameField.text, usernameInput.text, passwordInput.text);
string json = JsonUtility.ToJson(newUser);
dbReference.Child("users").Child(userID).SetRawJsonValueAsync(json);
}
}
public class User
{
public string firstnameInput;
public string lastnameInput;
public string usernameInput;
public string passwordInput;
public User(string firstnameInput, string lastnameInput, string usernameInput, string passwordInput)
{
this.passwordInput = firstnameInput;
this.passwordInput = lastnameInput;
this.usernameInput = usernameInput;
this.passwordInput = passwordInput;
}
}
Firebase 从版本 11.7.0 开始停止为 Oculus Quest 工作,因为它需要 Android SDK 版本 33 来编译,而 Quest 使用版本 32。显然,在测试和编译时不会出现问题,但当您将项目上传到 Meta Quest 时,不会出现问题能够。