我正在创建一个带有评分系统的游戏,但目前玩家的名字不包括在他们的高分中。这个想法是,一旦游戏出现在屏幕上,玩家就可以添加自己的名字,前提是他们获得了新的高分。
最初,系统只会保存一个高分,但现在保存了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;
}
}
如果您想让用户输入名称,您必须使用
- Unity的UI系统(例如输入字段(或
- 自己编写一些代码(例如,完成后观察Input.inputString(,只有当你有类似旧街机的东西,只允许3个字符时,我才会建议你这样做
在这两种情况下,您都需要能够通过可检测的提交(例如回车键或专用UI按钮(来检测用户输入的结束。之后,您可以将其与相应的点一起存储。
要将点和名称绑定在一起,只需添加一个包含两个的结构
public struct ScoreData {
public float Score { get; set; }
public string Name { get; set; }
}
并将其用于ScoreHistory
public List<ScoreData> ScoreHistory;
private string playerName; // store the name input by player here
...
public void SaveCurrentScore()
{
ScoreHistory.Add(new ScoreData { Score = scoreCount, Name = playerName });
//put scores in order
ScoreHistory = ScoreHistory.OrderBy(s => s.Score).ToList();
for (int i = 0; i < ScoreHistory.Count; i++)
{
//key is the name of the value being stored
var scoreKey = "High Score " + i;
var nameKey = "High Score Playername " + 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(scoreKey, value.Score);
PlayerPrefs.SetString(nameKey, value.Name);
}
}