Unity/Database

04. Firebase Realtime Database - Select

๐Ÿ”ท Select Data

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

// Firebase ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์„ ์–ธ
using Firebase;
using Firebase.Database;

// Firebase์— ์ €์žฅํ•  ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ
public class User
{
    public string userName;
    public int gold;

    // ์ƒ์„ฑ์ž
    public User(string _userName, int _gold)
    {
        this.userName = _userName;
        this.gold = _gold;
    }

}

public class FirebaseManager : MonoBehaviour
{
    // ์ €์žฅํ•  ๋ฐ์ดํ„ฐ UI
    public InputField userName;
    public InputField gold;

    // ๋กœ๋“œํ•œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•  UI
    public TMP_Text allDataField;
    private bool isLoad = false;
    private string allDataStr = "";

    // ์„ ํƒํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•  UI
    public TMP_Text selectDataField;
    private Query userNameQuery;

    // ํŒŒ์ด์–ด๋ฒ ์ด์Šค ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์ „์—ญ ์„ ์–ธ
    private DatabaseReference reference;
    // ํŒŒ์ด์–ด๋ฒ ์ด์Šค ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ณ ์œ ์ฃผ์†Œ(URI)
    private readonly string uri = "https://fir-demo-3c2bb-default-rtdb.firebaseio.com/";

    void Awake()
    {
        // ์•ฑ ์†์„ฑ ์ƒ์„ฑ
        AppOptions options = new AppOptions();
        options.DatabaseUrl = new System.Uri(uri);
        // ์•ฑ์„ ์ƒ์„ฑ
        FirebaseApp app = FirebaseApp.Create(options);
    }

    void Start()
    {
        reference = FirebaseDatabase.DefaultInstance.RootReference; // ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋ฃจํŠธ๋ฅผ ์ฐธ์กฐ

        allDataField.text = "Start";
    }

    // ๋ฐ์ดํ„ฐ ๋“ฑ๋ก
    public void InsertData()
    {
        // UI ์ž…๋ ฅ๊ฐ’
        string _userName = userName.text;
        int _gold = int.Parse(gold.text);

        // ๋ฐ์ดํ„ฐ ์ €์žฅ์„ ์œ„ํ•œ ํด๋ž˜์Šค ์ƒ์„ฑ
        User user = new User(_userName, _gold);

        // Json ํฌ๋งท์œผ๋กœ ํฌ๋งทํŒ…
        string json = JsonUtility.ToJson(user);
        // UserData ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ํ•˜์œ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€
        reference.Child("UserData").Child(_userName).SetRawJsonValueAsync(json);
    }

    /*
        ๋ฐ์ดํ„ฐ ์กฐํšŒ
        - GetValueAsync ์‚ฌ์šฉ
        - ๋น„๋™๊ธฐ ๋ฐฉ์‹, ํ˜ธ์ถœ์™„๋ฃŒ๋œ ํ›„ ๋ฐ์ดํ„ฐ ๊ฐ€๊ณต
    */
    public void LoadAllData()
    {
        // ์ง€์—ญ ๋ ˆํผ๋Ÿฐ์Šค ์„ ์–ธ
        DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference("UserData");
        // ๋ฐ์ดํ„ฐ ์กฐํšŒ
        reference.GetValueAsync().ContinueWith(task =>
        {
            if (task.IsFaulted)  // ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ ์‹คํŒจ
            {
                Debug.LogError("Failed load data!!!");
            }
            else if (task.IsCompleted)  // ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ ์„ฑ๊ณต
            {
                // ์Šค๋ƒ…์ƒท ์ƒ์„ฑ : ์กฐํšŒํ•œ ๋ฐ์ดํ„ฐ(๋ ˆ์ฝ”๋“œ)๋ฅผ ์ €์žฅํ•˜๋Š” ๋‹จ์œ„
                DataSnapshot snapshot = task.Result;

                // ๋ฐ์ดํ„ฐ ๊ฑด์ˆ˜ ์ถœ๋ ฅ
                Debug.Log($"๋ฐ์ดํ„ฐ ๋ ˆ์ฝ”๋“œ ๊ฐฏ์ˆ˜ : {snapshot.ChildrenCount}");
                // ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ
                allDataStr = "";
                foreach (DataSnapshot data in snapshot.Children)
                {
                    // DataSnapshot.Value ๋ฐ์ดํ„ฐ๋ฅผ ์ ‘๊ทผ
                    // {ํ‚ค}:{๊ฐ’} IDictionary ์ž๋ฃŒํ˜• ์‚ฌ์šฉ (Key:Value)
                    IDictionary _data = (IDictionary)data.Value;
                    allDataStr = allDataStr + $"Name : {_data["userName"]} , Gold : {_data["gold"]}\n";
                }
                isLoad = true;
            }
        });
        StartCoroutine(UpdateAllData());
    }

