「Unity/Firebase/Realtimedatabase/json」の版間の差分
提供: 初心者エンジニアの簡易メモ
| 行22: | 行22: | ||
</pre> | </pre> | ||
| − | + | 参考: | |
https://firebase.google.com/docs/database/unity/save-data?hl=ja | https://firebase.google.com/docs/database/unity/save-data?hl=ja | ||
| + | |||
| + | ==jsonで受け取る== | ||
| + | <pre> | ||
| + | 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"} | ||
| + | } | ||
| + | }); | ||
2021年9月29日 (水) 00:58時点における版
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"}
}
});
