简体   繁体   中英

How to get data from Firebase Realtime Database in Unity / C#

I use firebase in my unity project. I have a variable prodId inside the start method and will be assign/fetch from firebase. I insert a 2 Debug.Logs that show the value of prodId. What I want is the first Debug.Log for prodId is to be call first but what happening is that second Debug.Log is called first. It's like I want the 2nd log to happen once the firebase fetched the data.

Here's the code

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

It is very usual sending data to Firebase server and waiting for the response takes some time. This is exactly why your Debug.Log(prodId); line works late.
I suggest you to put the other lines down below Debug.Log(prodId); line.

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