- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的一个项目中,我最初开始使用 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/
我有三个 double 组列表。我想将数组1复制到数组2并将其放入第三个数组中。 // ArrayList 1 [0.0, 1.0, 0.0, 1.0] // ArrayList 2 // Rando
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在编写一个带有变量 long balance 的银行程序将美分存入账户。当用户输入金额时,我有一种方法可以将美元转换为美分: public static long convertFromUsd (
如何 DRY(不要重复自己)RSpec 中的 double?例如: let(:s1) {instance_double(Class1, :att
我是 Java 新手,我想知道为什么当你有双 10/4 时却得到 2? double 是否总是必须有小数才能得到正确的答案?谢谢。 public class Super { public st
我正在用 Python 为 Fortran 库编写前端。 Python 模块应该可以在 32 位和 64 位机器上运行;适用于 Windows、Linux 和 Mac。 我想了解一些数据类型的字节宽度
我有两个ajax json。一个用于聊天用户列表列,另一个用于每个用户的消息历史记录。当单击 $('.member_list ul li') 元素时...第二个 ajax 将 li 的值作为 json
从 GHCi 中的 .hs 文件加载 a = 2+2.0 并执行 :t a 显示 a::Double。 另一方面,在 GHCi 中执行 let b = 2+2.0 和 :t b 会显示 b::Frac
我是一名优秀的程序员,十分优秀!