/// <summary> /// 替换文件冲突的guid /// </summary> /// <param name="filePath">文件路径</param> /// <param name="duplicateGuidList">重复有冲突的guid列表</param> /// <param name="replaceGuidList">要替换的guid列表,各个元素索引与duplicateGuidList一致</param> private void ReplaceFileDuplicateGuid(string filePath, string[] duplicateGuidList, string[] replaceGuidList) { Regex regex = new Regex(@"guid:\s*"); List <string> fileLines = FileUtil2.GetFileLines(filePath, true, -1); int len = fileLines.Count; for (int i = 0; i < len; i++) { string line = fileLines[i]; Match match = regex.Match(line); if (match.Success) { int guidStartIndex = match.Index + match.Value.Length; string guidString = line.Substring(guidStartIndex, 32); int atDuplicateListIndex = Array.IndexOf(duplicateGuidList, guidString); if (atDuplicateListIndex > -1) { line = line.Remove(guidStartIndex, 32); line = line.Insert(guidStartIndex, replaceGuidList[atDuplicateListIndex]); fileLines[i] = line; } } } FileUtil2.WriteFileLines(fileLines.ToArray(), filePath); }
/// <summary> /// 返回指定文件夹下所有.meta文件的guid列表 /// </summary> /// <param name="folderPath">文件夹路径,如果是'\'路径,需要加@转换,如:getFolderGuidList(@"E:\unity_tags\Assets")</param> /// <param name="displayProgressBar">是否显示进度条</param> /// <returns></returns> public static string[] GetAllMetaFileGuidList(string folderPath, bool displayProgressBar = false) { if (displayProgressBar) { EditorUtility.DisplayProgressBar("Hold on...", "Get guid of all meta files...", 0f); } DirectoryInfo directoryInfo = new DirectoryInfo(folderPath); FileInfo[] fileInfos = directoryInfo.GetFiles("*.meta", SearchOption.AllDirectories); Regex regex = new Regex(@"guid:\s*", RegexOptions.Compiled); int len = fileInfos.Length; string[] list = new string[len]; for (int i = 0; i < len; i++) { FileInfo fileInfo = fileInfos[i]; List <string> fileLines = FileUtil2.GetFileLines(@fileInfo.FullName, false, 2); if (displayProgressBar) { EditorUtility.DisplayProgressBar("Hold on...", "Get guid of all meta file" + fileInfo.Name, (i + 1f) / (float)len); } //meta文件都是在第二行表示guid string guidLine = fileLines[1]; Match match = regex.Match(guidLine); list[i] = guidLine.Substring(match.Value.Length, 32); } if (displayProgressBar) { EditorUtility.ClearProgressBar(); } return(list); }
/// <summary> /// 修改.cs文件 /// </summary> /// <param name="filePath">文件路径,如果是'\'路径,需要加@转换,如:editCSharpFile(@"E:\unity_tags\Assets\Main.cs")。</param> /// <param name="projectName">导入的项目名称</param> private void EditCSharpFile(string filePath, string projectName) { List <string> fileLines = FileUtil2.GetFileLines(filePath, true); //修正不兼容的"SortingLayer"代码,使用"SortingLayer2"替换 FixSortingLayerCode(fileLines); //修正不兼容的"LayerMask"代码,使用"LayerMask2"替换 FixLayerMaskCode(fileLines); //修正不兼容的"SceneManager"代码,使用"SceneManager2"类替换 FixSceneManagerCode(fileLines); //修正不兼容的"QualitySettings"代码,使用"QualitySettings2"类替换 FixQualitySettingsCode(fileLines); //检测并添加以项目命名的namespace到.cs文件 CheckAndAddNameSpaceToCSharpFile(fileLines, projectName, filePath); //重新写入文件 FileUtil2.WriteFileLines(fileLines.ToArray(), filePath); }
/// <summary> /// 修改.unity文件 /// 注意:请确保需要修改的.unity文件没有在编辑器中打开,否则会修改不成功 /// </summary> /// <param name="filePath">文件路径,如果是'\'路径,需要加@转换,如:editCSharpFile(@"E:\unity_tags\Assets\main.unity")。</param> /// <param name="projectName">导入的项目名称</param> private void EditUnityFile(string filePath, string projectName) { //是否在编辑器中打开.unity string tempFilePath = filePath.Replace('\\', '/'); int sceneCount = EditorSceneManager.sceneCount; for (int i = 0; i < sceneCount; i++) { var scene = EditorSceneManager.GetSceneAt(i); if (tempFilePath.IndexOf(scene.path, StringComparison.Ordinal) > -1) { Debug.LogError("请关闭" + scene.path + "再重新导入"); break; } } // List <string> fileLines = FileUtil2.GetFileLines(filePath, true); //修正引用的SortingLayer.id FixSortingLayerID(fileLines); //重新写入文件 FileUtil2.WriteFileLines(fileLines.ToArray(), filePath); }