【发布时间】:2020-11-16 17:23:49
【问题描述】:
我正在创建一个带有评分系统的游戏,但目前玩家的名字不包括在他们的高分中。这个想法是玩家可以在游戏结束屏幕出现并获得新的高分后添加他们的名字。
最初系统只会保存一个高分,但现在保存了 5 个高分,这些高分将在其他地方的表格中调用,我也希望在该表格中显示玩家姓名。
我对 C# 不太熟悉,所以不知道如何合并这种用户输入,所以我很高兴听到可用的选项。
这是我的评分系统代码:
public class ScoreManager : MonoBehaviour
{
public Text scoreText;
public Text hiScoreText;
public Text gameOverScoreText;
public float scoreCount;
public float hiScoreCount;
public float distance;
public float pointsPerSecond;
public bool scoreIncreasing;
//create array for last 5 high scores
float[] highScores = new float[5];
//create list for scores
public List<float> ScoreHistory;
// Start is called before the first frame update
void Start()
{
//if there is a high score, the game will register it, otherwise it will be 0 - this is for one score
/*if (PlayerPrefs.HasKey("HighScore"))
{
hiScoreCount = PlayerPrefs.GetFloat("HighScore");
}*/
//obtain last 5 scores
ScoreHistory = LoadScoresFromPlayerPrefs(5);
// Set the current high score to be the highest score in the player history - if there is no high score saved, high score will be 0
if (ScoreHistory.Count > 0)
{
hiScoreCount = ScoreHistory[0];
}
else
{
hiScoreCount = 0;
}
}
// Update is called once per frame
void Update()
{
if (scoreIncreasing)
{
//adding points every frame/update => but shows decimals
scoreCount += pointsPerSecond * Time.deltaTime;
//scoreCount += Vector3.Distance(transform.position, camera.position);
}
if(scoreCount > hiScoreCount)
{
hiScoreCount = scoreCount;
//saves value called high score and the hiScoreCount value - not used if saving more than one score
//PlayerPrefs.SetFloat("HighScore", hiScoreCount);
}
//adding text to score
//Mathf.Round rounds scoreCount and hiScoreCount to nearest whole
scoreText.text = "Score: " + Mathf.Round(scoreCount);
hiScoreText.text = "High Score: " + Mathf.Round(hiScoreCount);
gameOverScoreText.text = "Your Score: " + Mathf.Round(scoreCount);
}
//function which needs a number value to take in - can be used more than once
public void AddScore(int pointsToAdd)
{
//adding points to score
scoreCount += pointsToAdd;
}
// Save the current score to the list of scores, and then write them to the player prefs
public void SaveCurrentScore()
{
ScoreHistory.Add(scoreCount);
//put scores in order
ScoreHistory.Sort();
for (int i = 0; i< ScoreHistory.Count; i++)
{
//key is the name of the value being stored
var key = "High Score " + i;
//value is what is being stored (i.e. the high scores) => ScoreHistory is being used as each score is being saved
var value = ScoreHistory[i];
//high scores are being saved using PlayerPrefs
PlayerPrefs.SetFloat(key, value);
}
}
// Loads the scores from the player prefs and returns them in a list of floats
private List<float> LoadScoresFromPlayerPrefs(int maximumNumberOfScoresToLoad)
{
//no visibility modifiers - this is a local variable
List<float> LoadScores = new List<float>();
//loop will run once per score
for (int i = 0; i < maximumNumberOfScoresToLoad; i++)
{
//key is the name of the value being stored
var key = "High Scores " + i;
//will return value of the key if there is one, otherwise will return default value of 0.0
//PlayerPrefs.GetFloat(key);
var score = PlayerPrefs.GetFloat(key);
LoadScores.Add(score);
}
return LoadScores;
}
}
【问题讨论】:
-
您需要一个播放器类,这样您就可以将每个播放器分开。播放器类需要名称和分数列表。然后你需要一个 List
并将所有玩家添加到列表中。
标签: c# unity3d user-input