๐ท Realtime Database ์ธํ
๐ถ Firebase

- Realtime Database

- ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ง๋ค๊ธฐ

- ๋ค์

- ํ ์คํธ ๋ชจ๋์์ ์์
- ์ฌ์ฉ ์ค์

- '+' ๊ธฐํธ

- ๋ฐ์ดํฐ ์๋ฌด๊ฑฐ๋ ๊ธฐ์ > ์ถ๊ฐ

- ๋ฐ์ดํฐ๊ฐ ์ฝ์ ๋ ๊ฒ์ ํ์ธ

- X ๋ฅผ ๋๋ฌ ๋ฐ์ดํฐ ์ญ์

- ํด๋ฆญํ์ฌ ์ฃผ์ ๋ณต์ฌ
๐ถ Unity
{
"project_info": {
"project_number": "878253878040",
"project_id": "fir-demo-3c2bb",
"storage_bucket": "fir-demo-3c2bb.appspot.com",
"firebase_url": "https://fir-demo-3c2bb-default-rtdb.firebaseio.com/"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:878253878040:android:e0df8407b7dffdcb280c6d",
"android_client_info": {
"package_name": "com.Ojui.FirebaseDemo"
}
},
"oauth_client": [
{
"client_id": "878253878040-a5l0kr9c79f4o5k5h13ilh4p80r2ffl8.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyBNAyCbmRLn5PD1_3ZiHJ44ka5CjqT-NrM"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "878253878040-a5l0kr9c79f4o5k5h13ilh4p80r2ffl8.apps.googleusercontent.com",
"client_type": 3
}
]
}
}
}
],
"configuration_version": "1"
}
- google-services.json ํ์ผ
- 6๋ฒ์งธ ์ค์ ,"firebase_url": "https://fir-demo-3c2bb-default-rtdb.firebaseio.com/" ๋ฃ๊ธฐ
๐ท Insert Data

- ๋น๊ฒ์์ค๋ธ์ ํธ : FirebaseManager


- ์คํฌ๋ฆฝํธ ์์ฑ : FirebaseManager
- FirebaseManager ๊ฒ์ ์ค๋ธ์ ํธ์ ์คํฌ๋ฆฝํธ ์ถ๊ฐ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
// 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;
// ํ์ด์ด๋ฒ ์ด์ค ๋ ํผ๋ฐ์ค๋ฅผ ์ ์ญ ์ ์ธ
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 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);
}
}
- uri์ ๋ณต์ฌํ ์ฃผ์ ๋ถ์ฌ๋ฃ๊ธฐ
- RootReference : ์ต์๋จ์ ์๋ ๋ ธ๋๋ฅผ ๋ปํจ

- Solid Color๋ก ๋ณ๊ฒฝ

- Canvas ์์ฑ
- Screen Spacd - Camera๋ก ์ค์

- Input Field 2๊ฐ ์์ฑ : InputField - userName, InputField - Gold

- Button ์์ฑ : Button - Insert
- FirebaseManager ์ค๋ธ์ ํธ ์ฐ๊ฒฐ > InsertData() ํจ์ ์ฐ๊ฒฐ

- ์ฐ๊ฒฐ


- ํ ์คํธ

- ๋งจ ์ฒ์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ ํ ๋๋ง UserData ๋ ธ๋๊ฐ ์์ฑ
- ๊ทธ ์ดํ์๋ ์ด๋ฆ์ด ๋๊ฐ์ ๋ ธ๋(UserData)๊ฐ ์๋ค๋ฉด, ํด๋น ๋ ธ๋ ์๋์ ๋ฐ์ดํฐ๊ฐ ์ฝ์ ๋จ
'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 |
03. Firebase Realtime Database - Load (0) | 2021.08.12 |
01. Firebase Setting (0) | 2021.08.10 |