C# Unity 5-PlayerRefs在Unity中保存,但在android上不保存?
我把这个附在我的英雄物品上,它被摧毁了。这可能是问题的一部分吗?我想做的就是保存最高分,然后在我的开始场景中显示它。代码如下:C# Unity 5-PlayerRefs在Unity中保存,但在android上不保存?,c#,android,unity3d,save,scoring,C#,Android,Unity3d,Save,Scoring,我把这个附在我的英雄物品上,它被摧毁了。这可能是问题的一部分吗?我想做的就是保存最高分,然后在我的开始场景中显示它。代码如下: public int highscore; public int score; void AddPoint1() { if (score > highscore) { highscore = score; PlayerPrefs.SetInt ("highscore", score); Pla
public int highscore;
public int score;
void AddPoint1()
{
if (score > highscore) {
highscore = score;
PlayerPrefs.SetInt ("highscore", score);
PlayerPrefs.Save ();
}
}
下面是附加到开始场景中的空对象的第二个脚本(不同场景)
根据我对Unity的了解,您不需要调用PlayerPrefs.Save(),而且我注意到您在本地存储了highscore,并且它从未定义过,因此您将分数与空值进行比较。您的新代码如下所示:
public int score;
void AddPoint1()
{
if(score > PlayerPrefs.GetInt("highscore"))
{
PlayerPrefs.SetInt("highscore", score);
}
}
而且,即使您最初将highscore设置为某个值,也可以说1。代码可以工作,但一旦关闭应用程序并重新运行,highscore将重置为1,然后即使玩家得了2分,它也会覆盖PlayerPrefs highscore,不管它是什么。我几乎遇到了同样的问题,但我找到了解决方案。我知道这个问题太老了,但也许我可以帮助别人 我必须在比赛结束前保存我的球员裁判。
这里有一个简单的解决方案,只需调用applicationpause()上的方法并将其放入PlayerPrefs中。我认为您应该在PlayerPrefs.GetInt上获取字符串的int 这是我的密码:
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();
}
}
我试过了,但仍然不起作用,我甚至试过设置一个变量=来获取int,但仍然没有骰子,我现在要研究序列化来保存。对不起,回信太晚了。嗯,有一件事要注意。您在一个方法中调用它,而该方法本身不会被调用。我会将此代码添加到更新方法中。如果你想让我查看itI,请发布其余代码。我尝试将其放入更新方法中,结果成功了!哇,我真不敢相信我错过了这么简单的事情。我想我需要对方法有更多的了解。谢谢你,伙计:)
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();
}
}