如何在相同级别上添加之前花费的时间和最近花费的时间?

时间:2019-07-10 11:25:50

标签: c# unity3d

我该如何添加玩家上一次玩的时间,例如1级,而下一次他玩相同级别的时间将添加到上一次,这将显示在结果板上。

为了获得以秒为单位的时间,我们使用了

timereasy1 += Time.deltaTime;

,然后将其保存到

PlayerPrefs.SetFloat("timer1",timereasy1);

并将其添加到

totaltime=totaltime+timer1;

但是它不会累加...它仍然会显示播放器最近的使用时间。

1 个答案:

答案 0 :(得分:1)

您需要按原样存储值,但是可以像这样简化它。

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);
}