首页 > 解决方案 > 如何将以前花费的时间和最近花费在同一级别上的时间相加?

问题描述

如何添加玩家上一次玩的时间,例如第 1 关,他下一次玩同一关卡的时间将添加到上一次,这将显示在结果板上。

为了获得我们使用的秒数

timereasy1 += Time.deltaTime;

然后将其保存到

PlayerPrefs.SetFloat("timer1",timereasy1);

并将其添加到

totaltime=totaltime+timer1;

但它不会加起来......它仍然会显示玩家最近花费的时间。

标签: c#unity3d

解决方案


您需要像现在一样存储值,但您可以像这样简化它。

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

推荐阅读