unity firebase show data code example

Example 1: unity get data from firebase

// Perform the steps at the following link to setup Firebase
// for your Unity project:
https://firebase.google.com/docs/unity/setup

// To connect to the databse in a script, use the following
// two lines:
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://YOURPROJECTNAME.firebaseio.com/");
DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference;

// To get a data snapshot and take action with it:
FirebaseDatabase.DefaultInstance.GetReference("DATABASENAME").GetValueAsync().ContinueWith(task => {
	if (task.IsFaulted)
	{
		// Failure
	}
	else if (task.IsCompleted)
	{
		DataSnapshot snapshot = task.Result;
		// Success
	}
});

Example 2: how to go through child in firebase Unity

void InitializeFirebase() {


    FirebaseApp app = FirebaseApp.DefaultInstance;
    app.SetEditorDatabaseUrl ("https://slol.firebaseio.com/");

    FirebaseDatabase.DefaultInstance
        .GetReference ("Products").OrderByChild ("category").EqualTo("livingroom")
        .ValueChanged += (object sender2, ValueChangedEventArgs e2) => {
        if (e2.DatabaseError != null) {
            Debug.LogError (e2.DatabaseError.Message);
        }


        if (e2.Snapshot != null && e2.Snapshot.ChildrenCount > 0) {

            foreach (var childSnapshot in e2.Snapshot.Children) {
                var name = childSnapshot.Child ("name").Value.ToString (); 

                text.text = name.ToString();
                Debug.Log(name.ToString());
                //text.text = childSnapshot.ToString();

            }

        }

    };
}

Tags:

Go Example