前言
既然你看到这篇文章了,你是否也有需要使用代码创建脚本的需求?使用编辑器扩展工具根据不同的表格或者新增的内容去创建你想要的脚本。如果不使用工具,那么你只能不断去修改某个脚本,这项工作既繁琐也浪费时间。这个时候作为程序员的我们就要自己写一个工具节省时间了,那么恰好unity editor可以帮助我们实现这个工具,接下来往下看👇
准备工作
这里我打算用一个excel表格作为我需要经常更新内容。比如:我们在做本地登录时,有很多种错误码需要接入进来,但是这个错误码经常根据产品需求频繁的更新,手动改的话既繁琐又浪费时间还极容易改错,怎么解决呢?继续往下看👇
核心代码
动态创建脚本就是这两个非常简单且经常使用的api,具体怎么使用,接着往下看👇
StringBuilder sb = new StringBuilder();
//写入文件
File.WriteAllText(filePath, fileContent);
第一步:加载excel数据
/// <summary>/// 读取excel表格code码/// </summary>/// <param name="path"></param>/// <returns></returns>static Dictionary<string, string> ReadExcelFile(string path){Dictionary<string, string> datas = new Dictionary<string, string>();// StreamingAssets目录下的文件的路径FileStream fileStream = File.Open(path, FileMode.Open, FileAccess.Read);IExcelDataReader excelDataReader = ExcelReaderFactory.CreateOpenXmlReader(fileStream);// 表格数据全部读取到result里DataSet result = excelDataReader.AsDataSet();// 获取表格有多少列 int colums = result.Tables[0].Columns.Count;// 获取表格有多少行int rows = result.Tables[0].Rows.Count;for (int i = 1; i < rows; i++){if (string.IsNullOrEmpty(result.Tables[0].Rows[i][0].ToString())) continue;datas.Add(result.Tables[0].Rows[i][0].ToString(), result.Tables[0].Rows[i][1].ToString());}return datas;}
第二步:格式化类内容
/// <summary>/// 生成静态类/// </summary>/// <param name="className"></param>/// <returns></returns>private static string GenerateStaticClassContent(string className){var datas = ReadExcelFile(Application.streamingAssetsPath + "/UserMgrCode.xlsx");StringBuilder sb = new StringBuilder();sb.AppendLine("using System;");sb.AppendLine("");sb.AppendLine("public class CommonCode");sb.AppendLine("{");sb.AppendLine(" public int code;");sb.AppendLine(" public string value;");sb.AppendLine("}");sb.AppendLine("");sb.AppendLine("/// <summary>");sb.AppendLine("/// CodeEngine错误码管理引擎");sb.AppendLine("/// <summary>");sb.AppendLine("public static class " + className);sb.AppendLine("{");foreach (var item in datas){sb.AppendLine(" /// <summary>");sb.AppendLine($" /// 错误码{item.Key}");sb.AppendLine(" /// <summary>");sb.AppendLine(" public static CommonCode P_" + (item.Key) + "= new CommonCode(){code = " +(item.Key) + $", value = \"{item.Value}\"}};");sb.AppendLine("");}sb.AppendLine("}");return sb.ToString();}
第三步:写入文件,生成脚本
//写入文件
File.WriteAllText(filePath, fileContent);
结果
sing System;public class CommonCode
{public int code;public string value;
}/// <summary>
/// CodeEngine错误码管理引擎
/// <summary>
public static class CodeEngine
{/// <summary>/// 错误码401000/// <summary>public static CommonCode P_401000= new CommonCode(){code = 401000, value = "用户名或者密码为空,无法登录!"};/// <summary>/// 错误码401001/// <summary>public static CommonCode P_401001= new CommonCode(){code = 401001, value = "用户名不存在,请联系管理员!"};/// <summary>/// 错误码401002/// <summary>public static CommonCode P_401002= new CommonCode(){code = 401002, value = "密码错误,请重试!"};/// <summary>/// 错误码401003/// <summary>public static CommonCode P_401003= new CommonCode(){code = 401003, value = "输入不能为空,请重新输入!"};/// <summary>/// 错误码401004/// <summary>public static CommonCode P_401004= new CommonCode(){code = 401004, value = "用户名重复,请重新输入!"};/// <summary>/// 错误码401005/// <summary>public static CommonCode P_401005= new CommonCode(){code = 401005, value = "输入不能为空!"};/// <summary>/// 错误码401006/// <summary>public static CommonCode P_401006= new CommonCode(){code = 401006, value = "新密码不能为原始密码!"};/// <summary>/// 错误码401007/// <summary>public static CommonCode P_401007= new CommonCode(){code = 401007, value = "新密码和确认密码不一致,请重新输入!"};/// <summary>/// 错误码401008/// <summary>public static CommonCode P_401008= new CommonCode(){code = 401008, value = "新设置的密码长度不符合要求,请重新输入!"};/// <summary>/// 错误码401009/// <summary>public static CommonCode P_401009= new CommonCode(){code = 401009, value = "密码包含特殊字符,请重新输入!"};/// <summary>/// 错误码401010/// <summary>public static CommonCode P_401010= new CommonCode(){code = 401010, value = "用户添加失败!"};}
Editor使用方法
[MenuItem("模拟维修/生成LoginCode码")]public static void CreateUserCode(){string className = "CodeEngine";string filePath = $"Assets/Scripts/SimulateEngine/{className}.cs";string fileContent = GenerateStaticClassContent(className);//创建目录Directory.CreateDirectory(Path.GetDirectoryName(filePath));//写入文件File.WriteAllText(filePath, fileContent);//刷新编辑器AssetDatabase.Refresh();}
Editor编辑器扩展之创建脚本ok了,点下赞和关注,就赶紧去试试吧。