简体   繁体   English

unity-从Firebase数据库检索数据

[英]unity - retrieving data from firebase database

I am new to firebase and somewhat new to Unity (also, this is my first stack exchange post). 我是Firebase的新手,也是Unity的新手(同样,这是我的第一个堆栈交换文章)。 I know how to write to firebase, but I do not know how to retrieve from the data tree. 我知道如何写入Firebase,但不知道如何从数据树中检索。

The way my data is structured (roughly) is as follows: 我的数据的结构方式(大致)如下:

{
Users:{
    "Email": , 
    "Password":
    }
}

How would I access the elements of my users and retrieve such things as their email and password? 我将如何访问用户的元素并检索其电子邮件和密码之类的东西?

I'm in the same boat as you (new to both firebase and unity), but something like the following worked for me. 我和你在同一条船上(无论是对火力阵营还是对团结来说都是新手),但是类似下面的内容对我来说却很有效。 I adapted it to your scenario without testing, so might have minor bugs but it should get you started. 我未经测试就将其调整为适合您的情况,因此可能会有一些小错误,但它应该可以帮助您入门。 Perhaps someone more experienced can improve upon it. 也许更有经验的人可以对此进行改进。 I'm intentionally sparing details on proper handling of passwords. 我故意保留有关正确处理密码的详细信息。

   //assuming
         public class User {


            public string email;
            public string password;

            public User (string email, string password) {
                this.email = email;
                this.password = password;
            }
        }

    //inside some class
        public void AddUser(){
          User user = new User("email@email.com", "password");
          string json = JsonUtility.ToJson(user);
          Firebase.Database.DatabaseReference dbRef = Firebase.Database.FirebaseDatabase.DefaultInstance.RootReference
          dbRef.Child("users").Push().SetRawJsonValueAsync(json);
        }

        public void GetUsers(){
          Firebase.Database.FirebaseDatabase dbInstance = Firebase.Database.FirebaseDatabase.DefaultInstance;
          dbInstance.GetReference("users").GetValueAsync().ContinueWith(task => {
                    if (task.IsFaulted) {
                        // Handle the error...
                    }
                    else if (task.IsCompleted) {
                      DataSnapshot snapshot = task.Result;
                      foreach ( DataSnapshot user in snapshot.Children){
                        IDictionary dictUser = (IDictionary)user.Value;
                        Debug.Log ("" + dictUser["email"] + " - " + dictUser["password"]);
                      }
                    }
          });

By creatively copy/pasting from the documentation page I get to this: 通过创造性地从文档页面复制/粘贴,可以达到以下目的:

FirebaseDatabase.DefaultInstance
  .GetReference("Users")
  .ValueChanged += HandleValueChanged;
}

void HandleValueChanged(object sender, ValueChangedEventArgs args) {
  if (args.DatabaseError != null) {
    Debug.LogError(args.DatabaseError.Message);
    return;
  }
  Debug.Log(arg.Snapshot.Child("Email").Value)
}

Ik this is an old post but it might help someone, 好像这是一个旧帖子,但可能会对某人有所帮助,

Just call SaveAndRetrieveData() function to save and retrieve data where ever you want 只需调用SaveAndRetrieveData()函数即可在需要的任何地方保存和检索数据

public void SaveAndRetrieveData() //from the database (server)...
{
    //store the data to the server...
    reference.Child("My Car Collections").Child("Sports Car").SetValueAsync("Ferrari");
    print("data saved");
    //Retrieve the data and convert it to string...
    FirebaseDatabase.DefaultInstance.GetReference("My Car Collections").GetValueAsync().ContinueWith(task => 
    {  
        DataSnapshot snapshot = task.Result;
        string ss = snapshot.Child("Sports Car").Value.ToString();
        print(ss);
        print("data retrieved");
    });        
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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