c# - 如何在 Unity 中保存和加载文本的值?
问题描述
我有一个代码,其中有一个体验栏,下面是一个级别文本框和一个体验文本框。这是代码:
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 完成了它,就像代码中显示的那样,但它不起作用。如果有人可以帮助我,请。
解决方案
您需要调用 Save() 以将更改保存在 PlayerPrefs 上。请参阅https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
如果 OnApplicationQuit 没有被调用,它可能不会被自动调用,例如,如果您的应用程序是 WebGL 应用程序。有关详细信息,请参阅https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationQuit.html。
推荐阅读
- microsoft-graph-api - groupPolicyConfigurations/{groupPolicyConfigurationId}/updateDefinitionValues 无法使用 API 更新配置
- python - 如果输入的值超出了我希望在多输入函数中输入的值,我可以限制输入值或返回错误吗?
- c# - Polly 处理响应并检查状态码
- php - 如何更新 Laravel 表中列的数据类型?
- python-3.x - 使用范围刮取美丽汤中的链接列表
- python-3.x - OSerror:在使用 python 3.7 更新 windows 上的 anaconda 环境后,在 tensorflow keras 中加载 h5 保存的模型
- c - 全局变量和 .data 部分
- reactjs - React - 使用 Auth0 进行不和谐身份验证
- java - 程序的“安全/锁定窗口”,Windows 10
- python - Python pandas drop_duplicates() 不准确