|
|
(同じ利用者による、間の11版が非表示) |
行1: |
行1: |
− | ==Realtimedatabaseのインストール==
| + | [[Unity/Firebase/Realtimedatabase/インストール]] |
− | FirebaseDatabase.unitypackageをAssets/Importからインストールする
| + | |
| | | |
− | https://firebase.google.com/download/unity?hl=ja
| + | [[Unity/Firebase/Realtimedatabase/基本]] |
| | | |
− | ==料金課金周り==
| + | [[Unity/Firebase/Realtimedatabase/更新]] |
− | [[Gcp/Firebase/RealtimeDatabase]] [ショートカット] | + | |
| | | |
− | ==ルール==
| + | [[Unity/Firebase/Realtimedatabase/json]] |
− | 以下のようなルールになってると、更新できないので、test時は修正後のように
| + | |
− | 修正前
| + | |
− | <pre>
| + | |
− | {
| + | |
− | "rules": {
| + | |
− | ".read": false,
| + | |
− | ".write": false
| + | |
− | }
| + | |
− | }
| + | |
− | </pre>
| + | |
− | 修正後
| + | |
− | <pre>
| + | |
− | {
| + | |
− | "rules": {
| + | |
− | ".read": true,
| + | |
− | ".write": true
| + | |
− | }
| + | |
− | }
| + | |
− | </pre>
| + | |
| | | |
− | ==サンプル==
| + | [[Unity/Firebase/Realtimedatabase/リスト]] |
− | SampleScene.cs
| + | |
− | <pre>
| + | |
− | using System.Collections;
| + | |
− | using System.Collections.Generic;
| + | |
− | using UnityEngine;
| + | |
| | | |
− | using Firebase;
| + | [[Unity/Firebase/Realtimedatabase/削除]] |
− | using Firebase.Database;
| + | |
− | using Firebase.Unity.Editor;
| + | |
| | | |
− | public class SampleScene : MonoBehaviour
| + | [[Unity/Firebase/Realtimedatabase/切断]] |
− | {
| + | |
− | void Start()
| + | |
− | {
| + | |
− | // Get the root reference location of the database.
| + | |
− | DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference;
| + | |
| | | |
− | // 書き込み
| + | [[Unity/Firebase/Realtimedatabase/chat]] |
− | FirebaseDatabase.DefaultInstance.GetReference("users")
| + | |
− | .Child("name")
| + | |
− | .SetValueAsync("taro");
| + | |
| | | |
− | // 読み込み
| + | [[Unity/Firebase/Realtimedatabase/timestamp]] |
− | FirebaseDatabase.DefaultInstance.GetReference("users")
| + | |
− | .Child("name")
| + | |
− | .GetValueAsync().ContinueWithOnMainThread(task =>
| + | |
− | {
| + | |
− | if (task.IsFaulted)
| + | |
− | {
| + | |
− | Debug.LogError("not found users");
| + | |
− | }
| + | |
− | else if (task.IsCompleted)
| + | |
− | {
| + | |
− | DataSnapshot snapShot = task.Result;
| + | |
− | Debug.Log(snapShot.Value);
| + | |
− | }
| + | |
− | });
| + | |
− | | + | |
− | }
| + | |
− | }
| + | |
− | </pre>
| + | |
− | 参考:https://firebase.google.com/docs/database/unity/retrieve-data?hl=ja
| + | |
− | | + | |
− | 参考:http://siguma-sig.hatenablog.com/entry/2018/09/30/213822
| + | |
− | | + | |
− | ==json更新==
| + | |
− | FirebaseDatabase.DefaultInstance.GetReference("users")
| + | |
− | .Child("name")
| + | |
− | .SetRawJsonValueAsync("{'name': 'taro','age' : 25}");
| + | |
− | | + | |
− | ==変更があったら呼び出し==
| + | |
− | <pre>
| + | |
− | public class SampleScene : MonoBehaviour
| + | |
− | {
| + | |
− | void Start()
| + | |
− | {
| + | |
− | // Get the root reference location of the database.
| + | |
− | DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference;
| + | |
− | | + | |
− | // 変更があったら呼び出し
| + | |
− | FirebaseDatabase.DefaultInstance.GetReference("user")
| + | |
− | .Child("name").ValueChanged += HandleNameValueChanged;
| + | |
− | | + | |
− | // 書き込み
| + | |
− | FirebaseDatabase.DefaultInstance.GetReference("user")
| + | |
− | .Child("name")
| + | |
− | .SetValueAsync("taro");
| + | |
− | }
| + | |
− | private void HandleNameValueChanged(object sender, ValueChangedEventArgs args)
| + | |
− | {
| + | |
− | if (args.DatabaseError != null)
| + | |
− | {
| + | |
− | Debug.LogError(args.DatabaseError.Message);
| + | |
− | return;
| + | |
− | }
| + | |
− | Debug.Log("HandleNameValueChanged=" + args.Snapshot.Value);
| + | |
− | }
| + | |
− | }
| + | |
− | </pre>
| + | |
− | | + | |
− | ==テキストフィールドから送信して、別テキストで受信するまで==
| + | |
− | #UIからInputFieldを選択し、/Canvas/InputFieldを作成
| + | |
− | #UIからTextを選択し、/Canvas/CommonTextを作成
| + | |
− | #以下csを用意する。
| + | |
− | | + | |
− | 入力したテキストが、別端末の同アプリにリアルタイムに表示されるれば成功。
| + | |
− | | + | |
− | サンプル
| + | |
− | <pre>
| + | |
− | using System.Collections;
| + | |
− | using System.Collections.Generic;
| + | |
− | using UnityEngine;
| + | |
− | using UnityEngine.UI;
| + | |
− | | + | |
− | using Firebase;
| + | |
− | using Firebase.Database;
| + | |
− | using Firebase.Unity.Editor;
| + | |
− | | + | |
− | public class SampleScene : MonoBehaviour
| + | |
− | {
| + | |
− | DatabaseReference reference;
| + | |
− | void Start()
| + | |
− | {
| + | |
− | reference = FirebaseDatabase.DefaultInstance.RootReference;
| + | |
− | FirebaseDatabase.DefaultInstance.GetReference("user")
| + | |
− | .Child("name").ValueChanged += HandleNameValueChanged;
| + | |
− | | + | |
− | InputField input = GameObject.Find("/Canvas/InputField").GetComponent<InputField>();
| + | |
− | input.onValueChanged.AddListener(OnValueChanged);
| + | |
− | }
| + | |
− | // 受信
| + | |
− | void HandleNameValueChanged(object sender, ValueChangedEventArgs args)
| + | |
− | {
| + | |
− | if (args.DatabaseError != null)
| + | |
− | {
| + | |
− | Debug.LogError(args.DatabaseError.Message);
| + | |
− | return;
| + | |
− | }
| + | |
− | Debug.Log("HandleNameValueChanged=" + args.Snapshot.Value.ToString());
| + | |
− | GameObject.Find("/Canvas/CommonText").GetComponent<Text>().text = args.Snapshot.Value.ToString();
| + | |
− | }
| + | |
− | // 送信
| + | |
− | void SetName(string name)
| + | |
− | {
| + | |
− | Debug.Log("SetName=" + name);
| + | |
− | FirebaseDatabase.DefaultInstance.GetReference("user")
| + | |
− | .Child("name")
| + | |
− | .SetValueAsync(name);
| + | |
− | }
| + | |
− | // テキスト変更イベント
| + | |
− | void OnValueChanged(string value)
| + | |
− | {
| + | |
− | Debug.Log("OnValueChanged=" + value);
| + | |
− | SetName(value);
| + | |
− | }
| + | |
− | }
| + | |
− | </pre>
| + | |
− | | + | |
− | ==階層==
| + | |
− | /で区切ったり、
| + | |
− | .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>
| + | |
− | | + | |
− | ==参考==
| + | |
− | https://firebase.google.com/docs/database/unity/save-data?hl=ja
| + | |