我想保存一些变量的值。我试图将其保存到文件中,但不起作用。所以现在我以另一种方式尝试。但是它也不会保存数据...如果我在Unity中启动仿真,更改变量的值,停止仿真然后再次启动,则我的变量值很旧。
void OnEnable()
{
LoadFromFile();
}
void OnDisable()
{
SaveToFile();
}
public void SaveToFile()
{
GameObject gameManagerObject = GameObject.FindGameObjectWithTag("GAMEMANAGER");
GAMEMANAGER gameManager = gameManagerObject.GetComponent<GAMEMANAGER>();
IndicatorsInfo indicatorsInfo = new IndicatorsInfo();
PlayerPrefs.SetFloat("my_setting", indicatorsInfo.walkSpeedTemfFile);
}
public void LoadFromFile()
{
GameObject gameManagerObject = GameObject.FindGameObjectWithTag("GAMEMANAGER");
GAMEMANAGER gameManager = gameManagerObject.GetComponent<GAMEMANAGER>();
gameManager.walkSpeedTemp = PlayerPrefs.GetFloat("my_setting");
}
[Serializable]
class IndicatorsInfo
{
public float walkSpeedTemfFile;
}
IndicatorsInfo indicatorsInfo = new IndicatorsInfo(); PlayerPrefs.SetFloat("my_setting", indicatorsInfo.walkSpeedTemfFile);
您正在创建有关何时使用关键字的新 实例。您是从该新实例保存的,而没有为您创建的新实例分配值。IndicatorsInfo
new
indicatorsInfo.walkSpeedTemfFile
indicatorsInfo
也许您想要做的是保存编辑器中的值?
GameObject gameManagerObject = GameObject.FindGameObjectWithTag("GAMEMANAGER");
GAMEMANAGER gameManager = gameManagerObject.GetComponent<GAMEMANAGER>();
PlayerPrefs.SetFloat("my_setting", gameManager.walkSpeedTemp);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句