C# Unity 2019.4.11f1:PlayerPrefs未保存在Android设备上,但在Unity中正确保存

C# Unity 2019.4.11f1:PlayerPrefs未保存在Android设备上,但在Unity中正确保存,c#,unity3d,C#,Unity3d,我使用PlayerPrefs在游戏中保存5个高分。当使用Unity编辑器时,PlayerPref工作正常,但当我在Android上构建并运行游戏时,分数不会保存 --更新-- 当硬编码(手动设置值)高分时,这些值会显示在Android上。然而,分数不能与球员输球时的分数相比较 void Update() { if (CurrentHealth == 0) { Dead(); } } void Dead() { pauseMenuUI.S

我使用PlayerPrefs在游戏中保存5个高分。当使用Unity编辑器时,PlayerPref工作正常,但当我在Android上构建并运行游戏时,分数不会保存

--更新--
当硬编码(手动设置值)高分时,这些值会显示在Android上。然而,分数不能与球员输球时的分数相比较

    void Update() {
    if (CurrentHealth == 0)
    {
        Dead();
    }
}

void Dead() {
    pauseMenuUI.SetActive(true);
    Time.timeScale = 0f;

    //Check to see if a high score is achieved
    scoreManager.checkScore(score.returnScore());
    
}
检查高分并保存到PlayerPrefs的代码

    public void checkScore(int score)
{

    if(score > PlayerPrefs.GetInt("HighScore1" ))
    {
        PlayerPrefs.SetInt("HighScore1", score);
        PlayerPrefs.Save();

    }

    else if (score > PlayerPrefs.GetInt("HighScore2"))
    {
        PlayerPrefs.SetInt("HighScore2", score);
        PlayerPrefs.Save();

    }

    else if (score > PlayerPrefs.GetInt("HighScore3"))
    {
        PlayerPrefs.SetInt("HighScore3", score);
        PlayerPrefs.Save();

    }

    else if (score > PlayerPrefs.GetInt("HighScore4"))
    {
        PlayerPrefs.SetInt("HighScore4", score);
        PlayerPrefs.Save();

    }

    else if (score > PlayerPrefs.GetInt("HighScore5"))
    {
        PlayerPrefs.SetInt("HighScore5", score);
        PlayerPrefs.Save();

    }

}
    private void Awake()
{
    instance = this;
    
            highScore1 = PlayerPrefs.GetInt("HighScore1");
            highScore2 = PlayerPrefs.GetInt("HighScore2");
            highScore3 = PlayerPrefs.GetInt("HighScore3");
            highScore4 = PlayerPrefs.GetInt("HighScore4");
            highScore5 = PlayerPrefs.GetInt("HighScore5");
    
    createArray();
}

void Start()
{
    
}

//Creates Initial Array
public void createArray()
{
    int[] HighScore = {highScore1, highScore2, highScore3, highScore4, highScore5};
    this.HighScore = HighScore;
}

//Allows for the array to be returned
public int[] getArray()
{
    createArray();
    
    return this.HighScore;

}
代码从PlayerPrefs检索高分

    public void checkScore(int score)
{

    if(score > PlayerPrefs.GetInt("HighScore1" ))
    {
        PlayerPrefs.SetInt("HighScore1", score);
        PlayerPrefs.Save();

    }

    else if (score > PlayerPrefs.GetInt("HighScore2"))
    {
        PlayerPrefs.SetInt("HighScore2", score);
        PlayerPrefs.Save();

    }

    else if (score > PlayerPrefs.GetInt("HighScore3"))
    {
        PlayerPrefs.SetInt("HighScore3", score);
        PlayerPrefs.Save();

    }

    else if (score > PlayerPrefs.GetInt("HighScore4"))
    {
        PlayerPrefs.SetInt("HighScore4", score);
        PlayerPrefs.Save();

    }

    else if (score > PlayerPrefs.GetInt("HighScore5"))
    {
        PlayerPrefs.SetInt("HighScore5", score);
        PlayerPrefs.Save();

    }

}
    private void Awake()
{
    instance = this;
    
            highScore1 = PlayerPrefs.GetInt("HighScore1");
            highScore2 = PlayerPrefs.GetInt("HighScore2");
            highScore3 = PlayerPrefs.GetInt("HighScore3");
            highScore4 = PlayerPrefs.GetInt("HighScore4");
            highScore5 = PlayerPrefs.GetInt("HighScore5");
    
    createArray();
}

