[英]Unity C# Player Prefs Highscore not working
我在 Android 上的 PlayerPrefs 统一(C#)出现错误
好像没有保存
脚本 1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Scores : MonoBehaviour
{
public static int highscore;
public static int points;
public void Start()
{
}
public void Update()
{
highscore = PlayerPrefs.GetInt("highscore");
PlayerPrefs.SetInt("highscore", highscore);
PlayerPrefs.Save();
}
}
脚本 2(使用它的部分)
void Update()
{
highscore = PlayerPrefs.GetInt("Highscore");
if (points > highscore)
{
highscore = points;
highscoretext.text = "Highscore: " + highscore;
highscoretext2.text = "Highscore: " + highscore;
}
当我得到一个高分(例如 12)并且我死了我 go 回到主屏幕然后回到游戏时,我只能弄清楚它,并且在重新启动应用程序时,高分是相同的游戏
当您现在设置高分时,您将获得当前的高分并将其存储在highscore
变量中。 然后,您继续使用highscore
变量覆盖高分,有效地将高分设置为自身。 您真正想要做的是检查当前分数是否超过高分,如果是,则将高分设置为当前分数。
highscore = PlayerPrefs.GetInt("highscore");
if(score > highscore)
{
PlayerPrefs.SetInt("highscore", score);
PlayerPrefs.Save();
}
您的代码不起作用的另一个原因是您在脚本 1 中用小 h 拼写"highscore"
,但在脚本 2 中用大 h 拼写。
此外,每次更新都调用PlayerPrefs.GetInt
效率非常低。 您应该将它们变成仅在需要获取或设置高分时才调用的函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.