「Unity/Firebase/Realtimedatabase/json」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→jsonで受け取る) |
(→jsonの値を取得例) |
||
| (同じ利用者による、間の3版が非表示) | |||
| 行61: | 行61: | ||
string json = snapShot.GetRawJsonValue(); | string json = snapShot.GetRawJsonValue(); | ||
Dictionary<string, User> dic = JsonConvert.DeserializeObject<Dictionary<string, User>>(json); | Dictionary<string, User> dic = JsonConvert.DeserializeObject<Dictionary<string, User>>(json); | ||
| − | foreach (KeyValuePair<string, | + | foreach (KeyValuePair<string, User> x in dic) |
{ | { | ||
Debug.Log(x.Key + ": " + x.Value.text); | Debug.Log(x.Key + ": " + x.Value.text); | ||
| 行84: | 行84: | ||
} | } | ||
</pre> | </pre> | ||
| + | |||
| + | ==jsonの値を取得例== | ||
| + | 取得時にcallbackで、イベントを返してあげると良いが、別スレッドになってるので、メインスレッドでcallbackする。 | ||
| + | <pre> | ||
| + | List<User> users; | ||
| + | public void ExecGetUsers() | ||
| + | { | ||
| + | var context = SynchronizationContext.Current; // メインスレッドでcontextを設置 | ||
| + | FirebaseDatabase.DefaultInstance.GetReference("users") | ||
| + | .GetValueAsync().ContinueWith(task => | ||
| + | { | ||
| + | if (task.IsFaulted) | ||
| + | { | ||
| + | Debug.LogError("not found users"); | ||
| + | } | ||
| + | else if (task.IsCompleted) | ||
| + | { | ||
| + | DataSnapshot snapShot = task.Result; | ||
| + | string json = snapShot.GetRawJsonValue(); | ||
| + | |||
| + | Dictionary<string, User> dic = JsonConvert.DeserializeObject<Dictionary<string, User>>(json); | ||
| + | users = new List<User>(); | ||
| + | |||
| + | foreach (KeyValuePair<string, User> x in dic) | ||
| + | { | ||
| + | Debug.Log(x.Key + ": " + x.Value.text); | ||
| + | users.Add(x.Value); | ||
| + | } | ||
| + | // メインスレッドへ戻す | ||
| + | context.Post(d => | ||
| + | { | ||
| + | if (callbacks != null) | ||
| + | { | ||
| + | callbacks(ResponseType.Users, null); | ||
| + | } | ||
| + | }, null); | ||
| + | } | ||
| + | }); | ||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | 参考: | ||
| + | [[unity/Csharp/コールバック]] [ショートカット] | ||
| + | |||
| + | https://light11.hatenadiary.com/entry/2021/01/07/203448 | ||
2021年9月30日 (木) 01:21時点における最新版
jsonをそのまま送信
User user = new User("taro", "taro@example");
string json = JsonUtility.ToJson(user);
string userId = "111";
FirebaseDatabase.DefaultInstance.GetReference("user").Child(userId)
.SetRawJsonValueAsync(json);
User.cs
public class User
{
public string username;
public string email;
public User(string username, string email)
{
this.username = username;
this.email = email;
}
}
参考: https://firebase.google.com/docs/database/unity/save-data?hl=ja
jsonで受け取る
string userId = "111";
FirebaseDatabase.DefaultInstance.GetReference("user").Child(userId)
.GetValueAsync().ContinueWith(task =>
{
if (task.IsFaulted)
{
Debug.LogError("not found users");
}
else if (task.IsCompleted)
{
DataSnapshot snapShot = task.Result;
Debug.Log("json=" + snapShot.GetRawJsonValue()); // {"username":"taro", "email", "taro@example"}
}
});
参考:https://ntw-games.net/wp/develop/363/
keyを複数含んだjsonを取得
JsonUtilityだと、Dictionaryのjsonが、取り込めないので、Newtonsoft.Json側を、使うと良い。
using Newtonsoft.Json;
FirebaseDatabase.DefaultInstance.GetReference("users")
.GetValueAsync().ContinueWith(task =>
{
if (task.IsFaulted)
{
Debug.LogError("not found users");
}
else if (task.IsCompleted)
{
DataSnapshot snapShot = task.Result;
string json = snapShot.GetRawJsonValue();
Dictionary<string, User> dic = JsonConvert.DeserializeObject<Dictionary<string, User>>(json);
foreach (KeyValuePair<string, User> x in dic)
{
Debug.Log(x.Key + ": " + x.Value.text);
}
}
});
User.cs
public class User
{
public string username;
public string email;
public User()
{
}
public User(string username, string email)
{
this.username = username;
this.email = email;
}
}
jsonの値を取得例
取得時にcallbackで、イベントを返してあげると良いが、別スレッドになってるので、メインスレッドでcallbackする。
List<User> users;
public void ExecGetUsers()
{
var context = SynchronizationContext.Current; // メインスレッドでcontextを設置
FirebaseDatabase.DefaultInstance.GetReference("users")
.GetValueAsync().ContinueWith(task =>
{
if (task.IsFaulted)
{
Debug.LogError("not found users");
}
else if (task.IsCompleted)
{
DataSnapshot snapShot = task.Result;
string json = snapShot.GetRawJsonValue();
Dictionary<string, User> dic = JsonConvert.DeserializeObject<Dictionary<string, User>>(json);
users = new List<User>();
foreach (KeyValuePair<string, User> x in dic)
{
Debug.Log(x.Key + ": " + x.Value.text);
users.Add(x.Value);
}
// メインスレッドへ戻す
context.Post(d =>
{
if (callbacks != null)
{
callbacks(ResponseType.Users, null);
}
}, null);
}
});
}
参考: unity/Csharp/コールバック [ショートカット]
