gpt4 book ai didi

c# - 使用 PlayerPrefs 保存双倍?

转载 作者:行者123 更新时间:2023-11-30 14:22:29 25 4
gpt4 key购买 nike

在我的一个项目中,我最初开始使用 float ,但过了一会儿我意识到它的精度并不是我想要的。我有一个自动化系统,每秒增加 X 个硬币的值(value),并且考虑到这个 float 很大(超过 200 万),它就不会再增加这么小的值(value)了。

所以我的下一个决定是使用 double 而不是 float。在将所有内容从 float 更改为 double 之后,我意识到我将值存储在 PlayerPrefs 中,我想我不能在其中存储 double?所以现在我有点困惑。

如何从 PlayerPrefs 中提取现有的 float 信息并将该 float 值放入 double 中,然后将该 double 值保存在 PlayerPrefs 中?我有点困惑,所以我正在寻求帮助。

这是我的部分代码:

// old
// public float totalP;

// new
public double totalP;

void OnEnable()
{

if (!PlayerPrefs.HasKey ("game_totalP"))
{
PlayerPrefs.SetFloat ("game_totalP", totalP);
}
else
{
totalP = PlayerPrefs.GetFloat ("game_totalP");
}
}

如有任何帮助,我们将不胜感激!

最佳答案

您可以将 double 编码为两个 int 并存储和检索它们。

存储:

var storeBytes = BitConverter.GetBytes(totalP);
var storeIntLow = BitConverter.ToInt32(storeBytes, 0);
var storeIntHigh = BitConverter.ToInt32(storeBytes, 4);
PlayerPrefs.SetInt("game_totalP_low", storeIntLow);
PlayerPrefs.SetInt("game_totalP_high", storeIntHigh);

检索:

var retrieveBytes = new byte[8];
Array.Copy(BitConverter.GetBytes(storeIntLow), retrieveBytes, 4);
Array.Copy(BitConverter.GetBytes(storeIntHigh), 0, retrieveBytes, 4, 4);
totalP = BitConverter.ToDouble(retrieveBytes, 0);

关于c# - 使用 PlayerPrefs 保存双倍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49594049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com