我想单独保存每个级别的高分,并将它们写在不同的场景中我还希望保留每个级别的所有高分,这部分有效。
当我完成第一级时系统似乎工作正常,我得到5482,总计5482 在第二级它也工作,我得到2299,总说7781,这是正确的 当我获得第3级但是我得到0并且总数保持在7781时,关卡完成的方式和编辑器中的所有内容都没有正确设置。 第4级也给0,总数保持不变等等。
更大脚本的一部分,但这里是将得分写入文本的代码。
public int scorelevel;
public Text findText;
public bool highscore;
public bool totalhighscore;
void Start()
{
findText = GetComponent<Text>();
if(highscore){
if (PlayerPrefs.GetInt ("highscorelevel" + scorelevel) > 0) {
findText.text = "Highest score: " + PlayerPrefs.GetInt ("highscorelevel" + scorelevel);
} if(GameObject.Find ("LockedLevel" + (scorelevel)).active == true) {
findText.text = null;
}
}
if (totalhighscore) {
for (int j = 1; j < LockLevel.levels; j++){
PlayerPrefs.SetInt ("totalHighscore", PlayerPrefs.GetInt ("totalHighscore") + PlayerPrefs.GetInt ("highscorelevel"+j));
}
findText.text = "Total Score: " + PlayerPrefs.GetInt ("totalHighscore");
}
更大的脚本,但这里是保存分数的部分:
public class UnlockLevel : MonoBehaviour {
protected string currentLevel;
protected int levelIndex;
protected int levelIndex2;
void Start () {
//save the current level name
currentLevel = Application.loadedLevelName;
}
//check if the player hits the Finish
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "Finish") {
Highscores ();
}
}
public void Highscores (){
for(int h = 1; h < LockLevel.levels; h++){
if(currentLevel == "Level"+h.ToString()){
//worldIndex = (i+1);
levelIndex2 = (h);
if(ScoreManager.score > PlayerPrefs.GetInt("highscorelevel"+levelIndex2.ToString())){
PlayerPrefs.SetInt("highscorelevel"+levelIndex2.ToString(), (int) ScoreManager.score);
}
}
}
}
}