Unity里用C# 简单读取和写入CSV文件的通用模板

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/NippyLi/article/details/81048157

我这里读取和写入csv的想法是想根据一个数据格式类作为判定依据,后面针对的读取想要的数据就比较方便。

我这里默认表数据格式是第一行类型字符串, 第二行属性键, 第三行属性值 。默认的主键ID是int

已经修改为按key值读取,博客代码为旧代码的。新的看https://github.com/ChuKuang/CSVHelper

  • 定义一个空的数据类
  public class CSVDataBase
    {
        //子类里面定义属性,目前只支持(int ,string, float, bool)
        //public int Id { get; set; }
    }
  • 根据数据类读取数据到内存中,用字典保存
//只读取csv字符串里的属性数据,其他无关数据不读取
        public static Dictionary<int, T> GetDataTable<T>(string csvTableStr) where T: CSVDataBase, new ()
        {

            string content = csvTableStr.Replace("\r", "");
            string[] lines = content.Split('\n');
            if(lines.Length < 3)
            {
                Debug.Log("the table is empty");
                return null;
            }

            Dictionary<int, T> dic = new Dictionary<int, T>();
            //string keyLinse = lines[0];

            PropertyInfo[] pins = typeof(T).GetProperties();

            for(int i = 2; i < lines.Length; i ++)
            {
                T data = new T();
                string[] values = lines[i].Split(',');
                int major = GetInt(values[0]);

                for(int j =0; j < values.Length; j++)
                {
                    string value = values[j].Trim();
                    Type type = pins[j].PropertyType;

                    if(type == typeof(int))
                    {
                        pins[j].SetValue(data, GetInt(value), null);
                    }
                    if(type == typeof(float))
                    {
                        pins[j].SetValue(data, GetFloat(value), null);
                    }
                    if(type == typeof(bool))
                    {
                        pins[j].SetValue(data, GetBool(value), null);
                    }
                    else
                    {
                        pins[j].SetValue(data, GetString(value), null);
                    }
                }
                dic.Add(major, data);
            }

            return dic;
        }
  • 将自己操作的csv数据,转换为字符串,方便后续存储为文本
      // 生成的第一行是类型(int, string等)
        //第二行属性名字,对应类属性名
        //第三行开始是属性值
        //将返回的字符串保存文本就是完整csv格式了
        public static string GetCSVContent<T>(Dictionary<int, T> dictionary) where T : CSVDataBase, new()
        {
            string content = string.Empty;
            PropertyInfo[] keyProper = typeof(T).GetProperties();

            foreach (PropertyInfo item in keyProper)
            {
                content += GetTypeName(item.PropertyType.Name);
            }
            content.Trim();
            content += "\n";

            foreach (PropertyInfo item in keyProper)
            {
                content += item.Name + ",";
            }
            content.Trim();

            foreach (T data in dictionary.Values)
            {
                content += "\n";

                //这路通过反射,获取所有值拼接成字符串
                foreach (PropertyInfo item in keyProper)
                {
                    System.Object value = item.GetValue(data, null);
                    if (data == null)
                        value = string.Empty;

                    content += (value.ToString() + ",").Trim();
                }

                content += content.Remove(content.Length - 1);
            }
            return content;
        }
  • 一些格式转换的辅助方法
 public static int GetInt(string dText)
        {
            if (string.IsNullOrEmpty(dText))
                return 0;
            else
                return (int)float.Parse(dText);
        }

        public static string GetString(string dText)
        {
            if (string.IsNullOrEmpty(dText))
                return string.Empty;
            else
                return dText;
        }

        public static float GetFloat(string dText)
        {
            if (string.IsNullOrEmpty(dText))
                return 0.0f;
            else
                return float.Parse(dText);
        }


        //这里我当表里的是"true", "false"
        //如果是用0 和1代替的需要更改
        public static bool GetBool(string dText)
        {
            if (string.IsNullOrEmpty(dText))
                return false;    
            else
                return bool.Parse(dText.ToLower());
        }


        public static string GetTypeName(string type)
        {
            string name = string.Empty;
            switch(type)
            {
                case "Int32":
                    name = "int";
                    break;
                case "String":
                    name = "string";
                    break;
                case "Single":
                    name = "float";
                    break;
                case "Boolean":
                    name = "bool";
                    break;
                default:
                    name = type;
                    break;
            }

            return name;
        }

猜你喜欢

转载自blog.csdn.net/NippyLi/article/details/81048157
今日推荐