Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

I want to retrieve data from Firebase to unity on Texmashpro text filed

Discussion in 'AR' started by pilancer_07, Sep 19, 2020.

?

Is it possible to display data from firebase to unity text filed? ( Text Mashpro )

  1. Yes

    100.0%
  2. No

    0 vote(s)
    0.0%
  1. pilancer_07

    pilancer_07

    Joined:
    Jun 11, 2020
    Posts:
    3
    Hello Unity.

    I'm facing an bad issues on my Unity project, I'm developing an AR project, where i have some user input on my APP. When user fill those fields, it will store those data to firebase Real-time database. I have 2 script, 1) Data saving from apps to firebase 2) Data retrieving from firebase to my APP text field.Till now i'm able to store data to firebase, but i'm not able to retrieving data from firebase to my app scene.Here is my Data Retrieving code: Can you please take a look where is the problem occurring please.




    upload_2020-9-19_19-45-42.png

    Pic 1:

    This is the user Input field where , user will input data, to store in Firebase "Real-time Database", I have done this section, and it can save data to Firebase. Now....


    upload_2020-9-19_19-47-11.png

    Pic 2.

    THis is the Interface where i want to show data from firebase.

    ########I'm using this code to retrieve data from firebase, but there is some conflict that is, every time, when i input a new data from the Pic 1. Scene. It update on firebase console. And then scan the barcode, it shows those info in Pic 2, Interface. but after again i close the app and try scan again, that time it not showing any data info on the pic 2, Interface.



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5. using TMPro;
    6. using Firebase;
    7. using Firebase.Database;
    8. using Firebase.Unity.Editor;
    9. using UnityEngine.UI;
    10. [System.Serializable]
    11. public class PumpData
    12. {
    13.     public string pumpName;
    14.     public string pumpTemp;
    15.     public string pumpHealth;
    16.     public string pumpVibx, pumpViby, pumpVibz;
    17. }
    18. [System.Serializable]
    19. public class MotorData
    20. {
    21.     public string pumpName;
    22.     public string motorTemp;
    23.     public string motorHealth;
    24.     public string motorVibx, motorViby, motorVibz;
    25. }
    26. [System.Serializable]
    27. public class GearData
    28. {
    29.     public string pumpName;
    30.     public string gearTemp;
    31.     public string gearHealth;
    32.     public string gearVibx, gearViby, gearVibz;
    33. }
    34. public class FirebaseDisplayingData : MonoBehaviour
    35. {
    36.     public string barcodeOutput;
    37.     // name of tables where data is stored in database
    38.     [SerializeField] private string pumpDatabaseTableName, motorDatabaseTableName, gearDatabaseTableName;
    39.     // text input values references from scene
    40.     [SerializeField] private TextMeshProUGUI pumpHealthh, pumpTempp, pumpVibxx, pumpVibyy, pumpVibzz;
    41.     [SerializeField] private TextMeshProUGUI motorHealthh, motorTempp, motorVibxx, motorVibyy, motorVibzz;
    42.     [SerializeField] private TextMeshProUGUI gearHealthh, gearTempp, gearVibxx, gearVibyy, gearVibzz;
    43.     // simplay array that contains special characters
    44.     char[] commas = { '"' };
    45.     // firebase database address fetched from firebase console and can be input from inspector in scene view
    46.     [SerializeField] private string databaseAddress;
    47.     [SerializeField] List<PumpData> pumps = new List<PumpData>();
    48.     [SerializeField] List<GearData> gears = new List<GearData>();
    49.     [SerializeField] List<MotorData> motors = new List<MotorData>();
    50.     void Start()
    51.     {
    52.         FirebaseApp.DefaultInstance.SetEditorDatabaseUrl(databaseAddress);
    53.         PopulatingDataFromDiffTables();
    54.         //    ComparingAndDisplayingData();
    55.     }
    56.     void PopulatingDataFromDiffTables()
    57.     {
    58.         FetchingGears();
    59.         FetchingPumps();
    60.         FetchingMotors();
    61.     }
    62.     void FetchingPumps()
    63.     {
    64.         FirebaseDatabase.DefaultInstance.GetReference(pumpDatabaseTableName).GetValueAsync().ContinueWith(t =>
    65.                {
    66.                    if (t.IsCompleted)
    67.                    {
    68.                        DataSnapshot data = t.Result;
    69.                        JSONObject pumpData = new JSONObject(data.GetRawJsonValue());
    70.                        for (int i = 0; i < pumpData.Count; i++)
    71.                        {
    72.                            JSONObject jS = new JSONObject(pumpData[i].ToString());
    73.                            string healthPump = jS["pumpHealth"].ToString().Trim(new char[1] { '"' });
    74.                            string namePump = jS["pumpName"].ToString().Trim(new char[1] { '"' });
    75.                            string tempPump = jS["pumpTemperature"].ToString().Trim(new char[1] { '"' });
    76.                            string vibxPump = jS["pumpVibx"].ToString().Trim(new char[1] { '"' });
    77.                            string vibyPump = jS["pumpViby"].ToString().Trim(new char[1] { '"' });
    78.                            string vibzPump = jS["pumpVibz"].ToString().Trim(new char[1] { '"' });
    79.                            PumpData thisData = new PumpData();
    80.                            thisData.pumpName = namePump;
    81.                            thisData.pumpHealth = healthPump;
    82.                            thisData.pumpTemp = tempPump;
    83.                            thisData.pumpVibx = vibxPump;
    84.                            thisData.pumpViby = vibyPump;
    85.                            thisData.pumpVibz = vibzPump;
    86.                            pumps.Add(thisData);
    87.                        }
    88.                        DisplayingPump();
    89.                    }
    90.                });
    91.     }
    92.     void FetchingGears()
    93.     {
    94.         FirebaseDatabase.DefaultInstance.GetReference(gearDatabaseTableName).GetValueAsync().ContinueWith(t =>
    95.                {
    96.                    if (t.IsCompleted)
    97.                    {
    98.                        DataSnapshot data = t.Result;
    99.                        JSONObject gearData = new JSONObject(data.GetRawJsonValue());
    100.                        for (int i = 0; i < gearData.Count; i++)
    101.                        {
    102.                            JSONObject jS = new JSONObject(gearData[i].ToString());
    103.                            string namePump = jS["pumpName"].ToString().Trim(new char[1] { '"' });
    104.                            string healthgear = jS["gearHealth"].ToString().Trim(new char[1] { '"' });
    105.                            string tempgear = jS["gearTemperature"].ToString().Trim(new char[1] { '"' });
    106.                            string vibxgear = jS["gearVibx"].ToString().Trim(new char[1] { '"' });
    107.                            string vibygear = jS["gearViby"].ToString().Trim(new char[1] { '"' });
    108.                            string vibzgear = jS["gearVibz"].ToString().Trim(new char[1] { '"' });
    109.                            GearData thisData = new GearData();
    110.                            thisData.gearHealth = healthgear;
    111.                            thisData.pumpName = namePump;
    112.                            thisData.gearTemp = tempgear;
    113.                            thisData.gearVibx = vibxgear;
    114.                            thisData.gearViby = vibygear;
    115.                            thisData.gearVibz = vibzgear;
    116.                            gears.Add(thisData);
    117.                        }
    118.                        DisplayingGear();
    119.                    }
    120.                });
    121.     }
    122.     void FetchingMotors()
    123.     {
    124.         FirebaseDatabase.DefaultInstance.GetReference(motorDatabaseTableName).GetValueAsync().ContinueWith(t =>
    125.                {
    126.                    if (t.IsCompleted)
    127.                    {
    128.                        DataSnapshot data = t.Result;
    129.                        JSONObject motorData = new JSONObject(data.GetRawJsonValue());
    130.                        for (int i = 0; i < motorData.Count; i++)
    131.                        {
    132.                            JSONObject jS = new JSONObject(motorData[i].ToString());
    133.                            string namePump = jS["pumpName"].ToString().Trim(new char[1] { '"' });
    134.                            string healthmotor = jS["motorHealth"].ToString().Trim(new char[1] { '"' });
    135.                            string tempmotor = jS["motorTemperature"].ToString().Trim(new char[1] { '"' });
    136.                            string vibxmotor = jS["motorVibx"].ToString().Trim(new char[1] { '"' });
    137.                            string vibymotor = jS["motorViby"].ToString().Trim(new char[1] { '"' });
    138.                            string vibzmotor = jS["motorVibz"].ToString().Trim(new char[1] { '"' });
    139.                            MotorData thisData = new MotorData();
    140.                            thisData.motorHealth = healthmotor;
    141.                            thisData.motorTemp = tempmotor;
    142.                            thisData.pumpName = namePump;
    143.                            thisData.motorVibx = vibxmotor;
    144.                            thisData.motorViby = vibymotor;
    145.                            thisData.motorVibz = vibzmotor;
    146.                            motors.Add(thisData);
    147.                        }
    148.                        DisplayingMotor();
    149.                    }
    150.                });
    151.     }
    152.     void ComparingAndDisplayingData()
    153.     {
    154.         DisplayingPump();
    155.         DisplayingGear();
    156.         DisplayingMotor();
    157.     }
    158.     void DisplayingPump()
    159.     {
    160.         for (int n = 0; n < pumps.Count; n++)
    161.         {
    162.             if (pumps[n].pumpName == barcodeOutput)
    163.             {
    164.                 pumpHealthh.text = "" + pumps[n].pumpHealth;
    165.                 pumpTempp.text = "" + pumps[n].pumpTemp;
    166.                 pumpVibxx.text = "" + pumps[n].pumpVibx;
    167.                 pumpVibyy.text = "" + pumps[n].pumpViby;
    168.                 pumpVibzz.text = "" + pumps[n].pumpVibz;
    169.                 Debug.Log("Displaying pump");
    170.                 break;
    171.             }
    172.         }
    173.     }
    174.     void DisplayingGear()
    175.     {
    176.         for (int n = 0; n < gears.Count; n++)
    177.         {
    178.             if (gears[n].pumpName == barcodeOutput)
    179.             {
    180.                 gearHealthh.text = "" + gears[n].gearHealth;
    181.                 gearTempp.text = "" + gears[n].gearTemp;
    182.                 gearVibxx.text = "" + gears[n].gearVibx;
    183.                 gearVibyy.text = "" + gears[n].gearViby;
    184.                 gearVibzz.text = "" + gears[n].gearVibz;
    185.                 Debug.Log("gear data");
    186.                 break;
    187.             }
    188.         }
    189.     }
    190.     void DisplayingMotor()
    191.     {
    192.         for (int n = 0; n < motors.Count; n++)
    193.         {
    194.             if (motors[n].pumpName == barcodeOutput)
    195.             {
    196.                 motorHealthh.text = "" + motors[n].motorHealth;
    197.                 motorTempp.text = "" + motors[n].motorTemp;
    198.                 motorVibxx.text = "" + motors[n].motorVibx;
    199.                 motorVibyy.text = "" + motors[n].motorViby;
    200.                 motorVibzz.text = "" + motors[n].motorVibz;
    201.                 Debug.Log("motor data");
    202.                 break;
    203.             }
    204.         }
    205.     }
    206.     // Update is called once per frame
    207.     void Update()
    208.     {    }
    209. }
     
  2. A_S_B

    A_S_B

    Joined:
    Dec 11, 2018
    Posts:
    2
    estas cargando los datos al abrir la aplicacion?
     
  3. pilancer_07

    pilancer_07

    Joined:
    Jun 11, 2020
    Posts:
    3
    Yes, When i press the play button on the unity to run my project , on the game scene view, when i update the data and then scan the QR code, it show the data details, but again when i close the play mode and run again it not showing any value on the text field, as well we i build the apk and run on my phone it not even save the data to firebase console, and nothing showing on display.
     
  4. charuvdhn02

    charuvdhn02

    Joined:
    Oct 2, 2020
    Posts:
    3
    Heyy did you found out the solution?