Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在统一中保存分数_C#_Android_Unity3d_Unity5 - Fatal编程技术网

C# 如何在统一中保存分数

C# 如何在统一中保存分数,c#,android,unity3d,unity5,C#,Android,Unity3d,Unity5,我陷入了以下问题:我有一个钓鱼游戏。到目前为止,如果我玩这个游戏,我可以开始钓鱼,再钓2、3、4条鱼,一切都很好 但是,如果我想暂停游戏(按ESC或我在场景中放置的按钮),我的分数将重置。此外,我不知道如何保存我的分数,即使我暂停游戏或进入商店按钮 启动功能 void Start() { PlayerPrefs.GetInt("Pesti"); NrPesti.text = PlayerPrefs.GetInt("Pesti").ToString(); PlayerPr

我陷入了以下问题:我有一个钓鱼游戏。到目前为止,如果我玩这个游戏,我可以开始钓鱼,再钓2、3、4条鱼,一切都很好

但是,如果我想暂停游戏(按ESC或我在场景中放置的按钮),我的分数将重置。此外,我不知道如何保存我的分数,即使我暂停游戏或进入商店按钮

启动功能

void Start()
{
    PlayerPrefs.GetInt("Pesti");
    NrPesti.text = PlayerPrefs.GetInt("Pesti").ToString();

    PlayerPrefs.GetInt("Viermisori");
    NrViermisori.text = PlayerPrefs.GetInt("Viermisori").ToString();

    PlayerPrefs.GetInt("Score");
    Score.text = PlayerPrefs.GetInt("Score").ToString();

    PlaySound(0);
}
更新功能:

void Update()
{
    Debug.Log(NrViermisori.text);
    Debug.Log(NrPesti.text);
    Debug.Log(Score.text);
    if (NrPesti.name =="Pesti")
    {
        NrPesti.text = "Lovers: " + NrPesti.text;
    }
    PlayerPrefs.SetString("NrPesti", NrPesti.text);
   if(NrViermisori.name == "Viermisori")
    {

        NrViermisori.text = "Beasts: " + NrViermisori.text;
    }
    PlayerPrefs.SetString("Viermisori", NrViermisori.text);


    if (Input.GetKeyDown(KeyCode.Escape)) Application.Quit();

}

您应该有一种
GameManager
类,在该类中,您将需要记住的所有内容存储在相应的
属性中。当你开始游戏时,你会创建一个GM的实例,并在游戏逻辑中引用这个对象。像分数这样的东西应该在GM中存储和处理。这样你就可以确保在游戏进行期间“保存”你的分数

在您的更新方法中,尝试在检测到Escape Press的代码中添加一些内容

if (Input.GetKeyDown(KeyCode.Escape)){ 
    PlayerPrefs.setInt("Score", Int32.Parse(Score.text));
    //Verify property is saved
    Int scoreTest = PlayerPrefs.getInt("Score");
    Debug.Log(scoreTest);
    Application.Quit();
}

祝你好运

我认为直接对
Text.Text
进行操作并不是保存/加载分数的正确方法。
如果要保存和加载分数,请创建一个单独的变量
score
来存储分数,然后在UI元素中使用分数。
例如,这说明了如何在运行时保存分数:

public class Score: MonoBehaviour {

    public int score = 0;

    // Use this for initialization
    void Start () {
        // get the score when this gameObject initialized
        score = PlayerPrefs.GetInt("Player Score");
    }

    // Update is called once per frame
    void Update () {
        // this is how you set your score to PlayerPrefs
        if (Input.GetKeyDown(KeyCode.Escape)) {
            PlayerPrefs.SetInt("Player Score", score);
        }
    }

    // this is an public function to be used 
    // outside of the class to update score
    public void UpdateScore(int amount) {
        score += amount;
    }
}
并在UI类中使用您的分数:

public class DisplayScore : MonoBehaviour {

    public Text scoreText;
    public Score playerScore;

    // Use this for initialization
    void Start () {
        // get your Score component here or just drag it in inspector
    }

    // Update is called once per frame
    void Update () {
        // this updates the score at every frame
        scoreText.text = playerScore.score.ToString();
    }
}
不要忘记使用UnityEngine.UI
,以这种方式保存/加载分数会容易得多


通常,您不需要调用
PlayerPrefs.Save()
,因为当游戏退出时,数据会自动写入磁盘(它会在
OnApplicationQuit()
中自动调用),但您可能希望在某个点调用它(即检查点)以防程序崩溃或其他意外情况。

当您将分数保存到PlayerPrefs时,我看不到任何可处理的情况。可能是因为您没有在任何地方设置Pesti的值?另外,您没有保存PlayerPrefs。我仍然不明白如何保存。@Stefansmardache您需要了解
PlayerPrefs
的工作原理,
PlayerPrefs
在游戏会话之间存储和访问玩家首选项。您可以在场景中存储一个变量,为其指定一个键和一个值,并使用您指定的键访问另一个场景中的值。您可以在此处阅读有关
PlayerPrefs
的文档:()