我有一个代码,其中有一个体验栏,在它下面是一个级别文本框和一个体验文本框。这是代码:
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做到了,就像代码中显示的一样,但是它不起作用。如果有人可以帮助我,请。
答案 0 :(得分:0)
您需要调用Save()将更改保存在PlayerPrefs上。参见https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
如果未调用OnApplicationQuit,例如,可能不会自动调用它。如果您的应用程序是WebGL应用程序。有关更多信息,请参见https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationQuit.html。