unity使用SharpConfig读取修改配置文件

gamesetting.cfg  放在streamingAssets下

[GameStting]
name = gamesetting

[Test]
name = newName

[Array]
name = {123,345,567,879}

using SharpConfig;

加载配置文件

    public void Load()
    {
        string filepath = Application.streamingAssetsPath + "/gamesetting.cfg";
        Configuration cfg = Configuration.LoadFromFile(filepath);

        Section gamesetting = cfg["GameStting"];
        string name = gamesetting["name"].StringValue;

        Section test = cfg["Test"];
        string name1 = test["name"].StringValue;

        Debug.Log(name + "     " + name1);
    }

修改配置文件

   public void SaveCon()
    {
        Configuration cfg = new Configuration();
        string filepath = Application.streamingAssetsPath + "/gamesetting.cfg";

        Section gamesetting = new Section("GameStting");
        gamesetting.Add(new Setting("name", "gamesetting"));
        cfg.Add(gamesetting);

        Section test = new Section("Test");
        test.Add(new Setting("name", "newName"));
        cfg.Add(test);

        string[] str = { "123", "345", "567", "879" };
        Section array = new Section("Array");
        array.Add(new Setting("name", str));
        cfg.Add(array);

        cfg.SaveToFile(filepath);
    }

猜你喜欢

转载自blog.csdn.net/LuffyZ/article/details/106237719