0
我到目前爲止所做的是在遊戲中設置每秒增加的分數,獲得在遊戲場景中顯示的分數,然後設置高分等於分數if得分高於高分。這是到目前爲止我的代碼:在Unity2d中保存和加載高分
bool gameOver;
public Text scoreText;
public Text highScoreText;
int score;
int highScore;
// Use this for initialization
void Start() {
score = 0;
highScore = 0;
InvokeRepeating ("scoreUpdate", 1.0f, 1.0f);
gameOver = false;
}
// Update is called once per frame
void Update() {
scoreText.text = "★" + score;
highScoreText.text = "★" + highScore;
}
public void gameOverActivated() {
gameOver = true;
if (score > highScore) {
highScore = score;
}
PlayerPrefs.SetInt("score", score);
PlayerPrefs.Save();
PlayerPrefs.SetInt("highScore", highScore);
PlayerPrefs.Save();
}
void scoreUpdate() {
if (!gameOver) {
score += 1;
}} }
「GAME OVER」等於真當此代碼發生:
void OnCollisionEnter2D (Collision2D col) {
if (col.gameObject.tag == "enemyPlanet") {
ui.gameOverActivated();
Destroy (gameObject);
Application.LoadLevel ("gameOverScene2");
}
}
我想在這一點上(當物體發生碰撞和遊戲結束是真),以保存分數,然後加載場景上的遊戲。如何在比賽結束時保存比分,然後將比賽中的比分加載到場上以及保存的比分?