Unity/Database

02. Firebase Realtime Database - Insert

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