【问题标题】:How to get data from Firebase Realtime Database in Unity / C#如何在 Unity / C# 中从 Firebase 实时数据库获取数据
【发布时间】:2018-08-30 00:13:34
【问题描述】:

我在我的统一项目中使用 firebase。我在 start 方法中有一个变量 prodId ,将从 firebase 分配/获取。我插入了 2 个显示 prodId 值的 Debug.Logs。我想要的是首先调用 prodId 的第一个 Debug.Log,但发生的事情是首先调用第二个 Debug.Log。就像我希望在 Firebase 获取数据后发生第二个日志。

这是代码

public class Liquor : MonoBehaviour
{

    public GameObject[] gameObjects = new GameObject[24];
    public Material[] materialsI = new Material[3];

    // Use this for initialization
    void Start()
    {
        FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://shoplive-39a4f.firebaseio.com/");
        DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference;
        GameObject gameObject = new GameObject();
        string prodId=null;
        FirebaseDatabase.DefaultInstance
          .GetReference("products").Child(name)
          .GetValueAsync().ContinueWith(task => {
              if (task.IsFaulted)
              {
                  print("error"); // Handle the error...
              }
              else if (task.IsCompleted)
              {
                  DataSnapshot prod  = task.Result;
                  var values = prod.Value as Dictionary<string, object>;
                  foreach(var v in values)
                  {
                      if(v.Key=="prodId")
                      {
                          prodId = v.Value.ToString();
                          **Debug.Log(prodId);**
                      }
                  }

              }
          });


        //reference.Child("products").Child(product.prodId).SetRawJsonValueAsync(json);
        int productId = Int32.Parse(name);
        if(productId >=2 && productId <=4)
        {
            gameObject = Resources.Load("Prefabs/Liquor/beer2") as GameObject;
        } 
        if(productId >=7 && productId <=9)
        {
            gameObject = Resources.Load("Prefabs/Liquor/wine") as GameObject;
        }

        Vector3 tempPos = transform.position;
        float distZ = 0.023f;
        float distX = 0.027f;
        char[] productInfos = name.ToCharArray();
        int shelfGroup = productInfos[0];
        int row = productInfos[1];
        int col = productInfos[2];
        for (int i = 1; i < 11; i++)
        {
            gameObjects[i] = Instantiate(gameObject) as GameObject;
            gameObjects[i].transform.position = new Vector3(tempPos.x, tempPos.y, tempPos.z +distZ);
            **Debug.Log(prodId + "sample");**
            distZ += 0.023f;
        }
    }

【问题讨论】:

    标签: c# firebase unity3d firebase-realtime-database unity5


    【解决方案1】:

    通常将数据发送到 Firebase 服务器并等待响应需要一些时间。 这正是您的 Debug.Log(prodId); 线路工作晚的原因。
    我建议您将其他行放在Debug.Log(prodId); 行下方。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-17
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      • 2021-03-04
      • 2021-10-24
      相关资源
      最近更新 更多