|
|
(同じ利用者による、間の7版が非表示) |
行4: |
行4: |
| | | |
| [[Unity/Firebase/Realtimedatabase/更新]] | | [[Unity/Firebase/Realtimedatabase/更新]] |
| + | |
| + | [[Unity/Firebase/Realtimedatabase/json]] |
| | | |
| [[Unity/Firebase/Realtimedatabase/リスト]] | | [[Unity/Firebase/Realtimedatabase/リスト]] |
| + | |
| + | [[Unity/Firebase/Realtimedatabase/削除]] |
| | | |
| [[Unity/Firebase/Realtimedatabase/切断]] | | [[Unity/Firebase/Realtimedatabase/切断]] |
| | | |
| + | [[Unity/Firebase/Realtimedatabase/chat]] |
| | | |
− | ==階層==
| + | [[Unity/Firebase/Realtimedatabase/timestamp]] |
− | /で区切ったり、 | + | |
− | .Child("na/me")
| + | |
− | Childでつなげたり。
| + | |
− | .Child("na").Child("me")
| + | |
− | | + | |
− | ==jsonをそのまま送信==
| + | |
− | <pre>
| + | |
− | User user = new User("taro", "taro@example");
| + | |
− | string json = JsonUtility.ToJson(user);
| + | |
− | string userId = "111";
| + | |
− | FirebaseDatabase.DefaultInstance.GetReference("user").Child(userId)
| + | |
− | .SetRawJsonValueAsync(json);
| + | |
− | </pre>
| + | |
− | User.cs
| + | |
− | <pre>
| + | |
− | public class User
| + | |
− | {
| + | |
− | public string username;
| + | |
− | public string email;
| + | |
− | public User(string username, string email)
| + | |
− | {
| + | |
− | this.username = username;
| + | |
− | this.email = email;
| + | |
− | }
| + | |
− | }
| + | |
− | </pre>
| + | |
− | | + | |
− | ==keyを生成==
| + | |
− | userに存在しないキーを生成
| + | |
− | string key = FirebaseDatabase.DefaultInstance.GetReference("user").Push().Key;
| + | |
− | | + | |
− | ==リスト==
| + | |
− | 追加、更新、削除を検知
| + | |
− | <pre>
| + | |
− | using UnityEngine;
| + | |
− | using UnityEngine.UI;
| + | |
− | using Firebase.Database;
| + | |
− | using System.Collections.Generic;
| + | |
− | using System;
| + | |
− | | + | |
− | public class SampleScene : MonoBehaviour
| + | |
− | {
| + | |
− | void Start()
| + | |
− | {
| + | |
− | DatabaseReference userReference = FirebaseDatabase.DefaultInstance.GetReference("user");
| + | |
− | userReference.ChildAdded += HandleChildAdded;
| + | |
− | userReference.ChildChanged += HandleChildChanged;
| + | |
− | userReference.ChildRemoved += HandleChildRemoved;
| + | |
− | | + | |
− | InputField input = GameObject.Find("/Canvas/InputField").GetComponent<InputField>();
| + | |
− | GameObject.Find("AddButton").GetComponent<Button>().onClick.AddListener(delegate {
| + | |
− | AddUser(input.text);
| + | |
− | });
| + | |
− | }
| + | |
− | // 受信 追加された時
| + | |
− | void HandleChildAdded(object sender, ChildChangedEventArgs args)
| + | |
− | {
| + | |
− | if (args.DatabaseError != null)
| + | |
− | {
| + | |
− | Debug.LogError(args.DatabaseError.Message);
| + | |
− | return;
| + | |
− | }
| + | |
− | string json = args.Snapshot.GetRawJsonValue();
| + | |
− | Debug.Log("HandleChildAdded=" + json); // {"email":"ttt@example","username":"ttt"}
| + | |
− | | + | |
− | User user = JsonUtility.FromJson<User>(json);
| + | |
− | Debug.Log("HandleChildAdded username=" + user.username);
| + | |
− | }
| + | |
− | // 受信 更新された時
| + | |
− | void HandleChildChanged(object sender, ChildChangedEventArgs args)
| + | |
− | {
| + | |
− | if (args.DatabaseError != null)
| + | |
− | {
| + | |
− | Debug.LogError(args.DatabaseError.Message);
| + | |
− | return;
| + | |
− | }
| + | |
− | string json = args.Snapshot.GetRawJsonValue();
| + | |
− | Debug.Log("HandleChildChanged=" + json); // {"email":"ttt@example","username":"ttt"}
| + | |
− | | + | |
− | User user = JsonUtility.FromJson<User>(json);
| + | |
− | Debug.Log("HandleChildChanged username=" + user.username);
| + | |
− | }
| + | |
− | // 受信 削除された時
| + | |
− | void HandleChildRemoved(object sender, ChildChangedEventArgs args)
| + | |
− | {
| + | |
− | if (args.DatabaseError != null)
| + | |
− | {
| + | |
− | Debug.LogError(args.DatabaseError.Message);
| + | |
− | return;
| + | |
− | }
| + | |
− | string json = args.Snapshot.GetRawJsonValue();
| + | |
− | Debug.Log("HandleChildRemoved=" + json); // {"email":"ttt@example","username":"ttt"}
| + | |
− | | + | |
− | User user = JsonUtility.FromJson<User>(json);
| + | |
− | Debug.Log("HandleChildRemoved username=" + user.username);
| + | |
− | }
| + | |
− | void AddUser(string name)
| + | |
− | {
| + | |
− | User user = new User(name, name + "@example");
| + | |
− | string json = JsonUtility.ToJson(user); // {"email":"ttt@example","username":"ttt"}
| + | |
− | // userに存在しないキーを生成
| + | |
− | // FirebaseDatabase.DefaultInstance.GetReference("user").Push().Key;
| + | |
− | #if Unity_EDITOR
| + | |
− | string userId = "pc";
| + | |
− | #elif UNITY_ANDROID
| + | |
− | string userId = "android";
| + | |
− | #elif UNITY_IPHONE
| + | |
− | string userId = "iphone";
| + | |
− | #endif
| + | |
− | FirebaseDatabase.DefaultInstance.GetReference("user").Child(userId)
| + | |
− | .SetRawJsonValueAsync(json);
| + | |
− | }
| + | |
− | }
| + | |
− | </pre>
| + | |
− | | + | |
− | ==切断==
| + | |
− | DatabaseReference.GoOffline();
| + | |
− | とか?切断できてるか不明・・・。
| + | |
− | ==参考==
| + | |
− | https://firebase.google.com/docs/database/unity/save-data?hl=ja
| + | |