如何在Unity中保存和加载文本值?

问题描述 投票:0回答:1

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

    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时,它会增加该条的值。当条形的值达到最大值时,它返回0,并且变量nivel(级别)增加。

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

c# user-interface unity3d save load
1个回答
0
投票

您需要调用Save()将更改保存在PlayerPrefs上。参见https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

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

© www.soinside.com 2019 - 2024. All rights reserved.