我在Unity中创建了一个打砖块游戏,其中一个名为Game的场景根据从json文件接收的数据加载每个级别。
IE:
即使您按下“再次播放”按钮,我也希望高分信息保留在播放器偏好中。
但是我对此工作方式有些困惑。这是我的分数代码:
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
public Text scoreText;
public Text highScoreText;
private int score;
private int highScore;
void Start()
{
score = 0;
GetHighScore();
}
void Update()
{
UpdateScore();
SetHighScore();
GetHighScore();
}
// TO UPDATE HIGH SCORE
void SetHighScore()
{
if (score > highScore)
{
PlayerPrefs.SetInt("HighScore", score);
}
}
void GetHighScore()
{
highScore = PlayerPrefs.GetInt("HighScore");
highScoreText.text = "High score: " + highScore;
}
// TO UPDATE HIGH SCORE
// TO UPDATE SCORE
public void AddPoints(int points)
{
score = score + points;
UpdateScore();
}
void UpdateScore()
{
scoreText.text = "score: " + score;
}
// TO UPDATE SCORE
}
到目前为止,分数更新良好,但高分数没有任何反应。任何帮助表示赞赏!
答案 0 :(得分:1)
此方法无法实现
void GetHighScore()
{
PlayerPrefs.GetInt("HighScore");
}
应该是
void GetHighScore()
{
highScore = PlayerPrefs.GetInt("HighScore");
}
我看不出在Update
的每一帧中调用它的意义。在Start
此外,您可能想更新highScore
中的SetHighScore
if (score > highScore)
{
highScore = score;
PlayerPrefs.SetInt("HighScore", highScore);
highScoreText.text = "high score: " + highScore;
}