问题
Hey so im trying out unitys Playerprefs method and some how it wont save coins and when i close and exit the game it wont save it...
public Text CoinsText;
public int Coins;
public int clicks;
void Start()
{
PlayerPrefs.GetInt("Coins", Coins);
}
void Update()
{
CoinsText.text = "Memes: " + Coins;
if (Input.GetMouseButtonDown(0))
{
PlayerPrefs.SetInt("Coins", Coins);
Coins += clicks;
}
}
}
回答1:
You're never assigning to your Coins.
Try this in your Start():
Coins = PlayerPrefs.GetInt("Coins");
Note that the second value is only in case there wouldn't be a value saved.
public static int GetInt(string key, int defaultValue = 0);
Also, don't forget to Save all values before closing the program with:
PlayerPrefs.Save();
回答2:
First of all you should get int like this Coins = PlayerPrefs.GetInt("Coins");
Then after you SetInt you have to save it with PlayerPrefs.Save();
来源:https://stackoverflow.com/questions/40742286/playerprefs-not-working