반응형
파이어베이스 SDK 다운로드
압축 푼 뒤
dotnet4 / FirebaseDatabase.unitypackage 설치
- 코드 작성
using Firebase;
using Firebase.Database;
using Firebase.Extensions;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class User
{
public string username;
public string email;
}
public class FirebaseDataBase : MonoBehaviour
{
DatabaseReference reference;
void Start()
{
reference = FirebaseDatabase.DefaultInstance.RootReference;
}
private void SaveNewUser(string userId, string name, string email) //데이터 저장하기
{
User user = new User();
user.username = name;
user.email = email;
string json = JsonUtility.ToJson(user);
reference.Child("Users").Child(userId).SetRawJsonValueAsync(json);
Debug.Log("Save Data Success!");
}
private void LoadAllUsers() //데이터 불러오기
{
FirebaseDatabase.DefaultInstance.GetReference("Users").GetValueAsync().ContinueWithOnMainThread(task =>
{
if (task.IsFaulted)
{
Debug.Log("Load Data Faulted");
}
else if (task.IsCompleted)
{
Debug.Log("Load Data Success!");
DataSnapshot snapshot = task.Result;
}
});
}
}
참고할만한 글
반응형