    IEnumerator UpdateAllData()
    {
        yield return new WaitUntil(() => isLoad);
        isLoad = false;
        allDataField.text = allDataStr;
    }


    // ํ‚ค ๊ฐ’์œผ๋กœ ๊ฒ€์ƒ‰
    public void SelectData()
    {
        // ๊ฒ€์ƒ‰ํ•  UI ์ž…๋ ฅํ•ญ๋ชฉ
        string _userName = userName.text;
        // ์ง€์—ญ ๋ ˆํผ๋Ÿฐ์Šค ์„ ์–ธ
        DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference("UserData");
        // ์ •๋ ฌ, ์ถ”์ถœ Query
        // Query userNameQuery = reference.OrderByChild("userName").EqualTo(_userName);
        userNameQuery = reference.OrderByChild("userName").EqualTo(_userName);



        // ๊ฒ€์ƒ‰
        // ๊ฒ€์ƒ‰์ด ์™„๋ฃŒ๋˜๋ฉด or ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋ฉด
        // ValueChanged : ์ด๋ฒคํŠธ ํ•จ์ˆ˜
        userNameQuery.ValueChanged += OnDataLoaded;
    }

    // Query ๊ฒฐ๊ณผ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
    void OnDataLoaded(object sender, ValueChangedEventArgs args)
    {
        // ๋ฐ์ดํ„ฐ ์กฐํšŒ ์Šค๋ƒ…์ƒท ์ •์˜
        DataSnapshot snapshot = args.Snapshot;

        foreach (var data in snapshot.Children)
        {
            IDictionary _data = (IDictionary)data.Value;
            selectDataField.text = $"Name : {_data["userName"]}, Gold : {_data["gold"]}";
        }
    }

}

- userNameQuery.ValueChanged += OnDataLoaded;

    - ํ•จ์ˆ˜ ์—ฐ๊ฒฐ. ์—ฐ๊ฒฐํ•˜๋ฉด์„œ OnDataLoaded ํ•จ์ˆ˜๊ฐ€ ๋ฌด์กฐ๊ฑด ํ•œ๋ฒˆ ์‹คํ–‰๋จ.

    - userNameQuery ๊ฐ’์ด ๋ณ€ํ•˜๋ฉด OnDataLoaded() ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰

 

 

 

- TMP_text ์ƒ์„ฑ : Text (TMP) - Select Data

 

 

 

- ์—ฐ๊ฒฐ

 

 

 

- ๋ฒ„ํŠผ ์ƒ์„ฑ : Button - Select

 

 

 

- ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ : SelectData()

 

 

 

- play

- Ojui_1 ์ž…๋ ฅ > Select ๋ฒ„ํŠผ ํด๋ฆญ

- ํ•ด๋‹น ๊ฐ’์ด ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธ

 

 

 

- Ojui_1, 900 ์ž…๋ ฅ > Insert ๋ฒ„ํŠผ ํด๋ฆญ

