问题
I am stuck with the following problem: I have a fishing game. So far, if I play the game, I can start fishing, catch 2,3,4 more fish and everything is fine.
However, if I want to pause the game (Pressing ESC or a button that I placed in the scene) my score resets. Additionally, I don't know how to save my score, even if I pause the game or go into shop button.
Start function
void Start()
{
PlayerPrefs.GetInt("Pesti");
NrPesti.text = PlayerPrefs.GetInt("Pesti").ToString();
PlayerPrefs.GetInt("Viermisori");
NrViermisori.text = PlayerPrefs.GetInt("Viermisori").ToString();
PlayerPrefs.GetInt("Score");
Score.text = PlayerPrefs.GetInt("Score").ToString();
PlaySound(0);
}
Update function:
void Update()
{
Debug.Log(NrViermisori.text);
Debug.Log(NrPesti.text);
Debug.Log(Score.text);
if (NrPesti.name =="Pesti")
{
NrPesti.text = "Lovers: " + NrPesti.text;
}
PlayerPrefs.SetString("NrPesti", NrPesti.text);
if(NrViermisori.name == "Viermisori")
{
NrViermisori.text = "Beasts: " + NrViermisori.text;
}
PlayerPrefs.SetString("Viermisori", NrViermisori.text);
if (Input.GetKeyDown(KeyCode.Escape)) Application.Quit();
}
回答1:
In your update method try adding something to your code where you are detecting Escape Press.
if (Input.GetKeyDown(KeyCode.Escape)){
PlayerPrefs.setInt("Score", Int32.Parse(Score.text));
//Verify property is saved
Int scoreTest = PlayerPrefs.getInt("Score");
Debug.Log(scoreTest);
Application.Quit();
}
Good Luck
回答2:
I think this is not the right way to save/load your score by doing operations on the Text.text directly.
If you want to save and load your score, make a separate variable score to store your score, then use your score in your UI Elements.
For example, this explains how you save scores at runtime:
public class Score: MonoBehaviour {
public int score = 0;
// Use this for initialization
void Start () {
// get the score when this gameObject initialized
score = PlayerPrefs.GetInt("Player Score");
}
// Update is called once per frame
void Update () {
// this is how you set your score to PlayerPrefs
if (Input.GetKeyDown(KeyCode.Escape)) {
PlayerPrefs.SetInt("Player Score", score);
}
}
// this is an public function to be used
// outside of the class to update score
public void UpdateScore(int amount) {
score += amount;
}
}
And use your score in your UI class:
public class DisplayScore : MonoBehaviour {
public Text scoreText;
public Score playerScore;
// Use this for initialization
void Start () {
// get your Score component here or just drag it in inspector
}
// Update is called once per frame
void Update () {
// this updates the score at every frame
scoreText.text = playerScore.score.ToString();
}
}
don't forget using UnityEngine.UI, saving/loading score in this way will be much easier.
Usually, you don't need to call PlayerPrefs.Save() since the data will be automatically written to disk when the game quits (it calls automatically in OnApplicationQuit()), but you may want to call it at certain point (i.e checkpoints) in case of program crushes or other unexpected situations.
回答3:
You should have a kind of a GameManager class where you store everything you need to remember in appropriate attributes. When you start the game you create an instance of the GM and reference to this object in the game logic. Things like the Score should be stored and processed within the GM. That way you can make sure to 'save' your score while the game is alive.
来源:https://stackoverflow.com/questions/42252950/how-to-save-score-in-unity