๐ท 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 |