Unity 5 - Playerprefs 在 unity 中保存但在 android 上不保存?
Unity 5 - Playerprefs saving in unity but not on android?
我将此附加到我的英雄对象上,但它已被销毁。这可能是问题的一部分吗?我想要做的就是保存高分,然后将其显示在我的开始场景中。这是代码:
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 高分。
我几乎遇到了同样的问题,但我找到了解决方案。我知道这个问题太老了,但也许我可以帮助别人。
我必须在游戏结束前保存我的 PlayerPrefs。
有一个简单的解决方案,只需调用方法 OnApplicationPause() 并放入您的 PlayerPrefs.
我认为你应该将 int 获取到 PlayerPrefs.GetInt 上的字符串
这是我的代码:
public Text Score;
public Text HighScore;
public bool increasing;
static int score;
static int highScore;
public Movement player;
void Start ()
{
score = 0;
highScore = PlayerPrefs.GetInt ("HighScore");
}
void Update ()
{
Score.text = " " + score + "m";
HighScore.text = " " + highScore + "m";
if (player.ground == true && increasing == true)
{
score = score + 1;
}
if (score > PlayerPrefs.GetInt("HighScore"))
{
highScore = score;
PlayerPrefs.SetInt ("HighScore", highScore);
PlayerPrefs.Save();
}
}
我将此附加到我的英雄对象上,但它已被销毁。这可能是问题的一部分吗?我想要做的就是保存高分,然后将其显示在我的开始场景中。这是代码:
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 高分。
我几乎遇到了同样的问题,但我找到了解决方案。我知道这个问题太老了,但也许我可以帮助别人。
我必须在游戏结束前保存我的 PlayerPrefs。 有一个简单的解决方案,只需调用方法 OnApplicationPause() 并放入您的 PlayerPrefs.
我认为你应该将 int 获取到 PlayerPrefs.GetInt 上的字符串 这是我的代码:
public Text Score;
public Text HighScore;
public bool increasing;
static int score;
static int highScore;
public Movement player;
void Start ()
{
score = 0;
highScore = PlayerPrefs.GetInt ("HighScore");
}
void Update ()
{
Score.text = " " + score + "m";
HighScore.text = " " + highScore + "m";
if (player.ground == true && increasing == true)
{
score = score + 1;
}
if (score > PlayerPrefs.GetInt("HighScore"))
{
highScore = score;
PlayerPrefs.SetInt ("HighScore", highScore);
PlayerPrefs.Save();
}
}