๐ท ์ฃผ์ํ ์
...์ ๋ง ๋ณ ๊ฑฐ ์๋ ๊ฑธ๋ก ๋ช์๊ฐ์ฉ์ด๋ ์๋นํด์ ํ๋ค์๋ค.
๋ฐ์ดํฐ๋ฅผ ๋ก๋ํ๋ ๊ฒ์ ์ฑ๊ณตํ์์ง๋ง, 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 |