微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Unity显示最高10分

如何解决Unity显示最高10分

我正在努力使自己像记分牌一样,但仅针对单个玩家。我想将我最好的10个分数保存到板上,并显示在MainMenu中。到目前为止,我知道如何保存和仅显示一个结果。代码如下:

[Serializefield] private int score;
 
 void Update()
 {
  if (score > PlayerPrefs.GetInt("highestscore"))
         {
             PlayerPrefs.SetInt("highestscore",score);
         }
 }

我在MainMenu场景中显示如下

void Start()
 {
     highscore.text = PlayerPrefs.GetInt("highestscore").ToString();
 }

是否可以帮助我保存最近的10个最佳成绩并显示出来?我找不到有关如何执行此操作的任何教程。我想我应该将一个Ints数组并遍历该数组,然后将其保存到PlayerPrefs,但是我不知道该怎么做。希望您能对我有所帮助,并先谢谢您!

解决方法

您绝对可以保存更多密钥, PlayerPrefs 接受参数,一个是键,第二个是值 您可以像设置

PlayerPrefs.SetInt("highScore_1",value);
PlayerPrefs.SetInt("highScore_2",value);
PlayerPrefs.SetInt("highScore_3",value);
PlayerPrefs.SetInt("highScore_4",value);
PlayerPrefs.SetInt("highScore_5",value);

因此,当某人的得分超过先前最高得分之一时,您要做的就是

int newScore = 50;
for(int i=1; i <= 5; i++){
    if(newScore > PlayerPrefs.GetInt("highScore_"+i)){
        for(int x = 5; x > i; x--){
            int value = PlayerPrefs.GetInt("highScore_"+(x-1));
            PlayerPrefs.setInt("highScore_"+x,value);
        }
        break;
    }
}

让我知道它是否有效。

,

您可以使用ArrayPrefs2将其存储为数组(http://wiki.unity3d.com/index.php/ArrayPrefs2),然后使用以下命令进行更新:

static void UpdateHighScores(int newScore)
{ 
  var highScores = PlayerPrefsX.GetIntArray("highScores");
  if (newScore > highScores[0]) {
    highScores[0] = newScore;
    Array.Sort(highScores);
    PlayerPrefsX.SetIntArray("highScores",highScores);
  }
}

在其他位置初始化PlayerPrefs键"highScores"后,

PlayerPrefsX.SetIntArray("highScores",new Array[10]);

请注意,这会产生一个数组,数组的最高分数最低,而真正的最高分数排在最后。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。