繁体   English   中英

保存计时器HighScore Unity C#

[英]Save Timer HighScore Unity C#

我目前正在开发一款具有计时器功能的游戏,而最好的时间是可以最快完成课程的时间。 例如,如果我获得40秒的第一次跑步将被保存为高分,但是如果我获得30秒的第二次跑步将被保存为新的高分。 我的应用程序当前做的恰恰相反,如果我有更多的时间来获得新的高分。

注意:是的,我试图将符号切换为小于'(t <PlayerPrefs.GetFloat(“ HighScore”,0))',但是问题是没有时间可以打败“ 0”。

源代码C#:

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEngine.UI; 
using UnityEngine.Internal;

public class TImer : MonoBehaviour {

 public Text timerText;
 public Text highscore;
 private float startTime;
 private bool finished = false;



 void Start () {

     startTime = Time.time;
     highscore.text = PlayerPrefs.GetFloat ("HighScore", 0).ToString();
 }




 void Update ()
 {

     float t = Time.time - startTime;
     string minutes = ((int)t / 60).ToString ();
     string seconds = (t % 60).ToString ("f2");
     timerText.text = minutes + ":" + seconds;
     if (t > PlayerPrefs.GetFloat ("HighScore", 0)) {
         PlayerPrefs.SetFloat ("HighScore", t);
         highscore.text = t.ToString ();
     } 
}

有一些问题。

第一种,如果您想保存最低的价格,则需要执行Peter Bons的建议并使用float.MaxValue进行比较

第二,您要在更新过程中更新HighScore,它只应在玩家结束游戏时更新。 现在发生的是,您开始游戏,在更新中HighScore设置为0或接近0。 如果您将最短的时间存储为HighScore,则该时间将永远无法工作,因为Player在游戏开始时会达到“最高”(或最佳)得分。

考虑到这一点,我对您的代码进行了一些更改。 确保当玩家到达关卡/游戏结束时调用GameFinished()。

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEngine.UI; 
using UnityEngine.Internal;

public class TImer : MonoBehaviour {

 public Text timerText;
 public Text highscore;
 private float startTime;
 private bool finished = false;

 void GameFinished()
 {
      float t = Time.time - startTime;
      if (t < PlayerPrefs.GetFloat ("HighScore", float.MaxValue)) {
             PlayerPrefs.SetFloat ("HighScore", t);
             highscore.text = t.ToString ();
             PlayerPrefs.Save();
       } 
 }

 void Start () {

     startTime = Time.time;
     highscore.text = PlayerPrefs.GetFloat ("HighScore", 0).ToString();
 }


 void Update ()
 {

     float t = Time.time - startTime;
     string minutes = ((int)t / 60).ToString ();
     string seconds = (t % 60).ToString ("f2");
     timerText.text = minutes + ":" + seconds;

     if(/* Check if player finished the game */)
           GameFinished();
}

因此,如果我没记错的话,高分代表最短的时间。 在这种情况下,您应该检查播放时间是否小于存储的最高分数,而不是大于现在进行的比较。

是的,您确实写过您尝试了相反的尝试,但是这样一来,您永远都无法击败高分0(这是玩家第一次玩游戏时的默认值)。 解决方法很简单:将默认的高分设置为大数:

if (t < PlayerPrefs.GetFloat("HighScore", float.MaxValue)) { //Checks HighScore or compare with max value
     PlayerPrefs.SetFloat("HighScore", t);
     highscore.text = t.ToString();
     if (t < PlayerPrefs.GetFloat ("HighScore", float.MaxValue)) {
         PlayerPrefs.SetFloat ("HighScore", t);
         highscore.text = t.ToString ();
         PlayerPrefs.Save();//Saves PlayerPrefs to save the HighScore that was changed
     } 
}

现在,第一次玩游戏总会带来新的高分。 随后的比赛将根据该高分得分。

if (t > PlayerPrefs.GetFloat ("HighScore", 0))更改为if (t < PlayerPrefs.GetFloat("HighScore", 0) || PlayerPrefs.GetFloat("HighScore", 0) == 0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM