c# - 如何将以前花费的时间和最近花费在同一级别上的时间相加?
问题描述
如何添加玩家上一次玩的时间,例如第 1 关,他下一次玩同一关卡的时间将添加到上一次,这将显示在结果板上。
为了获得我们使用的秒数
timereasy1 += Time.deltaTime;
然后将其保存到
PlayerPrefs.SetFloat("timer1",timereasy1);
并将其添加到
totaltime=totaltime+timer1;
但它不会加起来......它仍然会显示玩家最近花费的时间。
解决方案
您需要像现在一样存储值,但您可以像这样简化它。
private float _timePlayed;
private void Awake()
{
//When the Game/Level starts get the previous amount of time played if there is a PlayerPref for it otherwise it defaults to 0 as an unset float
if (PlayerPrefs.HasKey("timer1"))
{
_timePlayed = PlayerPrefs.GetFloat("timer1");
}
}
private void Update()
{
//Update the Time played during Game Play
_timePlayed += Time.deltaTime;
}
private void GameOver()
{
//At the end of the Game/Level update the PlayerPref with the new value
PlayerPrefs.SetFloat("timer1", _timePlayed);
}
推荐阅读
- python - 在 Python 中异步播放声音
- javascript - 在单独的对象中使用 reduce 进行对象解构
- python - 如何使用 python 将文本打印到 .txt 文件中
- javascript - 我正在学习如何将 javascript 转换为 jquery,但无法通过全局变量正确转换
- spring-data-jdbc - StatementBuilder 是供外部使用的吗?
- mysql - 如何对数据进行非规范化
- gcc - riscv-gcc 无法构建 [GCC_NO_EXECUTABLES]
- javascript - React js 对象作为 React 子对象无效
- javascript - 有没有办法更新云功能中的集合组
- javascript - HighMaps:使用各种数据集更改工具提示上的状态名称