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

本文介绍如何在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);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值