/// <summary> /// 创建脚本,受自定义命名空间 /// </summary> /// <param name="name"></param> /// <param name="folderPath"></param> private static void GenerateScript(string name, string folderPath) { string fullPath = folderPath + "/" + name + ".cs"; FileStream fs = new FileStream(fullPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); fs.Close(); //写入作者名、日期时间、命名空间、类名 string content = EditScriptTemplate.EditScriptInfo(name) + EditScriptTemplate.EditCodeTemplate(ScriptTemplateData.CustomNamespace, name); //写入文件 File.WriteAllText(fullPath, content); }
/// <summary> /// 在创建资源的时候会调用此方法 /// path为资源所在路径 /// </summary> private static void OnWillCreateAsset(string path) { if (ScriptTemplateData.IsEditScriptTemplate == false) { return; } //1.获取新建的脚本的路径 path = path.Replace(".meta", ""); //2.安全检验脚本路径 if (path.EndsWith(".cs")) { //3.读取所有内容为一个字符串 string content = File.ReadAllText(path); //4.获取脚本类名 string className = EditScriptTemplate.GetClassName(content); //5.更改 写入作者名、日期时间、命名空间、类名 content = EditScriptTemplate.EditScriptInfo(className) + EditScriptTemplate.EditCodeTemplate(ScriptTemplateData.CustomNamespace, className); //6.写入文件 File.WriteAllText(path, content); UnityEditor.AssetDatabase.Refresh(); } }