【问题标题】:How to add previous time spent and recent time spent on the same level?如何将以前花费的时间和最近花费在同一级别上的时间相加?
【发布时间】:2019-07-11 22:26:42
【问题描述】:

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

为了获得我们使用的秒数

timereasy1 += Time.deltaTime;

然后保存到

PlayerPrefs.SetFloat("timer1",timereasy1);

并将其添加到

totaltime=totaltime+timer1;

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

【问题讨论】:

  • 你应该添加一点代码

标签: c# unity3d


【解决方案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);
}

【讨论】:

    猜你喜欢
    • 2017-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多