我是统一Firebase开发人员的新手。基本上,我做了一个函数GetPlayerPrize
,用于从firebase获取特定(登录用户)奖品的详细信息。但是从Firebase实时数据库检索数据时,我无法得到任何响应。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Firebase;
using Firebase.Auth;
using Firebase.Database;
using Firebase.Unity.Editor;
public class prizeCanvasScript : MonoBehaviour
{
public Text silver;
public Text gold;
public Button silverButton;
public Button GoldButton;
public Firebase.Auth.FirebaseAuth auth;
public Firebase.Auth.FirebaseUser user;
public void Start() {
InitializeFirebase();
}
public void InitializeFirebase()
{
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://robberbird.firebaseio.com/");
auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
user = auth.CurrentUser;
GetPlayerPrize();
}
void GetPlayerPrize()
{
Debug.Log("Getting player data" + user.UserId);
FirebaseDatabase.DefaultInstance
.GetReference("users")
.Child(user.UserId)
.GetValueAsync()
.ContinueWith((task) =>{
if (task.IsFaulted)
{
Debug.LogError("Error retriving user data: " + user.UserId);
// Handle the error...
}
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
Debug.Log("snapshot" );
Debug.Log(snapshot);
// silver.text = snapshot.silverChest.ToString();
// gold.text = snapshot.GoldChest.ToString();
}
});
}
}
答案 0 :(得分:0)
使用任务引用默认实例吗?
static Task DI = new System.Threading.Tasks.Task(LoginAnon);
public static void LoginAnon()
{
DI = FirebaseAuth.DefaultInstance.SignInAnonymouslyAsync().ContinueWith(result =>
{
FirebaseDatabase.DefaultInstance.GetReference("Lobbies/").ChildAdded += HandleLobbyAdded;
FirebaseDatabase.DefaultInstance.GetReference("Lobbies/").ChildRemoved += HandleLobbyRemoved;
});
}
..
public static void HandleLobbyRemoved(object sender, ChildChangedEventArgs e)
{
DataSnapshot snap = e.Snapshot;
if (e.DatabaseError != null)
{
Debug.LogError(e.DatabaseError.Message);
return;
}
//User who created the lobby
string uID = snap.Key.ToString();
}