簡體   English   中英

在 Unity C# 代碼中存儲高分不起作用

[英]Storing Highscore in Unity C# code not working

所以,我一直在嘗試存儲我的球員的高分,但無論我嘗試多少,回報都是 0。

這是分數設置

  highScore = playerScoref();
           if(PlayerPrefs.GetFloat("Score") < highScore) {
                PlayerPrefs.SetFloat("Score", highScore);
            }
            PlayerPrefs.Save();
       }    

這就是我取回代碼的地方

       Debug.Log("GAME OVER");
       highScore = PlayerPrefs.GetFloat("Score");
       Debug.Log(highScore);

我認為你需要做這樣的事情

public class Test : MonoBehaviour {

    private int ingameScore;
    private int highScore;
    private bool gameOver;
    private bool callOnce;

    void Start() {
        if (PlayerPrefs.HasKey("HighScore")) {
            highScore = PlayerPrefs.GetInt("HighScore");
        }
        else {
            //creating the key for highscore
            PlayerPrefs.SetInt("HighScore", 0);
            highScore = 0;
        }
    }

    void Update() {

        //if player lost the game set gameover boolean to true
        //and use callOnce boolean to call these block only once and only for one frame
        //to avoid extra cpu usage
        if (gameOver && !callOnce) {

            //and check if player collected higher score then highscore is and assign it 
            //to playerpref's highscore key 
            if(ingameScore > highScore) {
                PlayerPrefs.SetInt("HighScore", ingameScore);
            }

            callOnce = true;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM