Unity 5 - Playerprefs在团结中保存但在android上没有?

时间:2015-07-01 22:05:52

标签: c# android unity3d save scoring

我把这个附加到我的英雄对象被摧毁。这可能是问题的一部分吗?我想要做的就是保存高分,然后将其显示在我的开始场景中。这是代码:

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);
}

3 个答案:

答案 0 :(得分:1)

根据我对Unity的了解,您不需要调用PlayerPrefs.Save(),我也注意到您在本地存储了高分,并且从未定义过,因此您将分数与空值进行比较。您的新代码如下所示:

public int score; 
void AddPoint1()
{
     if(score > PlayerPrefs.GetInt("highscore"))
     {
           PlayerPrefs.SetInt("highscore", score);
     }
}

即使你最初将高分设置为某个东西,也就是说1.代码可以工作但是一旦你关闭应用程序并重新运行它,高分将被重置回一个,然后即使玩家得分为2,它无论它是什么,都会覆盖PlayerPrefs的高分。

答案 1 :(得分:0)

我几乎有同样的问题,但我找到了解决方案。我知道这个问题太老了,但也许我可以帮助别人。

我必须在比赛结束前保存我的PlayerPrefs。 有简单的解决方案,只需调用方法OnApplicationPause()并放入您的PlayerPrefs。

答案 2 :(得分:0)

我认为你应该在你的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();
    }
}