如何在Unity中保存分数 [英] How to save score in Unity

查看:944
本文介绍了如何在Unity中保存分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am stuck with the following problem: I have a fishing game. So far, if I play the game, I can start fishing, catch 2,3,4 more fish and everything is fine.

但是,如果我想暂停游戏(按ESC或我放置在场景中的按钮),则会重置我的分数.此外,即使暂停游戏或进入购物按钮,我也不知道如何保存分数.

However, if I want to pause the game (Pressing ESC or a button that I placed in the scene) my score resets. Additionally, I don't know how to save my score, even if I pause the game or go into shop button.

启动功能

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

}

推荐答案

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

I think this is not the right way to save/load your score by doing operations on the Text.text directly.
If you want to save and load your score, make a separate variable score to store your score, then use your score in your UI Elements.
For example, this explains how you save scores at runtime:

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类中使用您的分数:

And use your score in your UI class:

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

别忘了using UnityEngine.UI,以这种方式保存/加载得分会容易得多.

don't forget using UnityEngine.UI, saving/loading score in this way will be much easier.

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

Usually, you don't need to call PlayerPrefs.Save() since the data will be automatically written to disk when the game quits (it calls automatically in OnApplicationQuit()), but you may want to call it at certain point (i.e checkpoints) in case of program crushes or other unexpected situations.

这篇关于如何在Unity中保存分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