我将此附加到我的英雄对象上,但它已被销毁。这可能是问题的一部分吗?我想要做的就是保存高分,然后将其显示在我的开始场景中。这是代码:
public int highscore;
public int score;
void AddPoint1()
{
if (score > highscore) {
highscore = score;
PlayerPrefs.SetInt ("highscore", score);
PlayerPrefs.Save ();
}
}
这是附加到开始场景(不同场景)中的空对象的第二个脚本
void OnGUI()
{
GUI.Label (new Rect (Screen.width / 2, Screen.height/2, 100, 100), "Highscore: " + PlayerPrefs.GetInt("highscore"), customGuistyle);
}
最佳答案
根据我对 Unity 的了解,您不需要调用 PlayerPrefs.Save(),我还注意到您将高分存储在本地并且从未定义,因此您将分数与空值进行比较。您的新代码如下所示:
public int score;
void AddPoint1()
{
if(score > PlayerPrefs.GetInt("highscore"))
{
PlayerPrefs.SetInt("highscore", score);
}
}
此外,即使您最初将高分设置为某个值,比如 1。代码可以运行,但是一旦您关闭应用程序并重新运行它,高分就会重置回 1,然后即使玩家得分为 2,它将覆盖 PlayerPrefs 高分,无论它是什么。
关于c# - Unity 5 - Playerprefs 保存在统一但不是在 android 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31172496/