工具类-AutoCreateScript
用于生成csv文件对应的数据类
csv文件测试数据
生成的数据类
unity Assets下目录结构
工具类AutoCreateScript代码如下:
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
public class AutoCreateScript
{
public static string csv_folder = Application.dataPath + "/Auto/Csv/"; //读取的CSV目录
public static string script_folder = Application.dataPath + "/Auto/Model/";//生成的数据类所在目录
//数据类模板
public static string class_template =
@"
using System;
public class #class_name#
{
//auto
#class_menbers#
}
";
/// <summary>
/// 生成csv目录下所有csv对应数据类
/// </summary>
[MenuItem("Tools/CreateScripts/All")]
public static void All()
{
string[] files = Directory.GetFiles(csv_folder);
if (files.Length < 1)
{
Debug.LogError("get files from " + csv_folder + " fail...");
return;
}
for (int index = 0; index < files.Length; index++)
{
CreateSingleScript(files[index]);
}
}
/// <summary>
/// 生成csv目录下选中的csv对应的数据类
/// </summary>
[MenuItem("Tools/CreateScripts/Single")]
public static void Single()
{
string[] csv_guids = Selection.assetGUIDs;//选中的csv文件的guid
if (csv_guids.Length < 1)
{
Debug.LogError("select csv count < 1");
return;
}
List<string> csv_paths = new List<string>();
for (int guids_index = 0;guids_index<csv_guids.Length;guids_index++)
{
string csv_path = AssetDatabase.GUIDToAssetPath(csv_guids[guids_index]);//unity 通过guid获取路径
csv_path = Application.dataPath + csv_path.Substring(csv_path.IndexOf("/"));//通过guid获取的路径需要处理下 转成文件系统里的绝对路径
CreateSingleScript(csv_path);
}
}
/// <summary>
///
/// </summary>
/// <param name="file_path"></param>
private static void CreateSingleScript(string file_path)
{
if (string.IsNullOrEmpty(file_path))
{
return;
}
//忽略.meta
if (file_path.Substring(file_path.LastIndexOf(".") + 1) == "meta")
{
return;
}
//跳过非csv文件
if (file_path.Substring(file_path.LastIndexOf(".") + 1) != "csv")
{
Debug.Log(file_path + " 非csv文件");
return;
}
string file_name = file_path.Substring(file_path.LastIndexOf("/") + 1).Split('.')[0];//csv文件名,主要用于生成对应数据类的类名
StreamReader sr = null;
try
{
sr = File.OpenText(file_path);
}
catch
{
Debug.LogError("File cannot find ! ");
return;
}
string line = sr.ReadLine();
if (null == line)
{
Debug.Log(file_path + " read fail");
return;
}
line = sr.ReadLine();
sr.Close();
sr.Dispose();
file_name = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(file_name) + "DataAuto";
string script_path = script_folder + file_name + ".cs";
if (File.Exists(script_path))
{
File.Delete(script_path);
}
Debug.Log(file_name);
string class_content = class_template;
string memberstring = "";
string[] column = line.Split(',');
for (int column_index = 0; column_index < column.Length; column_index++)
{
string[] column_detail = column[column_index].Split('|');
memberstring += "public " + column_detail[1] + " " + column_detail[0] + ";\r\n\t";
}
class_content = class_content.Replace("#class_name#", file_name);
class_content = class_content.Replace("#class_menbers#", memberstring);
FileStream file = new FileStream(script_path, FileMode.CreateNew);
StreamWriter fileW = new StreamWriter(file, Encoding.UTF8);
fileW.Write(class_content);
fileW.Flush();
fileW.Close();
file.Close();
Debug.Log("create " + Application.dataPath + "/Auto/Model/" + file_name + ".cs success!");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}