- Insert๋ฅผ ํ•˜์—ฌ Ojui_1์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋‹ˆ, Selectํ•œ ๋ฐ์ดํ„ฐ์˜ ๊ฐ’์ด ์ €์ ˆ๋กœ ์—…๋ฐ์ดํŠธ๋œ ๊ฒƒ์„ ํ™•์ธ

 

 

 

๐Ÿ”ถ ๊ฐ’ ๋ณ€๊ฒฝํ•  ๋•Œ๋งˆ๋‹ค ์ €์ ˆ๋กœ ์—…๋ฐ์ดํŠธ๋˜์ง€ ์•Š๋„๋ก ์ฒ˜๋ฆฌ

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

// Firebase ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์„ ์–ธ
using Firebase;
using Firebase.Database;

// Firebase์— ์ €์žฅํ•  ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ
public class User
{
    public string userName;
    public int gold;

    // ์ƒ์„ฑ์ž
    public User(string _userName, int _gold)
    {
        this.userName = _userName;
        this.gold = _gold;
    }

}

public class FirebaseManager : MonoBehaviour
{
    // ์ €์žฅํ•  ๋ฐ์ดํ„ฐ UI
    public InputField userName;
    public InputField gold;

    // ๋กœ๋“œํ•œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•  UI
    public TMP_Text allDataField;
    private bool isLoad = false;
    private string allDataStr = "";

    // ์„ ํƒํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•  UI
    public TMP_Text selectDataField;
    private Query userNameQuery;

    // ํŒŒ์ด์–ด๋ฒ ์ด์Šค ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์ „์—ญ ์„ ์–ธ
    private DatabaseReference reference;
    // ํŒŒ์ด์–ด๋ฒ ์ด์Šค ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ณ ์œ ์ฃผ์†Œ(URI)
    private readonly string uri = "https://fir-demo-3c2bb-default-rtdb.firebaseio.com/";

    void Awake()
    {
        // ์•ฑ ์†์„ฑ ์ƒ์„ฑ
        AppOptions options = new AppOptions();
        options.DatabaseUrl = new System.Uri(uri);
        // ์•ฑ์„ ์ƒ์„ฑ
        FirebaseApp app = FirebaseApp.Create(options);
    }

    void Start()
    {
        reference = FirebaseDatabase.DefaultInstance.RootReference; // ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋ฃจํŠธ๋ฅผ ์ฐธ์กฐ

        allDataField.text = "Start";
    }

    // ๋ฐ์ดํ„ฐ ๋“ฑ๋ก
    public void InsertData()
    {
        // UI ์ž…๋ ฅ๊ฐ’
        string _userName = userName.text;
        int _gold = int.Parse(gold.text);

        // ๋ฐ์ดํ„ฐ ์ €์žฅ์„ ์œ„ํ•œ ํด๋ž˜์Šค ์ƒ์„ฑ
        User user = new User(_userName, _gold);

        // Json ํฌ๋งท์œผ๋กœ ํฌ๋งทํŒ…
        string json = JsonUtility.ToJson(user);
        // UserData ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ํ•˜์œ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€
        reference.Child("UserData").Child(_userName).SetRawJsonValueAsync(json);
    }

    /*
        ๋ฐ์ดํ„ฐ ์กฐํšŒ
        - GetValueAsync ์‚ฌ์šฉ
        - ๋น„๋™๊ธฐ ๋ฐฉ์‹, ํ˜ธ์ถœ์™„๋ฃŒ๋œ ํ›„ ๋ฐ์ดํ„ฐ ๊ฐ€๊ณต
    */
    public void LoadAllData()
    {
        // ์ง€์—ญ ๋ ˆํผ๋Ÿฐ์Šค ์„ ์–ธ
        DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference("UserData");
        // ๋ฐ์ดํ„ฐ ์กฐํšŒ
        reference.GetValueAsync().ContinueWith(task =>
        {
            if (task.IsFaulted)  // ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ ์‹คํŒจ
            {
                Debug.LogError("Failed load data!!!");
            }
            else if (task.IsCompleted)  // ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ ์„ฑ๊ณต
            {
                // ์Šค๋ƒ…์ƒท ์ƒ์„ฑ : ์กฐํšŒํ•œ ๋ฐ์ดํ„ฐ(๋ ˆ์ฝ”๋“œ)๋ฅผ ์ €์žฅํ•˜๋Š” ๋‹จ์œ„
                DataSnapshot snapshot = task.Result;

                // ๋ฐ์ดํ„ฐ ๊ฑด์ˆ˜ ์ถœ๋ ฅ
                Debug.Log($"๋ฐ์ดํ„ฐ ๋ ˆ์ฝ”๋“œ ๊ฐฏ์ˆ˜ : {snapshot.ChildrenCount}");
                // ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ
                allDataStr = "";
                foreach (DataSnapshot data in snapshot.Children)
                {
                    // DataSnapshot.Value ๋ฐ์ดํ„ฐ๋ฅผ ์ ‘๊ทผ
                    // {ํ‚ค}:{๊ฐ’} IDictionary ์ž๋ฃŒํ˜• ์‚ฌ์šฉ (Key:Value)
                    IDictionary _data = (IDictionary)data.Value;
                    allDataStr = allDataStr + $"Name : {_data["userName"]} , Gold : {_data["gold"]}\n";
                }
                isLoad = true;
            }
        });
        StartCoroutine(UpdateAllData());
    }

    IEnumerator UpdateAllData()
    {
        yield return new WaitUntil(() => isLoad);
        isLoad = false;
        allDataField.text = allDataStr;
    }


    // ํ‚ค ๊ฐ’์œผ๋กœ ๊ฒ€์ƒ‰
    public void SelectData()
    {
        // ๊ฒ€์ƒ‰ํ•  UI ์ž…๋ ฅํ•ญ๋ชฉ
        string _userName = userName.text;
        // ์ง€์—ญ ๋ ˆํผ๋Ÿฐ์Šค ์„ ์–ธ
        DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference("UserData");
        // ์ •๋ ฌ, ์ถ”์ถœ Query
        // Query userNameQuery = reference.OrderByChild("userName").EqualTo(_userName);
        userNameQuery = reference.OrderByChild("userName").EqualTo(_userName);



        // ๊ฒ€์ƒ‰
        // ๊ฒ€์ƒ‰์ด ์™„๋ฃŒ๋˜๋ฉด or ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋ฉด
        // ValueChanged : ์ด๋ฒคํŠธ ํ•จ์ˆ˜
        userNameQuery.ValueChanged += OnDataLoaded;
    }

    // Query ๊ฒฐ๊ณผ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
    void OnDataLoaded(object sender, ValueChangedEventArgs args)
    {
        // ๋ฐ์ดํ„ฐ ์กฐํšŒ ์Šค๋ƒ…์ƒท ์ •์˜
        DataSnapshot snapshot = args.Snapshot;

        foreach (var data in snapshot.Children)
        {
            IDictionary _data = (IDictionary)data.Value;
            selectDataField.text = $"Name : {_data["userName"]}, Gold : {_data["gold"]}";
        }
        userNameQuery.ValueChanged -= OnDataLoaded;
    }

}

- userNameQuery.ValueChanged -= OnDataLoaded; : ์ถ”๊ฐ€

 

 

 

- Ojui_1์„ Select >  Ojui_1, 100 Insert > Load

- ๊ฐ’์ด ๋ฐ”๋€Œ์–ด๋„ ์ €์ ˆ๋กœ ์—…๋ฐ์ดํŠธ๋˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ํ™•์ธ

'Unity > Database' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

06. Firebase Realtime Database - ์‹ฌํ™”  (1) 2021.08.15
05. Firebase Realtime Database - Delete  (0) 2021.08.14
03. Firebase Realtime Database - Load  (0) 2021.08.12
02. Firebase Realtime Database - Insert  (0) 2021.08.11
01. Firebase Setting  (0) 2021.08.10