Unity/Database

03. Firebase Realtime Database - Load

๐Ÿ”ท ์ฃผ์˜ํ•  ์ 

...์ •๋ง ๋ณ„ ๊ฑฐ ์•„๋‹Œ ๊ฑธ๋กœ ๋ช‡์‹œ๊ฐ„์”ฉ์ด๋‚˜ ์†Œ๋น„ํ•ด์„œ ํž˜๋“ค์—ˆ๋‹ค.

๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ์€ ์„ฑ๊ณตํ•˜์˜€์ง€๋งŒ, UI์˜ text๊ฐ€ ๋ฐ”๋€Œ์ง€ ์•Š์•„์„œ ๊ณจ๋จธ๋ฆฌ๋ฅผ ์ฉ์—ˆ๋‹ค.

 

 

 

ํ…์ŠคํŠธ๋Š” ๋ฐ”๋€Œ์ง€๋งŒ UI์˜ text๊ฐ€ ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค.

๋ฌธ์ œ๋Š” task ์•ˆ์ชฝ์—๋‹ค๊ฐ€ UI.text๋ฅผ ๋ฐ”๊พธ๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์„œ ๊ทธ๋Ÿฐ ๊ฒƒ์ด์—ˆ๋‹ค.

๋ฐ์ดํ„ฐ๊ฐ€ ๋กœ๋“œ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€ UI.text๋ฅผ ๋ฐ”๊พธ๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋‹ˆ ํ•ด๊ฒฐ๋˜์—ˆ๋‹ค.

 

 

๐Ÿ”ท Load 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 = "";

    // ํŒŒ์ด์–ด๋ฒ ์ด์Šค ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์ „์—ญ ์„ ์–ธ
    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;
    }
}

 

 

 

- TMP_text ์ถ”๊ฐ€ : Text (TMP) - All Data

 

 

 

- ์—ฐ๊ฒฐ

 

 

 

- play > Load ๋ฒ„ํŠผ ํด๋ฆญ

- ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ ํ™•์ธ

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

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