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;
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;
}