简体   繁体   中英

Can not retrieve data from firebase realtime database in unity3d

I am new in unity firebase developer. Basically, I made a function GetPlayerPrize to get the specific (login user) prize detail from firebase. But I cannot get any response while retrieving data from the firebase realtime database.

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();
        }
    });

}
}

Reference your default instance with a task?

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM