在我听说Firebase之后,我决定在我的小型Unity游戏上尝试它是个好主意。
我阅读了文档,遵循“入门”配置教程,并更改了身份验证规则,以便每个人都可以插入数据。
void Start()
{
// Set up the Editor before calling into the realtime database.
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://magicast-bd2fc.firebaseio.com/");
Firebase.Auth.FirebaseAuth.DefaultInstance.StateChanged += DefaultInstance_StateChanged;
// Get the root reference location of the database.
DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference;
var u = new User();
u.email = "sdjkdsdksadjsad";
u.fb_id = "sakdsajdhasd";
u.username = "jadhsjahjsda";
reference.SetValueAsync(u);
}
此代码确实执行,但是当我转到Firebase控制台时,我看不到数据库选项卡上的数据。我无法弄清楚我做错了什么,代码完全没有错误!我做错了什么?
这是我的用户类。它基本上是一个测试类,没什么大不了的东西:
public class User
{
public string username;
public string email;
public string fb_id;
public User()
{
}
public User(string username, string email, string fb_id)
{
this.username = username;
this.email = email;
this.fb_id = fb_id;
}
}
答案 0 :(得分:0)
您未经过身份验证,默认的Firebase安全规则要求const byte image[]={
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
ect ect ect
。
请参阅docs了解如何使用Unity进行身份验证。
答案 1 :(得分:0)
您无法在Firebase数据库的任意对象上调用.set()
,它们必须属于以下类别之一:
string
long
double
bool
Dictionary<string, Object>
List<Object>
而不是
reference.SetValueAsync(u);
您需要致电
string json = JsonUtility.ToJson(u);
reference.SetRawJsonValueAsync(json);
您实际上并没有登录任何用户。要登录匿名用户,请先转到Firebase控制台 - &gt;验证并启用“匿名”&#39;开关。然后使用以下代码进行身份验证:
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
auth.SignInAnonymouslyAsync().ContinueWith(task => {
if (task.IsCanceled) {
Debug.LogError("SignInAnonymouslyAsync was canceled.");
return;
}
if (task.IsFaulted) {
Debug.LogError("SignInAnonymouslyAsync encountered an error: " + task.Exception);
return;
}
Firebase.Auth.FirebaseUser newUser = task.Result;
Debug.LogFormat("User signed in successfully: {0} ({1})",
newUser.DisplayName, newUser.UserId);
});