반응형
Reference
https://firebase.google.com/docs/firestore/quickstart?hl=ko
using UnityEngine;
using Firebase.Firestore;
using Firebase.Extensions;
using System.Collections.Generic;
using System;
public class FirebaseManager : MonoBehaviour
{
FirebaseFirestore db;
void Start()
{
db = FirebaseFirestore.DefaultInstance;
}
public void WriteData()
{
Debug.Log("SSSS Firebase Add Data.");
DocumentReference docRef = db.Collection("users").Document("alovelace");
Dictionary<string, object> user = new Dictionary<string, object>
{
{ "First", "Ada" },
{ "Last", "Lovelace" },
{ "Born", 1815 },
};
docRef.SetAsync(user).ContinueWithOnMainThread(task => {
Debug.Log("Added data to the alovelace document in the users collection.");
});
}
public void WriteData2()
{
DocumentReference docRef = db.Collection("users").Document("aturing");
Dictionary<string, object> user = new Dictionary<string, object>
{
{ "First", "Alan" },
{ "Middle", "Mathison" },
{ "Last", "Turing" },
{ "Born", 1912 }
};
docRef.SetAsync(user).ContinueWithOnMainThread(task => {
Debug.Log("Added data to the aturing document in the users collection.");
});
}
public void ReadData()
{
Debug.Log("SSSS Firebase Read Data.");
CollectionReference usersRef = db.Collection("users");
usersRef.GetSnapshotAsync().ContinueWithOnMainThread(task =>
{
QuerySnapshot snapshot = task.Result;
foreach (DocumentSnapshot document in snapshot.Documents)
{
Debug.Log(String.Format("User: {0}", document.Id));
Dictionary<string, object> documentDictionary = document.ToDictionary();
Debug.Log(String.Format("First: {0}", documentDictionary["First"]));
if (documentDictionary.ContainsKey("Middle"))
{
Debug.Log(String.Format("Middle: {0}", documentDictionary["Middle"]));
}
Debug.Log(String.Format("Last: {0}", documentDictionary["Last"]));
Debug.Log(String.Format("Born: {0}", documentDictionary["Born"]));
}
Debug.Log("Read all data from the users collection.");
});
}
}
Rule
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
https://firebase.google.com/docs/rules/basics?hl=ko
반응형
'Unity > Unity 리서치' 카테고리의 다른 글
Unity IAP 연동 (0) | 2021.05.17 |
---|---|
Unity Admob 연동하기 (0) | 2021.05.16 |
Unity Firebase 연동 (0) | 2021.05.15 |
Unity 구글로그인 - (2) (0) | 2021.05.08 |
Unity 구글 플레이와 연동하기 - (1) (0) | 2021.05.06 |