firebase数据库unity可以用我从数据库中读取的值设置playerpref float
我需要从数据库中读取硬币值并用playerPrefs保存,问题是,甚至是读取作品,第一个print()打印出从数据库中读取的正确值,该值仍然不会用plarterprefs保存,并且没有显示错误。我尝试以许多方式进行此操作,从技术上讲,这些操作与我在此处发布的代码相同,但无效,但它们似乎都陷入了PlayerPrefs保存部分。
更新/编辑:我测试了,似乎读取与int和float之外的每种类型的变量一起工作,字符串无问题。这对我来说仍然很奇怪。
解决: 因此,这可能是一种愚蠢的方法,但这是我唯一可以做到的。我没有将数字保存为数据库中的INT,而是将其保存为字符串,当我从数据库中读取字符串时,我将其分解为一个数字,然后用playerPrefs保存该数字。这是一个非常奇怪的错误,我解决了它的怪异方法
await databaseReference.Child(FirebaseAuth.DefaultInstance.CurrentUser.UserId.ToString()).Child("rockets").GetValueAsync().ContinueWith((load =>
{
if (load.IsCanceled)
{
return;
}
else if (load.IsFaulted)
{
return;
}
else
{
DataSnapshot coins = load.Result;
print(coins.Value);
PlayerPrefs.SetFloat("coins", (int)coins.Value);
PlayerPrefs.Save();
print("COINS SET: " + coins.Value);
}
}));
I need to read the coins value from the database and save it with playerprefs, the problem is, even tho the reading works and the first print() prints out the correct value read from the database, the value still wont save with playerprefs and it shows no errors. I tried doing this in many ways which technically do the same thing as the code I posted here but none works and they all seem to get stuck on the playerprefs save part.
UPDATE/EDIT: I tested, and it seems that the reading works with every type of variable except int and float, string works without a problem. This is still really weird to me.
SOLVED:
So this is probably a stupid way to do this, but its the only way i could. Instead of saving the number as an int in the database, i save it as a string and when i read the string from the database i parse it into a number, and then save that number with the playerprefs. This is a really weird bug and weird way i solved it lol
await databaseReference.Child(FirebaseAuth.DefaultInstance.CurrentUser.UserId.ToString()).Child("rockets").GetValueAsync().ContinueWith((load =>
{
if (load.IsCanceled)
{
return;
}
else if (load.IsFaulted)
{
return;
}
else
{
DataSnapshot coins = load.Result;
print(coins.Value);
PlayerPrefs.SetFloat("coins", (int)coins.Value);
PlayerPrefs.Save();
print("COINS SET: " + coins.Value);
}
}));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在plarterPrefs中设置值后,您还必须致电playerprefs.save()来委托这些值。
After you set values in playerprefs, you have to also call PlayerPrefs.Save() to committ those values.