【问题标题】:How do I save and load the value of a text in Unity? [duplicate]如何在 Unity 中保存和加载文本的值? [复制]
【发布时间】:2020-02-25 01:17:01
【问题描述】:

我有一个代码,其中有一个经验栏,在它下面有一个级别文本框和一个经验文本框。代码如下:

    public Slider barraExperiencia;
    public Button botonManzana;
    public Text txtNumeroNivel, txtNumeroExperiencia;

    void Start()
    {
        barraExperiencia.value = PlayerPrefs.GetFloat("Experiencia");
        nivel = PlayerPrefs.GetFloat("Nivel");

        botonManzana.onClick.AddListener(ButtonAlimentoClicked);

    }

    void Update()
    {
        PlayerPrefs.SetFloat("Experiencia", barraExperiencia.value);
        PlayerPrefs.SetFloat("Nivel", nivel);
        txtNumeroExperiencia.text = barraExperiencia.value.ToString() + "/" + barraExperiencia.maxValue.ToString();

        if (barraExperiencia.value >= barraExperiencia.maxValue)
        {
            barraExperiencia.value = 0;
            nivel += 1;
            txtNumeroNivel.text = nivel.ToString();
            barraExperiencia.maxValue += 100;
        }

    }

    void ButtonAlimentoClicked()
    {
       barraExperiencia.value += 10;
    }

在代码中,当按下按钮 botonManzana 时,它会增加条形的值。当 bar 的值达到最大值时,它返回 0,变量 nivel(级别)增加。

我想保存和加载 bar 和“nivel”的值,我已经使用 PlayerPrefs 完成了它,就像代码中显示的那样,但它不起作用。如果有人可以帮助我,请。

【问题讨论】:

    标签: c# user-interface unity3d save load


    【解决方案1】:

    您需要调用 Save() 来保存对 PlayerPrefs 的更改。见https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

    如果 OnApplicationQuit 没有被调用,它可能不会被自动调用,例如如果您的应用程序是 WebGL 应用程序。请参阅https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationQuit.html 了解更多信息。

    【讨论】:

    • 也不要将 PlayerPrefs 用于游戏状态数据,它不是为此而设计的,它是为用户级别选项(例如音量级别)而设计的,这就是它不支持任意对象的原因。跨度>
    猜你喜欢
    • 1970-01-01
    • 2016-12-22
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 2018-04-11
    • 1970-01-01
    • 2017-02-25
    相关资源
    最近更新 更多