void Start()
{
    
}

//Creates Initial Array
public void createArray()
{
    int[] HighScore = {highScore1, highScore2, highScore3, highScore4, highScore5};
    this.HighScore = HighScore;
}

//Allows for the array to be returned
public int[] getArray()
{
    createArray();
    
    return this.HighScore;

}
将高分打印到屏幕的代码

    public String[] printScores() // Gett array of high scores as string array
{
    Highscores = scoreManager.getArray(); 
    String[] scores = new string[Highscores.Length];
    for(int i = 0; i < Highscores.Length; i++)
    {
        text.text = text.text +  "Score " + (i+1).ToString() + "                                          " + Highscores[i].ToString() + "\n";
    }

    return scores;
}
public String[]printScores()//获取作为字符串数组的高分数组
{
Highscores=scoreManager.getArray();
String[]scores=新字符串[Highscores.Length];
for(int i=0;i

我正在Galaxy S10+上构建和运行游戏。当玩家失败时,将调用检查高分的代码。打印分数的代码在该场景的Start()方法中被调用。

密切关注PlayerPrefs!他们喜欢改变。

撇开玩笑不谈,你的电脑和带有PlayerPrefs的手机之间的区别只是第一个值。如果第一次调用PlayerPrefs.GetInt,该值将是随机的,不会从零开始。在你的电脑上,你可能第一次就可以设置高分,所以在接下来的尝试中,它总是有效的。 在手机上,当你第一次玩游戏时,第一个高分可能是负数,所以你的系统的其他部分都坏了

而不是写作

PlayerPrefs.GetInt("Higscore");


这样,如果Unity第一次没有找到该值,它会将其设置为零。您的系统将正常工作

检查文件是否保存在手机上如果根本不起作用(文件夹不存在),您可能需要将自己的安全方法和文件写入Application.persistantPath。您可以在Unity文档中找到文件的位置。无论如何,对于高分来说可能更好,因为你可以加密和解密你的文件,这样操作就会变得更加困难。MathewHD的建议至少是一种减少用户进度损失的方法。您应该将重要的值保存到专用的file@MathewHD在测试了更多的东西之后,我相信比较另一个文件中的分数的方法可能会有问题。当我用PlayerPref代码硬编码脚本中的值时,它会显示在Android上。
该值将是随机的,不会从零开始。。我从没听说过。。如果该键不存在,它将使用
int
的默认值,即
0
。@derHugo ok可能不是随机的,但它甚至不是零。也许这取决于设备,但我认为player prefs将从一个不存在的内存单元进行搜索,就像搜索维度为6的数组的第7个单元一样。当我为android编写代码时,我的默认高分是一个6位数的数字,游戏中的最大值大约是2位数。我认为这也是PlayerPref将此部分作为默认值的原因。但也许我错了:如果您签出了,您将找到
公共静态int-GetInt(字符串键){int-defaultValue=0;对于没有默认参数的版本,返回PlayerPrefs.GetInt(key,defaultValue);}
)这是你我之间的事@derHugo先生;)@很抱歉反应太晚。更改GetInt()以包含默认值并没有解决问题。然而,我一直在测试一些东西,硬编码这些值使这些值在Android上弹出。if(PlayerPrefs.HasKey(“HighScore1”){PlayerPrefs.SetInt(“HighScore1”,15);}