unity读取配置文件confitData

using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Events;

public class LoadConfigData : MonoBehaviour
{
    public Dictionary<string, string> configDic = new Dictionary<string, string>();
    public static LoadConfigData loadConfig;
    private UnityAction _loadDone;

    private void Awake()
    {
        loadConfig = this;
        ReadConfigData();
    }
    public void ReadConfigData()
    {
        LoadFileAndIntialSpeed(Application.streamingAssetsPath, "configData.txt");
    }
    void LoadFileAndIntialSpeed(string sPath, string sName)
    {
        StreamReader sr = null;
        sr = File.OpenText(sPath + "//" + sName);

        string t_Line = "";

        if ((t_Line += sr.ReadLine()) != null)
        {
            // Debug.Log(t_Line);
            ConfigLoadDone(t_Line);
        }
        else
        {
            Debug.Log("配置文件错误");
        }

        sr.Close();
        sr.Dispose();
    }

    public void WirteConfigData(string info)
    {
        WirteConfigData(Application.streamingAssetsPath, "configData.txt", info);
    }

    void WirteConfigData(string sPath, string sName, string info)
    {
        FileStream file = File.OpenWrite(sPath + "//" + sName);
        //得到字符串的UTF8 数据流
        byte[] bts = System.Text.Encoding.UTF8.GetBytes(info);
        // 文件写入数据流
        file.Write(bts, 0, bts.Length);
        if (file != null)
        {
            //清空缓存
            file.Flush();
            // 关闭流
            file.Close();
            //销毁资源
            file.Dispose();
        }
    }

    private void ConfigLoadDone(string jsonInfo)
    {
        JsonData json = JsonMapper.ToObject(jsonInfo);

        foreach (var item in json.Keys)
        {
            configDic.Add(item, json[item].ToString());
        }
        if (_loadDone != null)
        {
            _loadDone.Invoke();
        }
    }

    public string GetConfigInfo(string infoKeys)
    {
        if (configDic.ContainsKey(infoKeys))
        {
            return configDic[infoKeys];
        }
        else
        {
            //Debug.Log("当前的Config中没有该值");
            return "";
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Unity 3D中,我们可以使用C#代码来读取配置文件。首先,我们需要创建一个文本文件来存储配置数据。可以使用任何文本编辑器创建一个.cfg或者.txt文件,并将其命名为你喜欢的名称,比如config.cfg。 接下来,我们可以使用Unity的Asset Store中的Simple File Browser插件来让用户选择配置文件。该插件可以方便地让用户在文件系统中选择文件,并获取文件的完整路径。 一旦我们有了配置文件的路径,我们就可以使用C#代码来读取它。我们可以使用Unity中的StreamReader类来读取文本文件的内容。下面是一个示例代码: ``` using UnityEngine; using System.IO; public class ConfigReader : MonoBehaviour { void Start() { string filePath = "选择的配置文件路径"; // 读取配置文件 StreamReader reader = new StreamReader(filePath); string configData = reader.ReadToEnd(); reader.Close(); // 处理配置数据 ProcessConfigData(configData); } void ProcessConfigData(string data) { // 在这里编写处理配置数据的代码 } } ``` 在上面的代码中,我们使用StreamReader打开和读取文件的内容,然后将其保存到一个字符串变量中。一旦我们获得了配置数据,我们可以编写处理数据的代码,根据具体需求进行解析和使用。 读取配置文件后,我们可以使用Split函数等C#字符串操作方法将配置数据分割成适当的键值对或者其他数据结构,然后可以根据需要将其用于游戏的逻辑和功能。 请注意,在使用StreamReader读取文件后,我们应该调用Close方法关闭文件流,以释放资源并避免内存泄漏。 这就是使用Unity 3D读取配置文件的基本方法。根据具体的需求,我们可以根据配置文件的格式进行适当的解析和处理,以满足游戏的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌上桑AGO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值