๐ท Firebase ์ฌํ
๋ฐ์ดํฐ ๊ตฌ์กฐ๊ฐ ์์ ํฌ์คํ ํ ์์ ์ฒ๋ผ ๊ฐ๋จํ์ง ์์ ๊ฒฝ์ฐ,
์๋์ ๊ธ์ ์ฐธ์กฐ
๐ถ ํ์ด์ด๋ฒ ์ด์ค ๋ฐ์ดํฐ ๊ตฌ์กฐ
- ์์ ๊ฐ์ด ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ์์ฑ
๐ท Load data
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
// Firebase ๋ค์์คํ์ด์ค ์ ์ธ
using Firebase;
using Firebase.Database;
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;
}
public void LoadAllData()
{
// ์ง์ญ ๋ ํผ๋ฐ์ค ์ ์ธ
DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference("student");
// ๋ฐ์ดํฐ ์กฐํ
reference.GetValueAsync().ContinueWith(task =>
{
if (task.IsFaulted)
{
Debug.LogError("Failed load data!!!");
}
else if (task.IsCompleted)
{
// ์ค๋
์ท ์์ฑ : ์กฐํํ ๋ฐ์ดํฐ(๋ ์ฝ๋)๋ฅผ ์ ์ฅํ๋ ๋จ์
DataSnapshot snapshot = task.Result;
// ๋ฐ์ดํฐ ์ถ๋ ฅ
allDataStr = "";
// code๊น์ง ์ ๊ทผ (0000,0001 ...)
foreach (DataSnapshot data in snapshot.Children)
{
IDictionary CodeData = (IDictionary)data.Value;
allDataStr += $"code : {CodeData["code"]}\n";
// info์ ์ ๊ทผ
DataSnapshot info = data.Child("info");
IDictionary infoData = (IDictionary)info.Value;
allDataStr += $"name: {infoData["name"]}, age: {infoData["age"]}, gender: {infoData["gender"]}\n";
// grade์ ์ ๊ทผ
DataSnapshot grade = data.Child("grade");
IDictionary gradeData = (IDictionary)grade.Value;
allDataStr += $"K: {gradeData["Korean"]}, E : {gradeData["English"]}, M : {gradeData["Math"]}, S : {gradeData["Science"]}\n";
allDataStr += "--------------------------------------\n\n";
}
isLoad = true;
}
});
StartCoroutine(UpdateAllData());
}
IEnumerator UpdateAllData()
{
yield return new WaitUntil(() => isLoad);
isLoad = false;
allDataField.text = allDataStr;
}
}
- Load ๋ฒํผ ํด๋ฆญ
- ๋ฐ์ดํฐ ์ ๋ถ๋ฌ์ค๋์ง ํ์ธ
๐ท Select data
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
// Firebase ๋ค์์คํ์ด์ค ์ ์ธ
using Firebase;
using Firebase.Database;
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;
}
public void LoadAllData()
{
// ์ง์ญ ๋ ํผ๋ฐ์ค ์ ์ธ
DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference("student");
// ๋ฐ์ดํฐ ์กฐํ
reference.GetValueAsync().ContinueWith(task =>
{
if (task.IsFaulted)
{
Debug.LogError("Failed load data!!!");
}
else if (task.IsCompleted)
{
// ์ค๋
์ท ์์ฑ : ์กฐํํ ๋ฐ์ดํฐ(๋ ์ฝ๋)๋ฅผ ์ ์ฅํ๋ ๋จ์
DataSnapshot snapshot = task.Result;
// ๋ฐ์ดํฐ ์ถ๋ ฅ
allDataStr = "";
// code๊น์ง ์ ๊ทผ (0000,0001 ...)
foreach (DataSnapshot data in snapshot.Children)
{
IDictionary CodeData = (IDictionary)data.Value;
allDataStr += $"code : {CodeData["code"]}\n";
// info์ ์ ๊ทผ
DataSnapshot info = data.Child("info");
IDictionary infoData = (IDictionary)info.Value;
allDataStr += $"name: {infoData["name"]}, age: {infoData["age"]}, gender: {infoData["gender"]}\n";
// grade์ ์ ๊ทผ
DataSnapshot grade = data.Child("grade");
IDictionary gradeData = (IDictionary)grade.Value;
allDataStr += $"K: {gradeData["Korean"]}, E : {gradeData["English"]}, M : {gradeData["Math"]}, S : {gradeData["Science"]}\n";
allDataStr += "--------------------------------------\n\n";
}
isLoad = true;
}
});
StartCoroutine(UpdateAllData());
}
IEnumerator UpdateAllData()
{
yield return new WaitUntil(() => isLoad);
isLoad = false;
allDataField.text = allDataStr;
}
// ํค ๊ฐ์ผ๋ก ๊ฒ์
public void SelectData()
{
// ๊ฒ์ํ UI ์
๋ ฅํญ๋ชฉ
string code = userName.text;
// ์ง์ญ ๋ ํผ๋ฐ์ค ์ ์ธ
DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference("student");
// ์ ๋ ฌ, ์ถ์ถ Query
userNameQuery = reference.OrderByChild("code").EqualTo(code);
// ๊ฒ์
userNameQuery.ValueChanged += OnDataLoaded;
}
// Query ๊ฒฐ๊ณผ๊ฐ์ ์ถ๋ ฅํ๋ ํจ์
void OnDataLoaded(object sender, ValueChangedEventArgs args)
{
// ๋ฐ์ดํฐ ์กฐํ ์ค๋
์ท ์ ์
DataSnapshot snapshot = args.Snapshot;
// ํด๋น ์ฝ๋๋ฅผ ๊ฐ์ง ํ์์ด ์์
if (snapshot.ChildrenCount == 0)
{
selectDataField.text = "No students have that code.";
}
else
{
// code๊น์ง ์ ๊ทผ (0000, 0001 ...)
foreach (var data in snapshot.Children)
{
IDictionary CodeData = (IDictionary)data.Value;
selectDataField.text = $"code : {CodeData["code"]}\n";
// info์ ์ ๊ทผ
DataSnapshot info = data.Child("info");
IDictionary infoData = (IDictionary)info.Value;
selectDataField.text += $"name: {infoData["name"]}, age: {infoData["age"]}, gender: {infoData["gender"]}\n";
// grade์ ์ ๊ทผ
DataSnapshot grade = data.Child("grade");
IDictionary gradeData = (IDictionary)grade.Value;
selectDataField.text += $"K: {gradeData["Korean"]}, E : {gradeData["English"]}, M : {gradeData["Math"]}, S : {gradeData["Science"]}\n";
}
}
userNameQuery.ValueChanged -= OnDataLoaded;
}
}
- 0001 ์ ๋ ฅ > Select ๋ฒํผ ํด๋ฆญ
- ํด๋น ์ฝ๋๋ฅผ ๊ฐ์ง ํ์์ ์ฐพ์ ์ถ๋ ฅํ๋ ๊ฒ์ ํ์ธ
- ํด๋น ์ฝ๋๊ฐ ์์ผ๋ฉด ์์ ๊ฐ์ด ์ถ๋ ฅ
'Unity > Database' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
01. Azure CosmosDB Settings (0) | 2021.08.21 |
---|---|
00. Azure ์ฒดํ๊ณ์ ํ์ฑํ (0) | 2021.08.20 |
05. Firebase Realtime Database - Delete (0) | 2021.08.14 |
04. Firebase Realtime Database - Select (0) | 2021.08.13 |
03. Firebase Realtime Database - Load (0) | 2021.08.12 |