Unity编辑器拓展之十三:CSV文件解析代码生成工具

博客迁移

个人博客站点,欢迎访问,www.jiingfengji.tech

CSV文件解析代码生成工具

工具示意图

这里写图片描述

左侧是工程下所有的CSV文件列表,并提供搜索框,右侧是解析部分,上边提供了保存代码文件和拷贝代码的菜单,中间是CSV文件预解析出来的字段、数据类型(可选)等,下面是代码预览区域。

获取所有CSV文件的路径

使用递归获取所有CSV文件的路径,并绘制在左侧

	[MenuItem("Tools/CreateCSVParseCode")]
	public static void CSVCode()
	{
		csvFilesPathList.Clear ();
        assetPath = Application.dataPath;
        //获取所有CSV文件的路径
        GetFiles (new DirectoryInfo (assetPath), "*.csv", ref csvFilesPathList);
		if (window == null)
			window = EditorWindow.GetWindow(typeof(CreateCSVParseCode)) as CreateCSVParseCode;
		window.titleContent = new GUIContent("CreateCSVParseCode");
		if(isLimitSize)
		{
			window.minSize = new Vector2(1420,674);
			window.maxSize = new Vector2(1420,674);
		}
		window.Show();
	}

	public static void GetFiles (DirectoryInfo directory, string pattern, ref List<string> fileList) 
	{
        if (directory != null && directory.Exists && !string.IsNullOrEmpty (pattern)) {
            try {
                foreach (FileInfo info in directory.GetFiles (pattern)) {
                    string path = info.FullName.ToString ();
                    fileList.Add (path.Substring (path.IndexOf ("Assets")));
                }
            } catch (System.Exception) 
			{
                throw;
            }
            foreach (DirectoryInfo info in directory.GetDirectories ()) 
			{
                GetFiles (info, pattern, ref fileList);
            }
        }
    }

预解析CSV文件

定义一个类存储预解析数据

public class CsvData
{
	public string header;
	public DataType dataType;
	public bool isParse = true;
}
public enum DataType
{
	Int = 0,
	String = 1,
	Double = 2,
	Bool = 3,
	//后续更新下面三种类型数据的解析
	//Vector3 = 4,
	//Vector2 = 5,
	//Color = 6,
}

详细代码参考后面代码工程git地址

代码格式化

将解析代码格式化存储在一个类中,以string format的形式存在。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CSVCodeConfig 
{
	public static string DataClassFormat = @"public class {0}
{{
{1}}}";

	//如果是带Vector3或者Vector3类型的数据需要使用带有UnityEngiine命名空间
	public static string DataClassNameSpaceFormat = @"
using UnityEngine;

public class {0}
{{
{1}
}}";

	public static string FieldFormat = "\tpubilc {0} {1} {{ set; get; }}";

	public static string ClassNameFormat = "{0}Model";

	public static string ModelListPrivateStatementFormat = "private List<{0}> {1}";

	public static string ModelListPublicPropertyFormat = @"
public List<{0}> {1}
{{
	get
	{{
		if({2} == null)
		{{
			{3}();
		}}
		return {4};
	}}
}}";

	public static string parseMethodNameFormat = "Init{0}";
	public static string methodFormat = "{0}();";
	public static string ModelDataParseMethodFormat = @"
private void {0}()
{{
	{1} = new List<{2}>;
	string path = DEFAULT_CSV_PATH + {3};
	CSVTable csvTable = CsvLoader.LoadCSV(path);
	foreach (CSVRecord record in csvTable.Records)
	{{
		{4} temp = new {5}();
		foreach (string header in csvTable.Headers)
		{{
			{6}
		}}
		{7}.Add(temp);
	}}
}}";

	public static string ModelDataHeaderEqualFormat_1 = @"if (string.Equals(header, {0}))
			{{
				{1}
			}}";

	public static string ModelDataHeaderEqualFormat_2 = @"
			else if (string.Equals(header, {0}))
			{{
				{1}
			}}";
	
	public static string ModelDataFieldValuationFormat_String = "temp.{0} = record.GetField(header);";
	public static string ModelDataFieldValuationFormat_Int = "temp.{0} = ConvertUtil.Str2Int(record.GetField(header));";
	public static string ModelDataFieldValuationFormat_Double = "temp.{0} = ConvertUtil.Str2Double(record.GetField(header));";
	public static string ModelDataFieldValuationFormat_Bool = "temp.{0} = ConvertUtil.Str2Int(record.GetField(header)) != 0;";
	
}

完整工程

https://github.com/JingFengJi/CSVParseCodeCreate

第三方ReorderableList编辑器工具

Rotorz

下载地址:https://bitbucket.org/rotorz/reorderable-list-editor-field-for-unity

以上知识分享,如有错误,欢迎指出,共同学习,共同进步。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值