[英]How to add previous time spent and recent time spent on the same level?
我该如何添加玩家上一次玩的时间(例如第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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.