-
[Firebase]파이어베이스 Database 2. 데이터 저장개발/유니티 2018. 3. 24. 19:15
메소드 사용 예제
RunTransaction()은 공유자원을 관리하는 것과 유사한데, 복잡하므로 나중에...
using System.Collections;using System.Collections.Generic;using Firebase;using Firebase.Database;using Firebase.Unity.Editor;using UnityEngine;public class DatabaseManager : MonoBehaviour {private static DatabaseManager Instance;public DatabaseReference reference;void Start() {// Set this before calling into DatabaseFirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://프로젝트 이름.firebaseio.com/");// Get the root reference location fo Databasereference = FirebaseDatabase.DefaultInstance.RootReference;User user1 = new User("htw", "naver");User user2 = new User("ksj", "daum");string json = JsonUtility.ToJson(user1);string json2 = JsonUtility.ToJson(user2);// Using Jsonreference.Child("users").Child("user1").SetRawJsonValueAsync(json);// Using Pathreference.Child("users").Child("user2").Child("name").SetValueAsync(user2.name);reference.Child("users").Child("user2").Child("email").SetValueAsync(user2.email);// Using Pushstring key = reference.Child("users").Push().Key;reference.Child("users").Child(key).Child("email").SetValueAsync("This is Push()");// Using UpdateDictionary<string, object> update = user2.ToDictionary();reference.Child("users").Child("user3").SetRawJsonValueAsync(json);reference.Child("users").Child("user3").UpdateChildrenAsync(update);}}class User {public string name;public string email;public User(string name, string email) {this.name = name;this.email = email;}public Dictionary<string, object> ToDictionary() {Dictionary<string, object> result = new Dictionary<string, object>();result["name"] = name;result["email"] = email;return result;}}결과
'개발 > 유니티' 카테고리의 다른 글
[GooglePlayGames]구글 로그인 API 사용하기 (0) 2018.03.28 [Firebase]파이어베이스 Auth 1. 시작하기 (0) 2018.03.28 [Firebase]파이어베이스 Database 1. 시작하기 (0) 2018.03.24