游戏关闭后无法保存数据以加载它(Unity3d)

Can't save data to load it after game was closed (Unity3d)

我想保存一些变量的值。我 将其保存到文件,但它不起作用。所以现在我尝试另一种方式。但它也不会保存数据...如果我在 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);

当您使用 new 关键字时,您正在创建 IndicatorsInfonew 实例 。您从 new 实例 中保存了 indicatorsInfo.walkSpeedTemfFile 而没有 value 分配给新的 indicatorsInfo 你创造了。

也许您想要做的是保存来自 Editor 的值?

GameObject gameManagerObject = GameObject.FindGameObjectWithTag("GAMEMANAGER");
GAMEMANAGER gameManager = gameManagerObject.GetComponent<GAMEMANAGER>();
PlayerPrefs.SetFloat("my_setting", gameManager.walkSpeedTemp);