/// <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> /// 导入项目的Assets文件夹,并修改.cs文件解决冲突 /// </summary> /// <param name="path">需要导入Assets文件夹的项目路径</param> /// <param name="currentProjectTempPath">临时文件夹</param> /// <param name="projectName">需要导入项目名称</param> public override void Import(string path, string currentProjectTempPath, string projectName) { //当前项目的Assets文件夹的全路径,路径中使用"/"分隔,不是"\"。 string assetsPath = Application.dataPath; //备份当前项目的所有GUID用于判断是否重复 string[] oldGuidList = GuidUtil.GetAllMetaFileGuidList(assetsPath, true); //创建子项目目录,如果目录存在则先删除 string childProjectPath = assetsPath + "/" + projectName; FileUtil2.CreateDirectory(childProjectPath, true); EditorUtility.ClearProgressBar(); //子项目Assets目录 string childProjectAssetsPath = childProjectPath + "/Assets"; //复制项目的Assets文件夹到子项目路径 FileUtil2.CopyDirectory(path + "/Assets", childProjectAssetsPath); //删除DOTweenSettings.asset DeleteDOTweenSettingsAsset(childProjectAssetsPath); //删除不需要导入的文件夹,如Editor、Gizmos、Plugins等 ForeachAndDeleteIgnoreFolders(childProjectAssetsPath); //修改文件夹下的.cs文件解决冲突 ForeachAndEditCSharpFiles(childProjectAssetsPath, projectName); //修改文件夹下的.unity文件,修正SortingLayer等 ForeachAndEditUnityFiles(childProjectAssetsPath, projectName); //修改冲突的GUID ForeachAndEditGuids(childProjectAssetsPath, oldGuidList); }
/// <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> /// 导入一个项目的Assets文件夹和ProjectSettings /// </summary> /// <param name="path">项目的路径位置</param> /// <param name="isImportAssets">是否导入Assets文件夹</param> /// <param name="isImportBuildSettings">是否导入BuildSettings</param> /// <param name="projectName">导入进来的文件夹名;项目中的所有设置文件的名称前缀,null时将从path的最后截取</param> /// <param name="isDeleteBuildSettingsScenes">导入前是否清除由projectName指定的项目在上一次导入时在BuildSettings窗口中的场景</param> /// <param name="isDeleteAssets">导入前是否清除由projectName指定的项目在上一次导入时的资源文件夹</param> public static void ImportProject(string path, bool isImportAssets = true, bool isImportBuildSettings = true, string projectName = null, bool isDeleteBuildSettingsScenes = true, bool isDeleteAssets = true) { if (projectName == null) { projectName = path.Substring(path.LastIndexOf('/') + 1); } //删除指定项目的所有资源和设置,用于重复导入时清空上一次导入的资源和设置 DeleteProject(projectName, isDeleteBuildSettingsScenes, isDeleteAssets); //创建临时文件夹,如果文件夹存在则先删除 FileUtil2.CreateDirectory(projectImporterTempPath, true); //导入tags和Layers var tagsAndLayersImporter = new TagsAndLayersImporter(); tagsAndLayersImporter.Import(path, currentProjectTempPath, projectName); if (isImportAssets) { //导入Assets文件夹,并修改.cs文件解决冲突,必须在导入tags和Layers之后执行 var assetsImporter = new AssetsImporter(); assetsImporter.Import(path, currentProjectTempPath, projectName); } //导入Time var timeImporter = new TimeImporter(); timeImporter.Import(path, currentProjectTempPath, projectName); //导入Physics var physicsImporter = new PhysicsImporter(); physicsImporter.Import(path, currentProjectTempPath, projectName); //导入Physics2D var physics2DImporter = new Physics2DImporter(); physics2DImporter.Import(path, currentProjectTempPath, projectName); //导入Quality var qualityImporter = new QualityImporter(); qualityImporter.Import(path, currentProjectTempPath, projectName); if (isImportBuildSettings) { //导入BuildSettings var buildSettingsImporter = new BuildSettingsImporter(); buildSettingsImporter.Import(path, currentProjectTempPath, projectName); } //所有事情完成,删除"ProjectImporter/temp"临时文件夹 AssetDatabase.DeleteAsset(projectImporterTempPath); }
/// <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); }