您当前的位置: 首页 >  unity

幻世界

暂无认证

  • 0浏览

    0关注

    237博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity解析csv或者txt配置表

幻世界 发布时间:2019-04-24 11:02:40 ,浏览量:0

欢迎加入Unity业内qq交流群:956187480

qq扫描二维码加群

制表转存csv或txt

 

 public static Dictionary LoadExcelFormTxt(string cfgName)
    {
        TextAsset tw = Resources.Load("Cfg/" + cfgName) as TextAsset;
        string strLine = tw.text;
        //开始解析字符串
        return ExplainString(strLine);
    }
    public static Dictionary ExplainString(string strLine)
    {
        //创建一个字典,用于存放数据表的内容(从配置表第四行读取)//
        Dictionary content = new Dictionary();
        string[] lineArray = strLine.Replace("\r\n", "*").Split(new char[] { '*' });
        //获取行数
        int rows = lineArray.Length - 1;
        //获取列数
        int Columns = lineArray[0].Split(new char[] { '\t' }).Length;
        //定义一个数组用于存放字段名
        string[] ColumnName = new string[Columns];
        for (int i = 0; i < rows; i++)
        {
            string[] Array = lineArray[i].Split(new char[] { '\t' });
            for (int j = 0; j < Columns; j++)
            {
                //获取Array的列的值
                string nvalue = Array[j].Trim();
                if (i == 0)
                {
                    //存储字段名
                    ColumnName[j] = nvalue;
                    content[ColumnName[j]] = new string[rows - 1];
                }
                else
                {
                    //存储对应字段的默认值//
                    content[ColumnName[j]][i - 1] = nvalue;
                }
            }
        }
        //开始解析
     return  ExplainDictionary(content);
    }
    public static Dictionary ExplainDictionary(Dictionary content)
    {
        Dictionary DicContent = new Dictionary();
        //获取字典中所有的键(字段名)
        Dictionary.KeyCollection Allkeys = content.Keys;
        //遍历所有的字段名
        foreach (string key in Allkeys)
        {
            //实例化一个hasData的字典//
            Dictionary hasData = new Dictionary();
            string[] Data = content[key];
            for (int i = 0; i < Data.Length; i++)
            {
                //
                hasData[content["ID"][i]] = Data[i];
            }
            DicContent[key] = hasData;
        }
        return DicContent;
    }

 

欢迎加入Unity业内qq交流群:956187480

qq扫描二维码加群

 

关注
打赏
1660704426
查看更多评论
立即登录/注册

微信扫码登录

0.0387s