//生成一条属性; static string GeneratePropertyBlock(TitleData tileData) { //用了@忽略转移字符 这是属性的一个模版 string propertyBlock = @" private {0} {2}; /// <summary> /// {3} /// </summary> public {0} {1} { get { return {2}; } set { {2} = value; } }"; string name = tileData.name.Substring(0, 1).ToLower() + tileData.name.Substring(1); string bigName = name.Substring(0, 1).ToUpper() + name.Substring(1); propertyBlock = propertyBlock.Replace("{0}", tileData.type.ToString()); propertyBlock = propertyBlock.Replace("{1}", bigName); propertyBlock = propertyBlock.Replace("{2}", name); propertyBlock = propertyBlock.Replace("{3}", tileData.des); return(propertyBlock); }
/// <summary> /// Generates the bean class. /// 生成对象类; /// 如果没有文件重新生成。有的话更新字段; /// 这个生成的类因为会被覆盖。所以不要在里面写东西; /// </summary> /// <param name="fileName">File name.</param> /// <param name="titles">Titles.</param> public static void GenerateBeanClass(string fileName, List <TitleData> titles) { string targetPath = Application.dataPath + "/Script_Game/Data/DataBeans/"; string file = targetPath + fileName + "Data.cs"; if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } if (File.Exists(file)) { // File.Delete(file); } using (FileStream fileStream = File.Open(file, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)) { StreamWriter outfile = new StreamWriter(fileStream); // outfile.WriteLine("using UnityEngine;"); outfile.WriteLine("using System.Collections;"); outfile.WriteLine("using ExcelParser;"); outfile.WriteLine(""); outfile.WriteLine("/// <summary>"); outfile.WriteLine("/// 自动生成类。不要修改"); outfile.WriteLine("/// 数据表的第一列为key"); outfile.WriteLine("/// </summary>"); outfile.WriteLine("public class " + fileName + "Data : IDataBean {"); // outfile.WriteLine(" "); for (int i = 0; i < titles.Count; i++) { TitleData td = titles[i]; string block = GeneratePropertyBlock(td); outfile.WriteLine(block); // outfile.WriteLine(" "); } outfile.WriteLine("}"); outfile.Close(); fileStream.Close(); } AssetDatabase.Refresh(); }
public static List <TitleData> GetTitleDataList(string dataTxt) { List <TitleData> titleDataList = new List <TitleData>(); dataTxt = dataTxt.Replace("\r", ""); // dataTxt = dataTxt.Replace(" ", ""); // dataTxt = dataTxt.Replace(" ", ""); string[] hList = dataTxt.Split('\n'); string title = hList[2]; string[] titles = title.Split('\t'); string[] types = hList[0].Split('\t'); for (int i = 0; i < titles.Length; i++) { TitleData titleData = new TitleData(); if (string.IsNullOrEmpty(titles[i])) { continue; } titleData.name = titles[i]; string typeStr = types[i].ToLower(); if (typeStr == "string" || typeStr == "int" || typeStr == "float") { titleData.type = typeStr.ToLower(); } titleDataList.Add(titleData); } return(titleDataList); }
/// <summary> /// Generates the bean class. /// </summary> /// <param name="fileName">File name.</param> /// <param name="titles">Titles.</param> public static void GenerateBeanClass(string fileName, List <TitleData> titles) { string targetPath = Application.dataPath + "/ExcelParser/Script/DataBeans/"; string file = targetPath + fileName + "Bean.cs"; if (!Directory.Exists(targetPath)) { Debug.LogError("no path"); return; } FileStream fileStream = new FileStream(file, FileMode.OpenOrCreate); StreamWriter outfile = new StreamWriter(fileStream); outfile.WriteLine("using UnityEngine;"); outfile.WriteLine("using System.Collections;"); outfile.WriteLine("using ExcelParser;"); outfile.WriteLine(""); outfile.WriteLine("public class " + fileName + "Bean : IDataBean {"); outfile.WriteLine(" "); outfile.WriteLine(" "); for (int i = 0; i < titles.Count; i++) { TitleData td = titles[i]; string block = GeneratePropertyBlock(td); outfile.WriteLine(block); outfile.WriteLine(" "); } outfile.WriteLine("}"); outfile.Close(); fileStream.Close(); AssetDatabase.Refresh(); }
static List <TitleData> GetTitleDataList(string dataTxt) { List <TitleData> titleDataList = new List <TitleData>(); //去掉干扰 dataTxt = dataTxt.Replace("\r", ""); //获取所有的行数据 string[] hList = dataTxt.Split('\n'); //第三行才是 标题 string title = hList[2]; string[] titles = title.Split('\t'); //第一行是属性类型 string[] types = hList[0].Split('\t'); //第二行是注释 string titleDes = hList[1]; string[] titleDesArr = titleDes.Split('\t'); for (int i = 0; i < titles.Length; i++) { TitleData titleData = new TitleData(); if (string.IsNullOrEmpty(titles[i])) { continue; } titleData.name = titles[i]; string typeStr = types[i].ToLower(); if (typeStr == "string" || typeStr == "int" || typeStr == "float" || typeStr == "bool") { titleData.type = typeStr; } titleData.des = titleDesArr[i]; titleDataList.Add(titleData); } return(titleDataList); }
static string GeneratePropertyBlock(TitleData tileData) { string propertyBlock = @" private {0} {2}; public {0} {1} { get { return {2}; } set { {2} = value; } }" ; string name = tileData.name; string bigName = name.Substring(0, 1).ToUpper() + name.Substring(1); propertyBlock = propertyBlock.Replace("{0}", tileData.type.ToString()); propertyBlock = propertyBlock.Replace("{1}", bigName); propertyBlock = propertyBlock.Replace("{2}", name); return(propertyBlock); }