Text格式的配置表读取

sing System.Collections.Generic;
using System.IO;
using System;
using System.Reflection;
using UnityEngine;


public class TextConverter {
    private static string[] lineContents;


    public static List<T> GetTypeData<T>(string fileName) where T : class, new()
    {
        List<T> mlist = new List<T>();
        TextAsset binAsset = LoadConfig("Config/"+ fileName, typeof(TextAsset)) as TextAsset;
        lineContents = binAsset.text.Split('\r'); //获取所有行的数据
        string[] keys = lineContents[0].Split('\t');//获取配置表第一行的字段名
        for (int i = 2; i < lineContents.Length; i++)
        {
            T t = new T();
            string[] values = lineContents[i].Split('\t');
            if (values.Length == 1)
                break;
            for (int j = 0; j < values.Length; j++)
            {
                FieldInfo f = t.GetType().GetField(keys[j]);
                //Debug.Log(keys[j] + ":" + values[j]);
                f.SetValue(t, Convert.ChangeType(values[j], f.FieldType));
            }
            mlist.Add(t);
        }
        return mlist;

    }


public static Object LoadConfig(string name, System.Type type)
    {
        Object obj = null;
       
        obj = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/" + name + ".txt", type);
        return obj;
    }

}

猜你喜欢

转载自blog.csdn.net/sun1018974080/article/details/77837927